gpt4 book ai didi

java - 使用 MockMvc 测试 Spring

转载 作者:行者123 更新时间:2023-12-02 12:34:20 24 4
gpt4 key购买 nike

请帮助我使用 MockMvc 进行 Spring 测试。

我有 Controller (我删除了方法的路径代码)

@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) {

}

还有我的测试

@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);
}
}

但我不明白为什么响应 404。也许我忘记了什么?也许需要配置文件,我不知道:(

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)

最佳答案

您需要提供所需的参数。

示例:

@测试
公共(public)无效testAddInformation()抛出异常{
this.mockMvc.perform(post("/sample").param("name", "provideName").param("address", "provideAddress")).andExpect(status().isOk());
};

关于java - 使用 MockMvc 测试 Spring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45190068/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com