카테고리 없음

Spring boot 2.1 의@WebMvcTest

머룽 2023. 4. 23. 14:05
오늘 이야기할 내용은 Spring boot 2.1의 @WebMvcTest 어노테이션에 대해서 살펴볼텐데 기존의 @WebMvcTest과 조금 다른 부분이 있어 그것에 대해 알아보도록 하자. 만약 Spring boot 2.0 혹은 그 이전 버전에서 Spring boot 2.1로 버전을 올린다면 함께 봐야 할 수도 있다. 물론 그 상황이 라면? 필자도 Spring boot 2.0 에서 2.1로 올렸을 때 발생한 이슈였다. 2.0에서는 문제 없이 잘 실행 되었지만 2.1로 버전을 올렸더니 갑자기 테스트 케이스들이 실패하였다. 그래서 그 이유가 무엇인지 찾아보기 시작했다. 일단 어떤 경우에 이런 현상이 나오는지 코드로 살펴보도록 하자. 이 코드는 단지 예제일 뿐이다.
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()
            .authorizeRequests()
            .antMatchers("/hi").authenticated()
            .anyRequest()
            .permitAll()
            .and()
            .csrf()
            .disable();
    }

    @Bean
    public UserManager userManager(UserRepository userRepository) {
        return new UserManager(userRepository);
    }
}
위는 Security 설정이다. 딱히 문제 될 것은 없어 보인다. 기본적인 Security 설정이며 UserManager 경우에는 Security 와 관련있는 어떤 클래스라고 생각하면 되겠다. 그래서 위와 같이 설정을 했다고 하자.
@RestController
public class HelloController {

    @GetMapping("hi")
    public String hi() {
        return "hi";
    }
}


@RunWith(SpringRunner.class)
@WebMvcTest(HelloController.class)
@WithMockUser
public class HelloControllerTests {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void hi() throws Exception {
        mockMvc.perform(get("/hi")).andExpect(status().isOk())
            .andExpect(content().string("hi"))
            .andDo(print());
    }
}
그리고 나서 위와 같이 테스트 코드를 작성해보자. 딱히 문제는 없어 보인다. 테스트 코드를 돌려봐도 문제가 없다. 초록색 창이 뜨면서 테스트가 성공한 것을 볼 수 있을 것이다. 하지만 여기에서 버전을 2.1로 올려서 테스트를 한다면 어떻게 될까? 위의 코드가 2.1에서 잘 동작했다면 필자는 이 글을 쓸 이유가 없었을 것이다. Spring boot 버전을 2.1로 올리고 테스트를 해보면 다음과 같은 에러가 발생한다.
java.lang.IllegalStateException: Failed to load ApplicationContext

....

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type com.example.demosecurity.UserRepository available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1644)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1203)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1164)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:857)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:760)
stack trace 가 길어서 특정부분만 가져왔다. 갑자기 버전을 올리니 위와 같은 에러가 발생한다. 아까 위에서 설정한 UserManager의 디펜더시인 UserRepository타입의 빈을 찾을 수 없다는 내용이다. 그럼 왜 갑자기 이런 에러가 발생하는 걸까? 그 이유는 @WebMvcTest어노테이션으로 테스트할 때 기존에는 Spring boot의 기본 Config를 사용했다면 2.1부터는 WebSecurityConfigurer를 사용한 Config들도 같이 설정되어 실행된다. 그래서 위와 같이 UserRepository 라는 빈을 찾지 못한다고 에러가 발생된 것이다. 기존에는 설정해 놓은 프로젝트의 Security의 설정이 동작하는 것이 아니라 Spring boot 의 기본 설정이 동작해서 이 것이 정말 맞는 테스트인가? 의문도 생길수 있다. 아래의 코드를 살펴보자.
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()
            .authorizeRequests()
            .antMatchers("/admin/**").access("hasRole(ADMIN)")
            .anyRequest()
            .permitAll()
            .and()
            .csrf()
            .disable();
    }
}
기존의 코드를 위의 코드로 변경하고 테스트케이스를 만들어보자.
@RunWith(SpringRunner.class)
@WebMvcTest(AdminController.class)
public class AdminControllerTests {

    @Autowired
    private MockMvc mockMvc;

    @Test
    @WithMockUser(username = "example", roles = {"ANONYMOUS"})
    public void adminTestWithBadAuthentication() throws Exception {
        mockMvc.perform(get("/admin"))
            .andExpect(status().isForbidden());
    }

    @Test
    @WithMockUser(username = "user", roles = {"ADMIN"})
    public void adminTestWithAuthentication() throws Exception {
        mockMvc.perform(get("/admin"))
            .andExpect(status().isOk())
            .andExpect(content().string("admin"));
    }

}
위의 테스트 코드는 전혀 문제가 없어 보인다. adminTestWithBadAuthentication 메서드를 테스트 할 경우 role 이 ANONYMOUS 이니 403 에러가 떨어져야 정상이다. 왜냐하면 /admin/** 으로 들어올경우 role이 ADMIN 이어야 한다고 설정했으니 말이다.
.antMatchers("/admin/**").access("hasRole(ADMIN)")
하지만 Spring boot 2.0 혹은 그 이전버전에서는 위의 테스트 코드가 정상적으로 성공하지 않는다. adminTestWithAuthentication 메서드는 성공하나 adminTestWithBadAuthentication 메서드는 실패로 돌아간다.
java.lang.AssertionError: Status 
Expected :403
Actual   :200
우리는 http status code가 403을 기대 했는데 뜬금없이 정상적인 코드 200이 떨어졌다. 우리가 원하는 테스트가 아니다. 만약 2.1 이전 버전에서 위의 테스트 코드를 성공시키고 싶다면 아래와 같이 코드를 조금 변경해야 한다.
@RunWith(SpringRunner.class)
@WebMvcTest(AdminController.class)
@Import(WebSecurityConfig.class)
public class AdminControllerTests {
  //...
}
그러면 아무 문제 없이 테스트를 통과 할 수 있다. 하지만 Spring boot 2.1 부터는 위와 같이 설정하지 않아도 기본적으로 프로젝트의 Security 설정을 등록하므로 위와 같은 설정은 필요 없다. 오늘은 이렇게 Spring boot 2.1의 @WebMvcTest 변화에 대해서 알아봤다. 만약 Spring boot 2.1로 올리면서 테스트가 깨진다면 해당 이유일 수도 있으니 참고하면 되겠다. 해당 이슈의 토론은 여기에서 확인 할 수 있다. 원래 해당이슈는 2.0 때 개발되었다가 revert 되었다. 아마도 2.0이 나오기 바로 직전에 개발되어서 리스크가 크다고 판단되어 revert를 하고 2.1에 다시 개발되었다.