单元测试调用controller层,一直报参数匹配不上
单元测试调用controller层,一直报参数匹配不上@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = KekeProjectApplication.class,webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//@WebAppConfiguration
@AutoConfigureMockMvc
public class SaleOrderTest {
@Resource
private ISaleOrderService saleOrderService;
@Resource
private WebApplicationContext context;
@Resource
private MockMvc mockMvc;
@Test
public void getInfo() throws Exception {
int id = 1856;
MockHttpServletRequestBuilder getRequestBuilder = get("/project/saleOrder"); //路径
getRequestBuilder.contentType(MediaType.APPLICATION_FORM_URLENCODED);
getRequestBuilder.param("id",String.valueOf(id)); //参数
getRequestBuilder.header("token","f7379090-8ff3-4dd4-a301-35fa309e15eb");
ResultActions resultActions = mockMvc.perform(getRequestBuilder);
resultActions.andExpect(MockMvcResultMatchers.status().isOk());//状态码是否相等
resultActions.andDo(MockMvcResultHandlers.print()); //输出信息
MvcResult response = resultActions.andReturn();
System.out.println("result:" + response.getResponse().getContentAsString());
}运行结果及报错内容
2022-07-26 15:45:44.469INFO 17828 --- com.netflix.discovery.DiscoveryClient : DiscoveryClient_KEKE-PROVIDER-PROJECT/keke-provider-project:192.168.101.64:0 - registration status: 204
2022-07-26 15:45:44.516INFO 17828 --- [ main] c.h.core.interceptor.AccessFilter : contentType=application/x-www-form-urlencoded
/project/saleOrder
(LoginInterceptor.java:51) INFOcom.huawucloud.core.interceptor.LoginInterceptor
当前用户token为:f7379090-8ff3-4dd4-a301-35fa309e15eb
2022-07-26 15:45:44.766WARN 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"
MockHttpServletRequest:
HTTP Method = GET
Request URI = /project/saleOrder
Parameters = {id=}
Headers = {Content-Type=, token=}
Handler:
Type = com.huawucloud.project.controller.ProjectController
Method = public com.huawucloud.core.utils.JsonRet com.huawucloud.project.controller.ProjectController.getInfo(java.lang.Integer)
Async:
Async started = false
Async result = null
Resolved Exception:
Type = org.springframework.web.method.annotation.MethodArgumentTypeMismatchException
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 400
Error message = null
Headers = {X-Application-Context=}
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies = []
java.lang.AssertionError: Status
Expected :200
Actual :400
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:164)
at com.huawucloud.test.SaleOrderTest.getInfo(SaleOrderTest.java:141)
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.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:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)将接口接收参数改为String类型,还是一样的报错
能够成功调用接口
下断点看看哪里报错的 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" 打断点,看下输出 单步调试
页:
[1]