Skip to content

Commit

Permalink
add: signup unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
jeongho1209 committed Apr 7, 2024
1 parent 66c9ec2 commit f9e5661
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ dependencies {
// test
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("io.projectreactor:reactor-test")
testImplementation("io.kotest:kotest-runner-junit5:5.4.2")
testImplementation("io.kotest:kotest-assertions-core:5.4.2")
testImplementation("io.kotest.extensions:kotest-extensions-spring:1.1.2")
testImplementation("io.mockk:mockk:1.13.8")

// security
implementation("org.springframework.boot:spring-boot-starter-security")
Expand Down
43 changes: 43 additions & 0 deletions src/test/kotlin/com/trip/safe/user/service/UserServiceTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.trip.safe.user.service

import com.trip.safe.common.security.jwt.JwtTokenProvider
import com.trip.safe.user.domain.User
import com.trip.safe.user.domain.UserRepository
import com.trip.safe.user.exception.UserExistException
import com.trip.safe.user.presentation.dto.request.UserSignUpRequest
import io.kotest.core.spec.style.DescribeSpec
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.mockk
import org.junit.jupiter.api.assertDoesNotThrow
import org.junit.jupiter.api.assertThrows
import org.springframework.security.crypto.password.PasswordEncoder

class UserServiceTest : DescribeSpec(
{
val userRepository = mockk<UserRepository>(relaxed = true)
val jwtTokenProvider = mockk<JwtTokenProvider>(relaxed = true)
val passwordEncoder = mockk<PasswordEncoder>(relaxed = true)
val userService = UserService(userRepository, jwtTokenProvider, passwordEncoder)
val requestStub = UserSignUpRequest("testAccountId", "testPassword", 20)

describe("유저가 회원가입을 하는 경우") {
context("이미 존재하는 유저이면") {
coEvery { userRepository.existsByAccountId(requestStub.accountId) } returns true
it("UserExistException을 던진다.") {
assertThrows<UserExistException> { userService.signUp(requestStub) }
coVerify(exactly = 0) { userRepository.save(any()) }
}
}

context("존재하지 않는 유저이면") {
coEvery { userRepository.existsByAccountId(requestStub.accountId) } returns false
coEvery { passwordEncoder.encode(requestStub.password) } returns requestStub.password
it("회원가입에 성공한다.") {
assertDoesNotThrow { userService.signUp(requestStub) }
coVerify(exactly = 1) { userRepository.save(any()) }
}
}
}
}
)

0 comments on commit f9e5661

Please sign in to comment.