【问题标题】:Spring Actuator and @DataJpaTest cannot find CounterService beanSpring Actuator 和 @DataJpaTest 找不到 CounterService bean
【发布时间】:2017-08-04 15:50:32
【问题描述】:

我刚刚在我的 Spring Boot 项目中设置了 Spring Actuator,但是现在当我运行我的 Jpa 单元测试时,spring 抱怨

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.actuate.metrics.CounterService'

因为找不到

@Resource
CounterService counterService;

这是我的测试课

@RunWith(SpringRunner.class)
@SpringBootTest
@DataJpaTest
@ActiveProfiles("dev")
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class MyTestClass {

【问题讨论】:

  • 将主类包含到@SpringBootTest(classes=MainApplication.java) 或其他要为此测试加载的类。
  • 是的,这行得通
  • @SpringBootTest@DataJpaTest 在一个类上没有多大意义不是吗?

标签: spring spring-boot junit actuator


【解决方案1】:

将主类包含到@SpringBootTest(classes=MainApplication.java) 或其他要为此测试加载的类

@RunWith(SpringRunner.class)
@SpringBootTest(classes=MainApplication.java)
@DataJpaTest
@ActiveProfiles("dev")
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class MyTestClass {

【讨论】:

    猜你喜欢
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    • 2019-09-16
    • 2018-12-13
    相关资源
    最近更新 更多