51Testing软件测试论坛

标题: 单元测试调用controller层,一直报参数匹配不上 [打印本页]

作者: 测试积点老人    时间: 2022-7-27 13:11
标题: 单元测试调用controller层,一直报参数匹配不上
单元测试调用controller层,一直报参数匹配不上
  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @SpringBootTest(classes = KekeProjectApplication.class,webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  3. //@WebAppConfiguration
  4. @AutoConfigureMockMvc
  5. public class SaleOrderTest {
  6.     @Resource
  7.     private ISaleOrderService saleOrderService;

  8.     @Resource
  9.     private WebApplicationContext context;

  10.     @Resource
  11.     private MockMvc mockMvc;

  12.     @Test
  13.     public void getInfo() throws Exception {
  14.         int id = 1856;
  15.         MockHttpServletRequestBuilder getRequestBuilder = get("/project/saleOrder");     //路径
  16.         getRequestBuilder.contentType(MediaType.APPLICATION_FORM_URLENCODED);
  17.         getRequestBuilder.param("id",String.valueOf(id));         //参数
  18.         getRequestBuilder.header("token","f7379090-8ff3-4dd4-a301-35fa309e15eb");

  19.         ResultActions resultActions = mockMvc.perform(getRequestBuilder);
  20.         resultActions.andExpect(MockMvcResultMatchers.status().isOk());  //状态码是否相等
  21.         resultActions.andDo(MockMvcResultHandlers.print());                //输出信息
  22.         MvcResult response = resultActions.andReturn();
  23.         System.out.println("result:" + response.getResponse().getContentAsString());
  24. }
复制代码
运行结果及报错内容
  1. 2022-07-26 15:45:44.469  INFO [keke-provider-project,,,,] 17828 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_KEKE-PROVIDER-PROJECT/keke-provider-project:192.168.101.64:0 - registration status: 204
  2. 2022-07-26 15:45:44.516  INFO [keke-provider-project,,,,] 17828 --- [           main] c.h.core.interceptor.AccessFilter        : contentType=application/x-www-form-urlencoded
  3. /project/saleOrder
  4. [2022-07-26 15:45:44,547] [main] (LoginInterceptor.java:51) INFO  com.huawucloud.core.interceptor.LoginInterceptor
  5.      当前用户token为:f7379090-8ff3-4dd4-a301-35fa309e15eb
  6. 2022-07-26 15:45:44.766  WARN [keke-provider-project,,,,] 17828 --- [           main] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to bind request element: org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; nested exception is java.lang.NumberFormatException: For input string: "saleOrder"

  7. MockHttpServletRequest:
  8.       HTTP Method = GET
  9.       Request URI = /project/saleOrder
  10.        Parameters = {id=[1856]}
  11.           Headers = {Content-Type=[application/x-www-form-urlencoded;charset=UTF-8], token=[f7379090-8ff3-4dd4-a301-35fa309e15eb]}

  12. Handler:
  13.              Type = com.huawucloud.project.controller.ProjectController
  14.            Method = public com.huawucloud.core.utils.JsonRet com.huawucloud.project.controller.ProjectController.getInfo(java.lang.Integer)

  15. Async:
  16.     Async started = false
  17.      Async result = null

  18. Resolved Exception:
  19.              Type = org.springframework.web.method.annotation.MethodArgumentTypeMismatchException

  20. ModelAndView:
  21.         View name = null
  22.              View = null
  23.             Model = null

  24. FlashMap:
  25.        Attributes = null

  26. MockHttpServletResponse:
  27.            Status = 400
  28.     Error message = null
  29.           Headers = {X-Application-Context=[keke-provider-project:dev:0]}
  30.      Content type = null
  31.              Body =
  32.     Forwarded URL = null
  33.    Redirected URL = null
  34.           Cookies = []

  35. java.lang.AssertionError: Status
  36. Expected :200
  37. Actual   :400
  38. to see difference>
  39.     at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:54)
  40.     at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:81)
  41.     at org.springframework.test.web.servlet.result.StatusResultMatchers$10.match(StatusResultMatchers.java:665)
  42.     at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:164)
  43.     at com.huawucloud.test.SaleOrderTest.getInfo(SaleOrderTest.java:141)
  44.     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  45.     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  46.     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  47.     at java.lang.reflect.Method.invoke(Method.java:498)
  48.     at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
  49.     at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  50.     at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
  51.     at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
  52.     at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
  53.     at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
  54.     at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
  55.     at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
  56.     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
  57.     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
  58.     at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
  59.     at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
  60.     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
  61.     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
  62.     at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
  63.     at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
  64.     at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
  65.     at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
  66.     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
  67.     at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
  68.     at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
  69.     at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
  70.     at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
  71.     at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
  72.     at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
  73.     at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
复制代码
将接口接收参数改为String类型,还是一样的报错
能够成功调用接口

作者: qqq911    时间: 2022-7-28 10:48
下断点看看哪里报错的
作者: 郭小贱    时间: 2022-7-28 13:45
Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; nested exception is java.lang.NumberFormatException: For input string: "saleOrder"
作者: bellas    时间: 2022-7-28 14:00
打断点,看下输出
作者: jingzizx    时间: 2022-7-28 17:06
单步调试




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2