简体   繁体   中英

Spring boot's actuator end points are unavailable during test

I've a web application that includes actuator which is exposed under the context path /manage and made available only to users with role ' administrators ' via spring security integration. In fact, the application does work as intended. However, I'm struggling to test the web-application ie write integration tests using spring boot's facilities.

Running a test like one below, that loads the spring context, I noticed that the actuator end points weren't registered.

@RunWith(SpringRunner.class)
@DirtiesContext
@SpringBootTest
@AutoConfigureTestDatabase(replace= AutoConfigureTestDatabase.Replace.AUTO_CONFIGURED)
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class, WithSecurityContextTestExecutionListener.class})
@WebAppConfiguration
@Slf4j
public class ActuatorControllerTest {

  private MediaType contentTypeJSON = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), StandardCharsets.UTF_8);

  protected MockMvc mockMvc;

  @Autowired
  private WebApplicationContext webApplicationContext;

  @Before
  public void setUp() throws Exception {
    this.webApplicationContext.getBean(HealthEndpoint.class).setEnabled(true);
    this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
  }

  @Test
  @WithMockUser(username="test",roles={"developers"})
  public void user_without_role_administrator_should_not_be_able_to_access_actuator() throws Exception {
    mockMvc.perform(get("/manage/health").with(csrf()))
        .andExpect(status().isForbidden());
  }

  @Test
  @WithMockUser(username="admin",roles={"administrator"})
  public void user_with_role_administrator_should_be_able_to_access_actuator() throws Exception {
    mockMvc.perform(get("/manage/health")
        .with(csrf())
        // I also tried the following approach which miserably failed 
        // .with(user(buildUserDetails()))
    ).andExpect(status().isOk());
  }


  private UserDetails buildUserDetails() {
    return User.withUsername("admin").password("admin").roles("administrators").build();
  }
}

Log file snippet

2017-06-19 09:09:35.966 DEBUG 986 --- [           main] o.s.w.s.h.BeanNameUrlHandlerMapping      : Rejected bean name 'environmentEndpoint': no URL paths identified
2017-06-19 09:09:35.966 DEBUG 986 --- [           main] o.s.w.s.h.BeanNameUrlHandlerMapping      : Rejected bean name 'healthEndpoint': no URL paths identified
2017-06-19 09:09:35.966 DEBUG 986 --- [           main] o.s.w.s.h.BeanNameUrlHandlerMapping      : Rejected bean name 'beansEndpoint': no URL paths identified
2017-06-19 09:09:35.966 DEBUG 986 --- [           main] o.s.w.s.h.BeanNameUrlHandlerMapping      : Rejected bean name 'infoEndpoint': no URL paths identified
2017-06-19 09:09:35.966 DEBUG 986 --- [           main] o.s.w.s.h.BeanNameUrlHandlerMapping      : Rejected bean name 'loggersEndpoint': no URL paths identified
2017-06-19 09:09:35.966 DEBUG 986 --- [           main] o.s.w.s.h.BeanNameUrlHandlerMapping      : Rejected bean name 'metricsEndpoint': no URL paths identified
2017-06-19 09:09:35.966 DEBUG 986 --- [           main] o.s.w.s.h.BeanNameUrlHandlerMapping      : Rejected bean name 'traceEndpoint': no URL paths identified
2017-06-19 09:09:35.966 DEBUG 986 --- [           main] o.s.w.s.h.BeanNameUrlHandlerMapping      : Rejected bean name 'dumpEndpoint': no URL paths identified
2017-06-19 09:09:35.966 DEBUG 986 --- [           main] o.s.w.s.h.BeanNameUrlHandlerMapping      : Rejected bean name 'autoConfigurationReportEndpoint': no URL paths identified
2017-06-19 09:09:35.966 DEBUG 986 --- [           main] o.s.w.s.h.BeanNameUrlHandlerMapping      : Rejected bean name 'shutdownEndpoint': no URL paths identified
2017-06-19 09:09:35.966 DEBUG 986 --- [           main] o.s.w.s.h.BeanNameUrlHandlerMapping      : Rejected bean name 'configurationPropertiesReportEndpoint': no URL paths identified
2017-06-19 09:09:35.966 DEBUG 986 --- [           main] o.s.w.s.h.BeanNameUrlHandlerMapping      : Rejected bean name 'endpoints-org.springframework.boot.actuate.endpoint.EndpointProperties': no URL paths identified
2017-06-19 09:09:35.966 DEBUG 986 --- [           main] o.s.w.s.h.BeanNameUrlHandlerMapping      : Rejected bean name 'org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration': no URL paths identified

Relevant snippet from application.properties are shown below.

management.port=8080
management.context-path=/manage
management.security.enabled=false
endpoints.health.sensitive=false
management.health.db.enabled=false
management.health.defaults.enabled=true
management.health.diskspace.enabled=true
management.security.roles=administrators

Can someone please help me and let me know what I'm doing wrong here and how to solve the rejected bean-name issue to proceed ahead?

In my case, spring boot and actuator's version is 1.5.2.RELEASE .

How you added the actuator dependency in pom? do you added any scope in dependency? If you have added like below, remove the compile or change it to test

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <scope>compile</scope>
</dependency>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM