이것저것 해보기🌼

springSecurityFilterChain cannot be null 에러 해결방법 본문

BE/Spring Boot

springSecurityFilterChain cannot be null 에러 해결방법

realtree 2024. 5. 7. 11:53

 

에러 로그

OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended

java.lang.IllegalStateException: springSecurityFilterChain cannot be null. Ensure a Bean with the name springSecurityFilterChain implementing Filter is present or inject the Filter to be used.

	at org.springframework.util.Assert.state(Assert.java:97)
	at org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurer.beforeMockMvcCreated(SecurityMockMvcConfigurer.java:76)
	at org.springframework.test.web.servlet.setup.AbstractMockMvcBuilder.build(AbstractMockMvcBuilder.java:176)
	at com.figstudy.web.BooksApiControllerTest.setup(BooksApiControllerTest.java:52)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)

 

 

에러 위치

@BeforeEach
    public void setup() {
        mvc = MockMvcBuilders
                .webAppContextSetup(context)
                .apply(springSecurity())
                .build();
    }

위 코드에서 build() 를 할때 springSecurityChain이 null이라서 에러가 발생한다.

 

 

 

 

 

참고 : https://sbs1621.tistory.com/54

 

[Spring] MockMvc를 이용한 Controller의 Test Code(JUnit5) 작성하기

간단하게 작성할 수 있는 게시판 Controller에 대한 테스트코드입니다. 테스트코드 추가하기 컨트롤러에서 cmd + shift + t(윈도우는 control + shift + t)를 누르시면 간단하게 테스트코드를 만들 수 있습

sbs1621.tistory.com

 

https://docs.spring.io/spring-security/site/docs/5.0.x/reference/html/test-mockmvc.html

 

12. Spring MVC Test Integration

There are a number of options available to associate a user to the current HttpServletRequest. For example, the following will run as a user (which does not need to exist) with the username "user", the password "password", and the role "ROLE_USER": You can

docs.spring.io