51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 498|回复: 4
打印 上一主题 下一主题

单元测试调用controller层,一直报参数匹配不上

[复制链接]
  • TA的每日心情
    无聊
    4 天前
  • 签到天数: 530 天

    连续签到: 2 天

    [LV.9]测试副司令

    跳转到指定楼层
    1#
    发表于 2022-7-27 13:11:11 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    1测试积点
    单元测试调用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类型,还是一样的报错
    能够成功调用接口

    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

  • TA的每日心情
    慵懒
    3 天前
  • 签到天数: 1521 天

    连续签到: 5 天

    [LV.Master]测试大本营

    2#
    发表于 2022-7-28 10:48:31 | 只看该作者
    下断点看看哪里报错的
    回复

    使用道具 举报

  • TA的每日心情
    奋斗
    2024-10-22 14:23
  • 签到天数: 1007 天

    连续签到: 1 天

    [LV.10]测试总司令

    3#
    发表于 2022-7-28 13:45:21 | 只看该作者
    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"
    回复

    使用道具 举报

  • TA的每日心情
    奋斗
    昨天 10:15
  • 签到天数: 756 天

    连续签到: 1 天

    [LV.10]测试总司令

    4#
    发表于 2022-7-28 14:00:23 | 只看该作者
    打断点,看下输出
    回复

    使用道具 举报

  • TA的每日心情
    奋斗
    前天 07:50
  • 签到天数: 2818 天

    连续签到: 6 天

    [LV.Master]测试大本营

    5#
    发表于 2022-7-28 17:06:57 | 只看该作者
    单步调试
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-11-25 00:34 , Processed in 0.062885 second(s), 21 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表