1

期間不可用我有一個Web應用程序,包括它的上下文路徑/管理,只與角色通過Spring安全集成「管理員」用戶可用下暴露驅動器。實際上,應用程序確實按預期工作。但是,我正在努力測試網​​絡應用程序,即使用spring boot的設施編寫集成測試。春天啓動的操作端點測試

運行下面的測試,加載彈簧上下文,我注意到執行器端點沒有註冊。

@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(); 
    } 
} 

日誌文件片段

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 

相關從application.properties片段如下所示。

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 

可有人請幫助我,讓我知道我在做什麼錯在這裏,如何解決拒絕豆的名字的問題着手未來的日子?

在我的情況下,彈簧啓動和執行器的版本是1.5.2.RELEASE

回答

0

如何在pom中添加執行器依賴項?你是否在依賴中添加了任何作用域? 如果您已添加如下所示,請刪除編輯或將其更改爲測試

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