-
Kotest와 MockK를 활용하여 컨트롤러 단위테스트하기프로젝트/미디어 스트리밍 서버 프로젝트 2022. 8. 23. 18:55반응형
개요
JUnit5와 @WebMvcTest를 이용한 컨트롤러 단위 테스트는 익숙하지만 Kotest와 MockK를 활용한 @WevMvcTest를 수행하고자 합니다.
일단 아주 간단한 테스트 컨트롤러를 하나 만들고 단위테스트를 진행해보겠습니다.
TestController
@RestController class TestController { private val test = "test" @GetMapping fun test(): String { return test } }
Controller 단위 테스트
@WebMvcTest(TestController::class) class MyTest3 constructor(@Autowired val mockMvc: MockMvc) : BehaviorSpec({ given("nothing"){ `when`("visit /"){ val result = mockMvc.get("/") { contentType = MediaType.APPLICATION_JSON accept = MediaType.APPLICATION_JSON } then("content is test"){ result.andExpect { status { MockMvcResultMatchers.status().isOk} content {"test"} } } } } })
Could not create instance of class com.idlenara.video.MyTest3. Specs must have a public zero-arg constructor.
위와 같은 에러가 발생하게 됩니다.
코틀린 스타일을 상속받고 실행하면 default constructor가 필요하다는 에러입니다.
해결 방법으로 dependency를 추가해줍니다.
testImplementation("io.kotest.extensions:kotest-extensions-spring:1.1.1")
출처
https://www.baeldung.com/kotlin/mockmvc-kotlin-dsl
MockMvc Kotlin DSL | Baeldung on Kotlin
Have a look at the new Kotlin-specific MockMvc support available in Spring
www.baeldung.com
https://www.baeldung.com/kotlin/spring-boot-testing
Guide to Spring Boot Testing with Kotlin | Baeldung on Kotlin
Explore a few unit and integration testing techniques for the Spring Boot app with Kotlin.
www.baeldung.com
https://velog.io/@wltn716/Kotlin-Spring-Kotest%EC%97%90-Mockk-%EC%A0%81%EC%9A%A9%ED%95%98%EA%B8%B0
[Kotlin + Spring] Kotest에 Mockk 적용하기
Item 레코드의 id를 받아 Item 엔티티를 Dto로 변환해 리턴하는 ItemService
velog.io
https://jaehhh.tistory.com/118
왜 Kotest를 사용해야 할까?
1. 왜 Kotest로 전환해야 할까? 먼저, JUnit 의 단점을 알아보자. 테스트코드가 중복될 경우가 많다. 한눈에 알아보기 어렵다. 테스트 스타일이 한정적이다, 단위테스트에 특화되어 있다. Kotest의 장
jaehhh.tistory.com
Spring Annotation @WebMvcTest does not work in an app that has Jpa repositories
I have a Spring App that uses JPA repositories (CrudRepository interfaces). When I try to test my controller using the new Spring test syntax @WebMvcTest(MyController.class), it fails coz it tries to
stackoverflow.com
'프로젝트 > 미디어 스트리밍 서버 프로젝트' 카테고리의 다른 글
kotlinDSL + RestDocs 적용하기 (0) 2022.09.28 MultipartFile 컨트롤러 단위 테스트(MockMvc) (0) 2022.08.24 ffmpeg install (Mac OS, Amazon Linux) (0) 2022.08.22 JPA Paging 적용하기 (0) 2022.08.20 Json with MultipartFile (0) 2022.08.19