Search code examples
springspring-bootjunitspring-boot-actuator

Spring Actuator and @DataJpaTest cannot find CounterService bean


I have just setup Spring Actuator in my Spring boot project but now when I run my Jpa unit test spring complains

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

Because it cannot find the

@Resource
CounterService counterService;

This my test class

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

Solution

  • Include the main class to @SpringBootTest(classes=MainApplication.java) or other classes to load for this test

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