2017-12-01 13 views
0

스프링 보안 양식을 테스트하려고합니다. 내 프로젝트에 전체 주석 구성이 있습니다 (아래에서 살펴보십시오). 이제 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 파일을 만들고 싶지 않습니다. 모든 자바 구성을 완료하고 싶습니다. 어떻게해야합니까? 사전에 도움을 주셔서 감사합니다

답변

0

나는 실제로 내 문제를 직접 해결했습니다. pom.xml 파일에서 스프링 데이터 2.0.1.RELEASE에 대한 의존성이 있었는데, 이는 테스트의 올바른 실행을 방해하고있었습니다. 가장 가능성있는 이유는 스프링 데이터 2.0.1이 pom.xml에 추가 한 다른 스프링 구성 요소의 일부 클래스를 가져 오는 것이지만 4.2.1.RELEASE 및 스프링 데이터로 가져온 것들은 5.0.0.RELEASE였습니다. 그래서 오버라이드가 발생했습니다. 어쨌든, pom.xml에서 스프링 데이터 의존성을 삭제 한 후에 모두 잘되었다. 답변 해 주셔서 감사합니다.

0

@WebAppConfiguration을 추가하면 WebApplicationContext을 사용할 수 있습니다.

@RunWith(SpringJUnit4ClassRunner.class) 
@WebAppConfiguration 
@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"))); 
    } 
}