我试图通过Junit测试用例调用我的ajax方法。我已经提到卡车装载的SO问题来达到这一点。但我还是得到了
TestMockMVC does not declare any static, non-private, non-fin
al, inner classes annotated with @Configuration.
我的AJAX电话是
@RequestMapping(value = { "/saveEntityAjax", "/modifyEntityAjax" }, method = RequestMethod.POST)
public @ResponseBody String saveOrUpdateEntityAjax(
@RequestParam(value = "id") String id,
@RequestParam(value = "number") String number, HttpServletRequest httpServletRequest) {
我的Junit测试案例是
package test.controllers;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.AnnotationConfigWebContextLoader;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import com.mnox.database.pojo.wrapper.v2.VehicleMasterPojoWrapper.VehiclePurpose;
@Configuration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader=AnnotationConfigWebContextLoader.class)
@WebAppConfiguration
public class TestMockMVC {
@Autowired
private WebApplicationContext context;
private MockMvc mvc;
@Before
public void setup() {
mvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).build();
}
@Test
public void test1CreateClient() {
SaveOrUpdateVehicleAjaxRequest vehicle = new SaveOrUpdateVehicleAjaxRequest("123", "KA-02-1234", 12,
VehiclePurpose.MAIN_VEHICLE.name(), "some alias");
try {
MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post("/saveVehicleAjaxMethod")
.contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)).andReturn();
mvcResult.getModelAndView();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private class SaveOrUpdateEntityAjaxRequest {
private String id;
private String number;
public SaveOrUpdateEntityAjaxRequest(String id, String number) {
super();
this.id = id;
this.number = number;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
}
我收到以下错误
INFO : org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [test.controllers.TestMockMVC]: TestMockMVC does not declare any static, non-private, non-final, inner classes annotated with @Configuration.
INFO : org.springframework.test.context.web.WebTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
INFO : org.springframework.test.context.web.WebTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@7f3b84b8, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@57a3af25, org.springframework.test.context.support.DirtiesContextTestExecutionListener@2b662a77, org.springframework.test.context.transaction.TransactionalTestExecutionListener@7f0eb4b4, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@5c33f1a9, org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener@1623b78d, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@c8c12ac, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@6adbc9d]
INFO : org.springframework.web.context.support.GenericWebApplicationContext - Refreshing org.springframework.web.context.support.GenericWebApplicationContext@3eb25e1a: startup date [Mon Sep 11 13:11:05 IST 2017]; root of context hierarchy
INFO : org.springframework.web.context.support.GenericWebApplicationContext - Closing org.springframework.web.context.support.GenericWebApplicationContext@3eb25e1a: startup date [Mon Sep 11 13:11:05 IST 2017]; root of context hierarchy
编辑:我正在使用的是
spring-boot-test-1.4.0.RELEASE.jar
spring-security-test-4.0.0.RELEASE.jar
spring-context-3.1.0.RELEASE.jar
spring-test-4.1.9.RELEASE.jar
spring-core-3.1.0.RELEASE.jar
spring-web-3.1.0.RELEASE.jar
spring-expression-3.1.0.RELEASE.jar
spring-webmvc-3.1.0.RELEASE.jar
编辑:我的文件夹结构是
src/main/java/test/controllers/TestMockMVC.java
src/main/webapp/WEB-INF/web.xml
src/main/webapp/WEB-INF/spring/root-context.xml
src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml
修改: 我有一个部分答案。我修改了我的代码以解决此问题中报告的问题。虽然我遇到了不同的问题,但我部分地回答了这个问题。
更改1
@Configuration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({
// "file:src/main/webapp/WEB-INF/web.xml"
"file:src/main/webapp/WEB-INF/spring/root-context.xml",
"file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml" })
@WebAppConfiguration
public class TestMockMVC {
@Autowired
FilterChainProxy springSecurityFilterChain;
@Autowired
private WebApplicationContext context;
private MockMvc mvc;
@Before
public void setup() {
mvc = MockMvcBuilders.webAppContextSetup(context)
.apply(SecurityMockMvcConfigurers.springSecurity(springSecurityFilterChain)).build();
}
@Test
public void test1CreateClient() {
SaveOrUpdateEntityAjaxRequest vehicle = new SaveOrUpdateEntityAjaxRequest("123", "KA-02-1234");
MvcResult mvcResult = null;
try {
MockHttpServletRequestBuilder request = MockMvcRequestBuilders.post("/saveEntityAjaxMethod")
.content(new Gson().toJson(vehicle).getBytes()).contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON);
mvcResult = mvc.perform(request).andReturn();
mvcResult.getModelAndView();
} catch (Exception e) {
e.printStackTrace();
}
}
更改2
在我的servlet-context.xml中,我添加了以下行。
<bean id="springSecurityFilterChain" class="org.springframework.security.web.FilterChainProxy">
<!-- properties -->
</bean>
我现在得到的例外
INFO : org.springframework.test.web.servlet.TestDispatcherServlet - FrameworkServlet '': initialization started
INFO : org.springframework.test.web.servlet.TestDispatcherServlet - FrameworkServlet '': initialization completed in 67 ms
java.lang.NullPointerException
at org.springframework.security.web.FilterChainProxy.getFilters(FilterChainProxy.java:223)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:269)
at org.springframework.test.util.ReflectionTestUtils.invokeMethod(ReflectionTestUtils.java:307)
at org.springframework.security.test.web.support.WebTestUtils.findFilter(WebTestUtils.java:118)
at org.springframework.security.test.web.support.WebTestUtils.getSecurityContextRepository(WebTestUtils.java:57)
at org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors$SecurityContextRequestPostProcessorSupport.save(SecurityMockMvcRequestPostProcessors.java:434)
at org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors$TestSecurityContextHolderPostProcessor.postProcessRequest(SecurityMockMvcRequestPostProcessors.java:511)
at org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder.postProcessRequest(MockHttpServletRequestBuilder.java:686)
at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:137)
at test.controllers.TestMockMVC.test1CreateClient(TestMockMVC.java:61)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:70)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:224)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)