ryantenney / metrics-spring

Spring integration for Metrics
http://www.ryantenney.com/metrics-spring/
Apache License 2.0
702 stars 227 forks source link

Spring MVC Test gives 404 if @EnableMetrics #198

Open karayv opened 7 years ago

karayv commented 7 years ago

Having @EnableMetrics I get 404 on Controller tests. Commenting out @EnableMetrics brings everything back to normal.

Full test to reproduce the issue below (notice if TestController does not implement anything, test passes):

@RunWith(SpringRunner.class)
@WebMvcTest({ TestController.class, MetricsConfiguration.class })
@ActiveProfiles("test")
public class SpringMetricsTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void test() throws Exception {
        mockMvc.perform(get("/my-service")).andExpect(status().isOk());
    }

    @RestController
    // no implements, no test failure
    public static class TestController implements TestIfc {

        @RequestMapping(value = "/my-service", method = GET)
        @Timed
        public String getMe() {
            return "Yeah!";
        }

    }

    public static interface TestIfc {
        // without this interface being implemented we get 200
    }

    @Configuration
    @EnableMetrics
    public static class MetricsConfig extends MetricsConfigurerAdapter {
    }

}
dennisosimon commented 7 years ago

Same issue for me, someone knows how to fix it?

helospark commented 7 years ago

Try using: @EnableMetrics(proxyTargetClass = true)

sripadapavan commented 7 years ago

@helospark Tried including "proxyTargetClass" the the issue still persists and my Test Cases are failing with below exception:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.ryantenney.metrics.spring.config.annotation.DelegatingMetricsConfiguration': Unsatisfied dependency expressed through method 'setMetricsConfigurers' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'metricsConfig': Unsatisfied dependency expressed through field 'systemPublicMetrics'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.actuate.endpoint.SystemPublicMetrics' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

helospark commented 7 years ago

@sripadapavan I have tried the test cases in the question in a Spring Boot application, they were passing.

Based on the error message, it seems like you have a systemPublicMetrics in your metricsConfig class that connot be autowired (have you added that, or does that come from the superclass? If you added manually, can you provide full example?)

This indicated that for whatever reason PublicMetricsAutoConfiguration is not invoked while autoconfiguring the context.

You can define such a bean by adding to your test @MockBean SystemPublicMetrics mockSystemPublicMetrics; If you really need the real bean you can manually invoke the autoconfiguration by adding this to your IT test class: @Import(PublicMetricsAutoConfiguration.class)

It might also worth a look, on why this autoconfiguration is not invoked.

Before adding targetProxyClass=true have you also experienced 404 error on controller and no bean creation exception?