From 23b416da73a09e934aba7a592076dfdfc039dd86 Mon Sep 17 00:00:00 2001 From: Pedro Felix Date: Thu, 20 Oct 2022 19:25:17 +0100 Subject: [PATCH] gh-2: example of a test with mocked dependencies --- code/tic-tac-tow-service/build.gradle.kts | 1 + .../service/SpringBasedServiceTests.kt | 65 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 code/tic-tac-tow-service/src/test/kotlin/pt/isel/daw/tictactow/service/SpringBasedServiceTests.kt diff --git a/code/tic-tac-tow-service/build.gradle.kts b/code/tic-tac-tow-service/build.gradle.kts index e176176..c7e9801 100644 --- a/code/tic-tac-tow-service/build.gradle.kts +++ b/code/tic-tac-tow-service/build.gradle.kts @@ -44,6 +44,7 @@ dependencies { runtimeOnly("io.netty:netty-resolver-dns-native-macos:4.1.82.Final:osx-aarch_64") } testImplementation(kotlin("test")) + testImplementation("org.mockito.kotlin:mockito-kotlin:4.0.0") ktlint("com.pinterest:ktlint:0.47.1") } diff --git a/code/tic-tac-tow-service/src/test/kotlin/pt/isel/daw/tictactow/service/SpringBasedServiceTests.kt b/code/tic-tac-tow-service/src/test/kotlin/pt/isel/daw/tictactow/service/SpringBasedServiceTests.kt new file mode 100644 index 0000000..0cf9e11 --- /dev/null +++ b/code/tic-tac-tow-service/src/test/kotlin/pt/isel/daw/tictactow/service/SpringBasedServiceTests.kt @@ -0,0 +1,65 @@ +package pt.isel.daw.tictactow.service + +import org.junit.jupiter.api.Test +import org.mockito.kotlin.any +import org.mockito.kotlin.doReturn +import org.mockito.kotlin.mock +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.boot.test.context.TestConfiguration +import org.springframework.context.annotation.Bean +import pt.isel.daw.tictactow.domain.PasswordValidationInfo +import pt.isel.daw.tictactow.domain.Token +import pt.isel.daw.tictactow.domain.TokenValidationInfo +import pt.isel.daw.tictactow.domain.User +import pt.isel.daw.tictactow.domain.UserLogic +import pt.isel.daw.tictactow.repository.Transaction +import pt.isel.daw.tictactow.repository.TransactionManager +import pt.isel.daw.tictactow.repository.UsersRepository +import java.time.Instant +import kotlin.test.assertEquals +import kotlin.test.assertNotNull + +@SpringBootTest +class SpringBasedServiceTests { + + @Autowired + lateinit var usersService: UsersService + + @Autowired + lateinit var userLogic: UserLogic + + @TestConfiguration + class Config { + @Bean + fun transactionManager(): TransactionManager = object : TransactionManager { + override fun run(block: (Transaction) -> R): R { + val mockedTransaction = mock { + val mockedUsersRepository = mock { + on { getTokenByTokenValidationInfo(any()) } doReturn Pair( + User(1, "alice", PasswordValidationInfo("")), + Token(TokenValidationInfo(""), 1, Instant.now(), Instant.now()) + ) + } + + on { usersRepository } doReturn mockedUsersRepository + } + + return block(mockedTransaction) + } + } + } + + @Test + fun check() { + // given: a valid token + val token = userLogic.generateToken() + + // when: getting a user given a token + val user = usersService.getUserByToken(token) + + // then: a user is returned + assertNotNull(user) + assertEquals("alice", user.username) + } +} \ No newline at end of file