Sangil's blog

https://github.com/ChoiSangIl Admin

Spring Kotest 설정 및 다양한 testing style 지원 DEV / WEB

2022-09-14 posted by sang12


Gradle 의존성 설정

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.kotest.extensions:kotest-extensions-spring:1.1.2")

해당 의존성을 설정해야 @Autowired와 같은 Spring 기능등을 사용하여 테스트 할 수 있다. Gradle 버전 마다 Setting 하는 방법이 다르므로 버전및 셋팅은 공식 페이지를 참고하자. (https://kotest.io/docs/quickstart )

다양한 testing style 지원

아래와 같이 다양한 testing style을 지원 한다.


참고 : https://kotest.io/docs/framework/testing-styles.html

ShouldSpec

@SpringBootTest
class BlogApplicationKtShouldSpec(
    @Autowired private val context: ApplicationContext
) : ShouldSpec({
    should("Spring Boot 서비스가 실행 됐을경우, ApllicationContext 객체를 반환해야 한다"){
        context shouldNotBe null
    }
})

BehaviorSpec

@SpringBootTest
class BlogApplicationKtBehaviorSpec(
    @Autowired private val context: ApplicationContext
) : BehaviorSpec({
    given("context 객체가 주어 졌을 때") {
        `when`("스프링부트 서비스가 올라갈 때") {
            then("context 객체를 반환해야한다") {
                context shouldNotBe null
            }
        }
    }
})

항상 junit으로 테스트 할 때… //given //when //then 을 주석으로 명시해주고 사용 했었다. 뭔가 더 직관적이다!

WorldSpec

@SpringBootTest
class BlogApplicationKtWordSpec(
    @Autowired private val context: ApplicationContext
) : WordSpec({
    "context 객체" should {
        "return not null" {
            context shouldNotBe null
        }
    }
})

StringSpec

@SpringBootTest
class BlogApplicationKtTest(
    @Autowired private val context: ApplicationContext
) : StringSpec({
    "Spring Boot 서비스가 실행 됐을경우, ApllicationContext 객체를 반환해야 한다" {
        context shouldNotBe null
    }
})
#kotest @Autowired
REPLY