Skip to content

Commit

Permalink
chore: clean codes
Browse files Browse the repository at this point in the history
  • Loading branch information
hantsy committed Jul 10, 2023
1 parent 6a419a5 commit c39f764
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions boot-kotlin/src/test/kotlin/com/example/demo/PostControllerTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {

Expand All @@ -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)
}

}

0 comments on commit c39f764

Please sign in to comment.