Spring引导的新手正在运行集成测试。使用TestRestTemplate
的交换方法,我无法将LocalDateTime请求参数传递给测试方法。这是我的MemberController
方法和MemberControllerTest
方法。我想这与日期和格式有关,因为当我测试id为整数的get成员时,相同的交换方法有效。
@Test
@Transactional
public void getTopMembersss() throws Exception {
LocalDateTime startTime = LocalDateTime.of(2015, Month.JULY, 29, 19, 30, 40);
LocalDateTime endTime = LocalDateTime.of(2019, Month.JANUARY, 01, 19, 30, 40);
ResponseEntity<List<TopMemberDTO>> response = template.exchange(
"/api/member/top-member?startTime={startTime}&endTime={endTime}", HttpMethod.GET, null,
new ParameterizedTypeReference<List<TopMemberDTO>>() {
}, startTime.toString(), endTime.toString());
// , "2018-12-31T12:12:12", "2018-12-31T12:12:12"
Assert.assertEquals(200, response.getStatusCode().value());
}
@GetMapping(path = "/api/member/top-member?startTime={startTime}&endTime={endTime}")
public ResponseEntity<List<TopMemberDTO>> getTopMembers(
@RequestParam(value = "startTime", required = true) @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss") LocalDateTime startTime,
@RequestParam(value = "endTime", required = true) @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss") LocalDateTime endTime) {
List<TopMemberDTO> topDrivers = new ArrayList<>();
/**
* Your Implementation Here.
*
*/
topDrivers = memberService.findTopMembersCount(startTime, endTime);
if (topDrivers.isEmpty()) {
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok(topDrivers);
}
}
java.lang.AssertionError: expected:<200> but was:<400>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:834)
at org.junit.Assert.assertEquals(Assert.java:645)
at org.junit.Assert.assertEquals(Assert.java:631)
at com.thankgod.controller.MemberControllerTest.getTopMembersss(MemberControllerTest.java:137)
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.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
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:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
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:190)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
答案 0 :(得分:1)
将@GetMapping(path = "/api/member/top-member?startTime={startTime}&endTime={endTime}")
更改为@GetMapping(path = "/api/member/top-member")