@WebfluxTest失败了子资源,例如帖子/帖子ID /评论

时间:2018-01-06 08:11:58

标签: spring spring-test project-reactor spring-webflux spring-boot-test

当我使用@WebFluxTest在模拟环境中测试我的控制器时,尝试测试Comment又名 / posts / 1 / comments Post子结果,失败了。但 / posts 相关端点的测试有效。

PostController代码如下所示,完整代码为here

@RestController()
@RequestMapping(value = "/posts")
class PostController {

    private final PostRepository posts;

    private final CommentRepository comments;

    public PostController(PostRepository posts, CommentRepository comments) {
        this.posts = posts;
        this.comments = comments;
    }

    //other methods for /posts endpoints

    @GetMapping("/{id}/comments")
    public Flux<Comment> getCommentsOf(@PathVariable("id") String id) {
        return this.comments.findByPost(new PostId(id));
    }

    @PostMapping("/{id}/comments")
    public Mono<Comment> createCommentsOf(@PathVariable("id") String id, @RequestBody @Valid CommentForm form) {
        Comment comment = Comment.builder()
            .post(new PostId(id))
            .content(form.getContent())
            .build();

        return this.comments.save(comment);
    }

}

PostControllerTests如下所示,完整代码为here

@RunWith(SpringRunner.class)
@WebFluxTest(controllers = PostController.class)
public class PostControllerTest {

    @Autowired
    WebTestClient client;

    @MockBean
    PostRepository posts;

    @MockBean
    CommentRepository comments;
    // other tests
    @Test
    public void getCommentsByPostId_shouldBeOk() {
        given(comments.findByPost(new PostId("1")))
            .willReturn(Flux.just(Comment.builder().id("comment-id-1").content("comment of my first post").build()));

        client.get().uri("/posts/1/comments").exchange()
            .expectStatus().isOk()
            .expectBody()
            .jsonPath("$[0].id").isEqualTo("comment-id-1")
            .jsonPath("$[0].content").isEqualTo("comment of my first post");

        verify(this.comments, times(1)).findByPost(any(PostId.class));
        verifyNoMoreInteractions(this.comments);

    }

    @Test
    public void createCommentOfPost_shouldBeOk() {

        Comment comment = Comment.builder().id("comment-id-1").content("content of my first post").createdDate(LocalDateTime.now()).build();
        given(comments.save(comment))
            .willReturn(Mono.just(comment));

        CommentForm form = CommentForm.builder().content("comment of my first post").build();
        client.post().uri("/posts/1/comments").body(BodyInserters.fromObject(form))
            .exchange()
            .expectStatus().isOk()
            .expectBody()
            .jsonPath("$.id").isEqualTo("comment-id-1")
            .jsonPath("$.content").isEqualTo("content of my first post")
            .jsonPath("$.createdDate").isNotEmpty();

        verify(this.comments, times(1)).save(any(Comment.class));
        verifyNoMoreInteractions(this.comments);
    }

}

当我运行getCommentsByPostId_shouldBeOk测试时,出现异常:

java.lang.AssertionError: No value at JSON path "$[0].id"

at org.springframework.test.util.JsonPathExpectationsHelper.evaluateJsonPath(JsonPathExpectationsHelper.java:247)
at org.springframework.test.util.JsonPathExpectationsHelper.assertValue(JsonPathExpectationsHelper.java:100)
at org.springframework.test.web.reactive.server.JsonPathAssertions.isEqualTo(JsonPathAssertions.java:49)
at com.example.demo.PostControllerTest.getCommentsByPostId_shouldBeOk(PostControllerTest.java:198)
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.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.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: com.jayway.jsonpath.PathNotFoundException: No results for path: $[0]['id']
at com.jayway.jsonpath.internal.path.EvaluationContextImpl.getValue(EvaluationContextImpl.java:133)
at com.jayway.jsonpath.JsonPath.read(JsonPath.java:187)
at com.jayway.jsonpath.JsonPath.read(JsonPath.java:345)
at com.jayway.jsonpath.JsonPath.read(JsonPath.java:329)
at org.springframework.test.util.JsonPathExpectationsHelper.evaluateJsonPath(JsonPathExpectationsHelper.java:244)
... 33 more

我已经通过 curl 尝试了注释endpionts,并且还编写了integration tests for it,它确实有效。

已更新:当我将测试getCommentsByPostId_shouldBeOk更改为以下内容时:

EntityExchangeResult<byte[]>  results = client.get().uri("/posts/1/comments").exchange()
        .expectStatus().isOk()
        .expectBody().returnResult();

log.debug("return results::" + new String(results.getResponseBody()));

它打印了一个空的json数组:

2018-01-07 11:42:52.385 DEBUG 17492 --- [           main] com.example.demo.PostControllerTest      : return results::[]

1 个答案:

答案 0 :(得分:1)

原因

问题出在你的mockito模拟准备中

given(comments.findByPost(new PostId("1")))
        .willReturn(Flux.just(...));

另请注意,在您的情况下,PostId的{​​{1}}和#equals形成不正确,因此#hashcode会返回new PostId("1").eqauls(new PostId("1"))

修复

尝试以下一种方式修复你的模拟准备,然后所有期望都将通过

false

given(comments.findByPost(any())) .willReturn(Flux.just(Comment.builder().id("comment-id-1").content("comment of my first post").build())); client.get() .uri("/posts/1/comments") .exchange() .expectStatus().isOk() .expectBody() .jsonPath("$[0].id").isEqualTo("comment-id-1") .jsonPath("$[0].content").isEqualTo("comment of my first post"); verify(this.comments, times(1)).findByPost(any(PostId.class)); verifyNoMoreInteractions(this.comments); 课程添加注释@EqualsAndHashCode,以便您当前的代码可以使用

相关问题