【问题标题】:How to implement junit test case for Configuration class?如何为配置类实现junit测试用例?
【发布时间】:2021-11-15 14:12:54
【问题描述】:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
 
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/actuator","/actuator/health").permitAll();
    }
}

上述类的测试用例如何实现??

【问题讨论】:

    标签: unit-testing junit spring-boot-actuator testcase health-check


    【解决方案1】:

    我可能会使用 RestAssured 框架编写一个集成测试并让它到达执行器端点。看起来像这样:

    @RunWith(SpringRunner.class)
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    public class IntegrationTests {
    
        @LocalServerPort
        private int port
    
        @Test
        public void shouldReturnSuccessWhenHittingActuator(){
            given()
                .port(port)
                .when()
                .get("/actuator")
                .then()
                .statusCode(HttpStatus.OK.value));
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-18
      • 1970-01-01
      • 2021-11-30
      • 1970-01-01
      • 2012-08-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多