I am trying to test my spring security form. I've got a full annotation configuration in my project (take a look below). Now I am trying to set up a test case by SpringJUnit4ClassRunner and check some conditions in it. I am not able to do it, because @ContextConfiguration annotation does not see my context config. I use AbstractAnnotationConfigDispatcherServletInitializer to configure my project. Also see the code below.
This is the testing class:
@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")));
}
}
This is the config class:
@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[]{"/"};
}
}
And also i enclose the error log
SEVERE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@4157f54e] to prepare test instance [securityTests.test@7fa98a66] org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityTests.test': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.web.context.WebApplicationContext securityTests.test.context; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.web.context.WebApplicationContext] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
All my spring dependencies have the same version in pom.xml (spring 4.2.3.RELEASE). I don't want to create .xml file which will configure context for me. I want all to be done in java configuration. How do I do it? Thanks for help in advance
Add an @WebAppConfiguration
so that WebApplicationContext
will be available.
@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")));
}
}
I actually solved my problem myself. It turned out that in my pom.xml file i had a dependency to spring data 2.0.1.RELEASE which was somehow interrupting in the correct running of the test. The most probable reason was that spring data 2.0.1 was pulling some classes from other spring components which I added to pom.xml but mine were at version 4.2.1.RELEASE and those pulled by spring data were 5.0.0.RELEASE. So the override happened. Anyway, after deleting spring data dependency from pom.xml it all went good. Thanks for answers
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.