这是我的测试类:

@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();
    }
}

我的AseCalculatorApplicationImpl类具有自动装配的bean(没有构造函数或setter,通过refelection自动装配):

@Component
public class AseCalculatorApplicationImpl {

    @Autowired
    StaticBean staticBean;

    @Autowired
    JmsTemplate jmsTopicTemplate;

    [ ... omitted for brevity]

在My Static Bean中,它自动装配一个Hibernate会话工厂:

@Component
public class StaticBean {

    @Autowired
    private SessionFactory hibernateSessionFactory;

    [ ... omitted for brevity]

现在我的理解是我的AseCalculatorApplicationImplTestConfig将创建一个模拟StaticBean,它基本上是一个Mockito空shell,而不是使用类本身的任何代码/依赖/ bean . 然而,我觉得我已经尝试了创建测试bean的每个变体,将它们注释为@MockBean,使用@InjectMocks注释我的Impl,但每次失败时:

org.springframework.beans.factory.NoSuchBeanDefinitionException: 
  No qualifying bean of type [org.hibernate.SessionFactory] found for dependency [org.hibernate.SessionFactory]

我知道我可以改变我的Impl类并且有一个标记为@Autowired的构造函数然后设置我的staticBean,但我喜欢它是多么干净,不必这样做 . 但是我无法弄清楚我的Test Config创建我的模拟Bean我做错了什么 - 为什么在bean被模拟时尝试注入hibernateSessionFactory?