From c39f76487480f8bd32352f5295ed919e12c110fa Mon Sep 17 00:00:00 2001 From: hantsy Date: Mon, 10 Jul 2023 16:54:24 +0800 Subject: [PATCH] chore: clean codes --- .../com/example/demo/PostControllerTests.kt | 38 ++++++++++++++----- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/boot-kotlin/src/test/kotlin/com/example/demo/PostControllerTests.kt b/boot-kotlin/src/test/kotlin/com/example/demo/PostControllerTests.kt index d79564934..d3d009aa6 100644 --- a/boot-kotlin/src/test/kotlin/com/example/demo/PostControllerTests.kt +++ b/boot-kotlin/src/test/kotlin/com/example/demo/PostControllerTests.kt @@ -4,7 +4,6 @@ package com.example.demo import org.junit.jupiter.api.BeforeAll import org.junit.jupiter.api.Test import org.mockito.BDDMockito.* -import org.mockito.Mockito import org.mockito.Mockito.verify import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration @@ -13,10 +12,12 @@ import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest import org.springframework.boot.test.mock.mockito.MockBean import org.springframework.test.web.reactive.server.WebTestClient import reactor.core.publisher.Flux +import reactor.core.publisher.Mono +import java.util.* @WebFluxTest( - controllers = [PostController::class], - excludeAutoConfiguration = [ReactiveUserDetailsServiceAutoConfiguration::class, ReactiveSecurityAutoConfiguration::class] + controllers = [PostController::class], + excludeAutoConfiguration = [ReactiveUserDetailsServiceAutoConfiguration::class, ReactiveSecurityAutoConfiguration::class] ) class PostControllerTests { @@ -35,17 +36,34 @@ class PostControllerTests { @Test fun `get all posts`() { val postsFlux = Flux.just("Post one", "Post two") - .map { - Post(title = it, content = "content of $it") - } + .map { + Post(title = it, content = "content of $it") + } given(posts.findAll()).willReturn(postsFlux) client.get() - .uri("/posts") - .exchange() - .expectStatus() - .isOk + .uri("/posts") + .exchange() + .expectStatus() + .isOk verify(posts, times(1)).findAll() verifyNoMoreInteractions(this.posts) } + @Test + fun `get post by id`() { + val id = UUID.randomUUID().toString() + val postMono = Mono + .just( + Post(id = id, title = "test", content = "content of test") + ) + given(posts.findById(any(String::class.java))).willReturn(postMono) + client.get() + .uri("/posts/$id") + .exchange() + .expectStatus() + .isOk + verify(posts, times(1)).findById(anyString()) + verifyNoMoreInteractions(this.posts) + } + }