Here is my test class:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={AseCalculatorApplicationImplTestConfig.class})
public class AseCalculatorApplicationImplTest {
@Autowired
AseCalculatorApplicationImpl aseCalculatorApplicationImpl;
@Test
public void test() {
Assert.assertNotNull(aseCalculatorApplicationImpl);
}
}
@Configuration
class AseCalculatorApplicationImplTestConfig {
@Bean
public StaticBean staticBean() {
return Mockito.mock(StaticBean.class);
}
@Bean
public JmsTemplate jmsTopicTemplate() {
return Mockito.mock(JmsTemplate.class);
}
@Bean
public AseCalculatorApplicationImpl aseCalculatorApplicationImpl() {
return new AseCalculatorApplicationImpl();
}
}
My AseCalculatorApplicationImpl class has the autowired beans (no constructor or setters, autowired by refelection):
@Component
public class AseCalculatorApplicationImpl {
@Autowired
StaticBean staticBean;
@Autowired
JmsTemplate jmsTopicTemplate;
[ ... omitted for brevity]
In My Static Bean it autowires a Hibernate Session Factory:
@Component
public class StaticBean {
@Autowired
private SessionFactory hibernateSessionFactory;
[ ... omitted for brevity]
Now my understanding is that my AseCalculatorApplicationImplTestConfig will create a mock StaticBean, which is basically a Mockito empty shell, and not use any of the code / dependencies / beans from the class itself. And yet, I feel like I've tried every variation of creating my test beans, annotating them as @MockBean, annotating my Impl with @InjectMocks, and yet every time it fails with:
org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [org.hibernate.SessionFactory] found for dependency [org.hibernate.SessionFactory]
I know I could change my Impl class and have a constructor marked as @Autowired that then sets my staticBean, but I like how clean it is not to have to do that. But I can't figure out what I've done wrong with my Test Config creating my mock Beans - why is it trying to inject the hibernateSessionFactory when the bean is mocked??
Aucun commentaire:
Enregistrer un commentaire