我是Spring Boot的新手。我正在使用TestRestTemplate运行Spring Boot测试。
在测试中成功获得对List<Member>
的请求。为了测试对Member的get请求抛出错误的请求400。我遵循与List<Member>
类似的模式,在Stackoverflow上阅读了许多答案,在这里我得到了List<Member>
的提示,它抛出了错误的请求。 HttpEntity
设置为JSON
。 Member
有id
。
找到以下用于设置标题的MemberController
,MemberControllerTest
和HttpEntity
方法
@GetMapping(path = "/api/member/{member-id}")
public ResponseEntity<Member> getMemberById(@PathVariable(name = "member-
id", required = true) Long memberId) {
Member member = memberService.findById(memberId);
if (member != null) {
return ResponseEntity.ok(member);
}
return ResponseEntity.notFound().build();
}
@Test
@Transactional
public void getMemberSuccessful() throws Exception {
HttpEntity<Object> member = getHttpEntity("{\"member-id\": \"2\"}");
ResponseEntity<Member> response =
template.exchange("/api/member/{member-id}", HttpMethod.GET, member,
Member.class, "member-id");
Assert.assertEquals(200, response.getStatusCode().value());
}
private HttpEntity<Object> getHttpEntity(Object body) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return new HttpEntity<Object>(body, headers);
}
预期结果是测试运行并返回状态码200,但出现此错误
java.lang.AssertionError:预期:<200>,但在:<400> org.junit.Assert.fail(Assert.java:88)在 org.junit.Assert.failNotEquals(Assert.java:834)在 org.junit.Assert.assertEquals(Assert.java:645)在 org.junit.Assert.assertEquals(Assert.java:631) com.thankgod.controller.MemberControllerTest.getMemberSuccessful(MemberControllerTest.java:151) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:498)在 org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 在 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 在 org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73) 在 org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83) 在 org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 在 org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75) 在 org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86) 在 org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84) 在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)处 org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251) 在 org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97) 在org.junit.runners.ParentRunner处$ 3.run(ParentRunner.java:290)在 org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)在 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)在 org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)在 org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)在 org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 在 org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) 在org.junit.runners.ParentRunner.run(ParentRunner.java:363)处 org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190) 在 org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89) 在 org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
答案 0 :(得分:1)
您应该在exchange()
方法的末尾添加urivariable:
template.exchange("/api/member/{member-id}", HttpMethod.GET, null,
Member.class, 2);
TestRestTemplate不会从HttpEntity
对象中读取