Skip to content

Commit

Permalink
Add mockito-kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgleissner committed Jun 21, 2024
1 parent 9e14f3b commit b03533f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ dependencyManagement {

dependencies {
implementation "com.github.ben-manes.caffeine:caffeine"
implementation "com.google.guava:guava:33.2.1-jre"
implementation "com.google.guava:guava:33.2.+"
implementation "org.springframework.boot:spring-boot-starter-actuator"
implementation "org.springframework.boot:spring-boot-starter-cache"
implementation "org.springframework.boot:spring-boot-starter-data-jpa"
Expand All @@ -37,6 +37,7 @@ dependencies {
testImplementation "org.assertj:assertj-core"
testImplementation "org.junit.jupiter:junit-jupiter"
testImplementation "org.junit-pioneer:junit-pioneer:2.2.+"
testImplementation 'org.mockito.kotlin:mockito-kotlin:5.3.+'
testImplementation "org.springframework.boot:spring-boot-starter-test"
testImplementation "org.springframework.boot:spring-boot-testcontainers"
testImplementation "org.testcontainers:junit-jupiter"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.ArgumentMatchers.anyIterable
import org.mockito.Mock
import org.mockito.Mockito.*
import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.kotlin.any
import org.mockito.kotlin.never
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import org.springframework.cache.Cache
import org.springframework.cache.CacheManager
import uk.gleissner.loomwebflux.config.AppProperties
import uk.gleissner.loomwebflux.movie.domain.Directors.davidLynch
import uk.gleissner.loomwebflux.movie.domain.Movie
import uk.gleissner.loomwebflux.movie.domain.Movies.mulhollandDrive
import uk.gleissner.loomwebflux.movie.domain.Movies.theStraightStory

Expand All @@ -35,15 +38,15 @@ class CachedMovieRepoTest {

@BeforeEach
fun beforeEach() {
`when`(cacheManager.getCache(any())).thenReturn(cache);
whenever(cacheManager.getCache(any())).thenReturn(cache);
sut = CachedMovieRepo(appProperties, movieRepo, cacheManager)
}

@Test
fun `findByDirectorName should delegate to underlying repo`() {
val directorName = davidLynch.lastName
val expectedMovies = setOf(mulhollandDrive, theStraightStory)
`when`(movieRepo.findByDirectorName(directorName)).thenReturn(expectedMovies)
whenever(movieRepo.findByDirectorName(directorName)).thenReturn(expectedMovies)

val movies = sut.findByDirectorName(directorName)

Expand All @@ -53,19 +56,19 @@ class CachedMovieRepoTest {

@Test
fun `save should return movie without saving when repoReadOnly is true`() {
`when`(appProperties.repoReadOnly()).thenReturn(true)
whenever(appProperties.repoReadOnly()).thenReturn(true)

val savedMovies = sut.saveAll(listOf(mulhollandDrive))

assertThat(savedMovies).containsExactly(mulhollandDrive)
verify(movieRepo, never()).save(any(Movie::class.java))
verify(movieRepo, never()).save(any())
}

@Test
fun `save should delegate to underlying repo when repoReadOnly is false`() {
val movies = listOf(mulhollandDrive)
`when`(appProperties.repoReadOnly()).thenReturn(false)
`when`(movieRepo.saveAll(anyIterable())).thenReturn(movies)
whenever(appProperties.repoReadOnly()).thenReturn(false)
whenever(movieRepo.saveAll(anyIterable())).thenReturn(movies)

val savedMovies = sut.saveAll(movies)

Expand All @@ -76,7 +79,7 @@ class CachedMovieRepoTest {
@Test
fun `deleteById should not delete when repoReadOnly is true`() {
val movieId = 1L
`when`(appProperties.repoReadOnly()).thenReturn(true)
whenever(appProperties.repoReadOnly()).thenReturn(true)

sut.deleteById(movieId)

Expand All @@ -86,7 +89,7 @@ class CachedMovieRepoTest {
@Test
fun `deleteById should delegate to underlying repo when repoReadOnly is false`() {
val movieId = 1L
`when`(appProperties.repoReadOnly()).thenReturn(false)
whenever(appProperties.repoReadOnly()).thenReturn(false)

sut.deleteById(movieId)

Expand Down

0 comments on commit b03533f

Please sign in to comment.