简体   繁体   中英

How to implement junit test case for Configuration class?

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
 
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/actuator","/actuator/health").permitAll();
    }
}

How to implement the test case for the above class??

I would probably write an integration test using the RestAssured framework and have it hit the actuator endpoint. Would look something like this:

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

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