스프링 보안 양식을 테스트하려고합니다. 내 프로젝트에 전체 주석 구성이 있습니다 (아래에서 살펴보십시오). 이제 SpringJUnit4ClassRunner으로 테스트 사례를 설정하고 테스트 조건을 확인하려고합니다. @ContextConfiguration 주석은 내 컨텍스트 구성을 보지 못하기 때문에이 작업을 수행 할 수 없습니다. 나는 AbstractAnnotationConfigDispatcherServletInitializer을 사용하여 프로젝트를 구성합니다. 또한 아래 코드를 참조하십시오.SpringJUnit4ClassRunner가 java 설정에 정의 된 컨텍스트에서 my를 보지 못했습니다.
@Configuration
public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer {
protected Class<?>[] getRootConfigClasses() {
return new Class[]{RootConfig.class, SecurityConfig.class};
}
protected Class<?>[] getServletConfigClasses() {
return new Class[]{WebConfig.class};
}
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
또한 내가 오류 로그를
심각한 묶 : TestExecutionListener을 허용하면서 잡은 예외를
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = WebInit.class) public class test { @Autowired private WebApplicationContext context; private MockMvc mockMvc; @Before public void setup() { mockMvc = MockMvcBuilders .webAppContextSetup(context) .apply(springSecurity()) .build(); } @Test public void someTest() throws Exception { mockMvc.perform(post("/").with(csrf().asHeader()).with(user("testuser").roles("USER"))); } }
이것은 설정 클래스는 다음과 같습니다
이
는 테스트 클래스입니다 테스트 인스턴스 준비 [org.springframewor [email protected]4157f54e] [email protected]] org.springframework.beans.factory.BeanCreationException : 이름이 'securityTests.test'인 bean을 생성하는 중 오류 발생 : 자동 종속 종속의 삽입이 실패했습니다. 중첩 예외는 org.springframework.beans.factory.BeanCreationException : 필드를 autowire 수 없습니다 : 개인 org.springframework.web.context.WebApplicationContext securityTests.test.context; 중첩 예외는 org.springframework.beans.factory.NoSuchBeanDefinitionException : [org.springframework.web.context.WebApplicationContext] 유형의 적격 bean이 종속성에 대해 발견되지 않았습니다.이 종속성에 대한 autowire 후보로 적합한 적어도 하나의 bean이 필요합니다. 종속성 주석 :
내 모든 스프링 의존성의 pom.xml (스프링 4.2.3.RELEASE)에서 동일한 버전이 {org.springframework.beans.factory.annotation.Autowired @는 (= TRUE 필수)}. 나를위한 컨텍스트를 구성 할 .xml 파일을 만들고 싶지 않습니다. 모든 자바 구성을 완료하고 싶습니다. 어떻게해야합니까? 사전에 도움을 주셔서 감사합니다