我是关于 Kotlin 的新手,Gradle。我试图对休息 api(控制器)测试进行 TDD。
我看到了帮助 kotlin tdd 初学者的https://spring.io/guides/tutorials/spring-boot-kotlin/。
但是,有些不对劲。
错误信息
CouponControllerTest.kt: (7, 8): Unresolved reference: io
CouponControllerTest.kt: (36, 5): Unresolved reference: every
我的测试代码是:
package com.unilep.demo.controller
import com.ninjasquad.springmockk.MockkBean
import com.unilep.demo.model.Coupon
import com.unilep.demo.service.CouponService
import com.unilep.demo.util.createCoupon
import io.mockk.every
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.http.MediaType
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.*
@WebMvcTest
class CouponControllerTest(@Autowired val mockMvc: MockMvc) {
@MockkBean
lateinit var couponService: CouponService
@Test
fun `한 유저가 2개의 쿠폰을 생성한 후 결과를 조회하면 2개의 쿠폰이 나와야 한다`() {
val email = "unilep@naver.com"
val coupon1 = Coupon(
id = 1,
email = email,
coupon = createCoupon()
)
val coupon2 = Coupon(
id = 2,
email = email,
coupon = createCoupon()
)
every { couponService.getCouponsByEmail(email) } returns listOf(coupon1, coupon2)
mockMvc.perform(get("/coupon/$email").accept(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().isOk)
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("\$.[0]").value(coupon1))
.andExpect(jsonPath("\$.[1]").value(coupon2))
}
}
我的毕业代码是:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("plugin.jpa") version "1.3.31"
id("org.springframework.boot") version "2.1.5.RELEASE"
id("io.spring.dependency-management") version "0.6.0.RELEASE"
kotlin("jvm") version "1.3.31"
kotlin("plugin.spring") version "1.3.31"
}
group = "com.unilep"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
runtimeOnly("com.h2database:h2")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(module="junit")
exclude(module="mockito-core")
}
testImplementation("com.ninja-squad:springmockk:1.1.0")
testImplementation("org.junit.jupiter:junit-jupiter-api")
testImplementation("org.junit.jupiter:junit-jupiter-engine")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "1.8"
}
}
我正在使用 intellij IDE,没有代码问题(红色错误消息)
为什么会这样??