Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Spring MVC Test gives 404 if @EnableMetrics #198

Open
karayv opened this issue Mar 10, 2017 · 4 comments
Open

Spring MVC Test gives 404 if @EnableMetrics #198

karayv opened this issue Mar 10, 2017 · 4 comments

Comments

@karayv
Copy link

karayv commented Mar 10, 2017

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
Copy link

dennisosimon commented Apr 13, 2017

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

@helospark
Copy link

Try using:
@EnableMetrics(proxyTargetClass = true)

@sripadapavan
Copy link

@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
Copy link

@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?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants