简体   繁体   English

使用MockMvc测试Spring

[英]Testing Spring with MockMvc

please could help to me with Spring testing with MockMvc. 请使用MockMvc对我进行春季测试,对我有帮助。

I have Controller(I deleted path code of metod) 我有控制器(我删除了方法的路径代码)

@Controller
@RequestMapping("/read/object-attributes")
public class GroupAttributeReadController {

@Autowired
private GroupAttributeService groupAttributeService;
@RequestMapping(value = "/import", method = RequestMethod.GET)
public
@ResponseBody
GroupAttributeBufferListResponse findAll(@RequestParam(value = "pageNum", required = true) int pageNum,
                                     @RequestParam(value = "pageSize", required = true) int pageSize,
                                     @RequestParam(value = "order", required = false) String order,
                                     @RequestParam(value = "orderDir", required = false) String orderDir,
                                     @RequestParam(value = "loadSession") Long loadSession,
                                     @RequestParam( value = "showCorrect", defaultValue = "0") Integer showCorrect,
                                     @RequestParam(value="naviUser") String user,
                                     @RequestParam Map<String, String > params,
                                     HttpServletResponse response, Locale locale) {

}

And my test 而我的测试

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class ControllersTest {
@Autowired
WebApplicationContext wac;

MockMvc mockMvc;

@Before
public void setup() {
    DefaultMockMvcBuilder builder = MockMvcBuilders.webAppContextSetup(this.wac);
    this.mockMvc = builder.build();
}

@Test
public void testController() throws Exception {
    ResultMatcher ok = MockMvcResultMatchers.status().isOk();

    MockHttpServletRequestBuilder request = MockMvcRequestBuilders.get("/read/object-attributes/import?pageNum=2&pageSize=5&order=test&orderDir=DESC&loadSession=1&showCorrect=0&naviUser=user&FILTER_Test=Test");
    this.mockMvc.perform(request)
            .andExpect(ok);
    }
}

But I'm not understand why response 404. Maybe I something forget? 但我不明白为什么会回应404。也许我有些忘记了? Maybe need config-file, I dont know :( 也许需要配置文件,我不知道:(

java.lang.AssertionError: Status 
Expected :200
Actual   :404
<Click to see difference>


at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:54)
at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:81)
at org.springframework.test.web.servlet.result.StatusResultMatchers$10.match(StatusResultMatchers.java:665)
at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:171)
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:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
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:191)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

You need to provide the required params. 您需要提供所需的参数。

Example: 例:

@Test public void testAddInformation() throws Exception { this.mockMvc.perform(post("/sample").param("name", "provideName").param("address", "provideAddress")).andExpect(status().isOk()); };

You should include @ContextConfiguration as well - here is a snippet from @WebAppConfiguration documentation : 您还应该包括@ContextConfiguration这是@WebAppConfiguration文档的摘录:

Note that @WebAppConfiguration must be used in conjunction with @ContextConfiguration , either within a single test class or within a test class hierarchy. 请注意, @WebAppConfiguration必须在单个测试类中或在测试类层次结构中与@ContextConfiguration结合使用。

So your test class should look something like that: 因此您的测试类应如下所示:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration( classes = AppConfig.class, WebConfig.class )
public class ControllersTest {
   (...)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM