KO EN

[Spring Boot] Kotest + Mockk

by 민갤

Back End /

Kotest

Open source testing framework to test and validate kotlin code

It provides 10 test layouts to test various situations. (Link)

Mockk

Kotlin Mocking Framework

Write a test code based on Domain Specific Language (DSL).

  • Mocking: A simulated object. A test object that virtually replaces an external service or module.

Example

Tested services using Behavior Driven Development (BDD) method, BehaviorSpec

build.gradle.kts

testImplementation("io.kotest:kotest-runner-junit5:5.5.5")
testImplementation("io.kotest:kotest-assertions-core:5.5.5")
testImplementation("io.mockk:mockk:1.13.4")

Intellij Plugin Install

  • Settings → Plugins → Search 'Kotest' → Install
intellij_kotest.png

intellij_kotest.png

service (src>main>kotlin>package-path>service>UserServiceImpl)

@Service
class UserServiceImpl(
    private val userRepository: UserRepository
) : UserService {

    override fun login(email: String, password: String): String {
        // Account Inquiry
        val user = userRepository.findByEmail(email) ?: throw RuntimeException("email? $email")
        // Confirm password
        if (user.password != password) throw PasswordFailException()
        return getSession(email)
    }

    override fun getSession(email: String): String {
        return "session-key"
    }

}

test (src>test>kotlin>package-path>service>UserServiceTest)

internal class UserServiceTest : BehaviorSpec({
    // UserService dependency object declaration
    val userRepository = mockk<UserRepository>()
    // Test Data Declaration
    var email: String
    var password: String

    Given("Test Login") {
        email = "test@gmail.com"
        password = "test-password"

        // Inject dependency objects into UserService
        val service = withContext(Dispatchers.IO) {
            UserServiceImpl(userRepository)
        }

        When("If you enter a valid account") {
            // Set account lookup result value arbitrarily
            every { userRepository.findByEmail(any()) } returns user
            Then("Login Successful") {
                val result = service.login(email = email, password = password)
                // Verification of the result value
                result shouldNotBe null
                result shouldBe "session-key"
            }
        }

        When("If you enter an invalid account") {
            And("If you enter an email that does not exist") {
                every { userRepository.findByEmail(any()) } returns null

                Then("An exception occurs") {
                    val result = shouldThrow<RuntimeException> {
                        service.login(email = email, password = password)
                    }
                    result.message shouldBe "email? $email"
                }
            }
            And("If you enter the wrong password") {
                password = "test-password-1"
                every { userRepository.findByEmail(any()) } returns user

                Then("An exception occurs") {
                    shouldThrow<PasswordFailException> {
                        service.login(email = email, password = password)
                    }
                }
            }
        }
    }
}) {
    companion object {
        val user = User(
            id = 1L,
            email = "test@gmail.com",
            password = "test-password"
        )
    }
}

Run the test

Press the green arrow on the left with the Kotest Plugin to run the test in each step.

If it runs normally from Given, the following results will be obtained.

intellij_kotest_given.png

intellij_kotest_given.png

Full code link

Author

민갤

민갤

Back-End Developer

꾸잉꾸잉하고 웁니다.

로그인

디코에 오신 것을 환영해요!
전문가들의 수많은 아티클 창고 🤓