0

I'm newbie about Kotlin, Gradle. i tried to TDD for rest api(controller) test.

i saw https://spring.io/guides/tutorials/spring-boot-kotlin/ that help kotlin tdd beginner.

however, something wrong.

error message

CouponControllerTest.kt: (7, 8): Unresolved reference: io
CouponControllerTest.kt: (36, 5): Unresolved reference: every

and my test code are:

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))
  }
}

my gradle code is:

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"
  }
}

i am using intellij IDE, there are no code problems (red error message)

why this happen??

i just follow up https://spring.io/guides/tutorials/spring-boot-kotlin/

unilep
  • 313
  • 2
  • 9
  • I'm mockk author, not able to say what is wrong NinjaSquad/springmockk. You can go to their github repo and create an issue as a request for help. In general seems the problem is that mockk ia not included as a dependendcy. I would reimport project from gradle sources again additionally to creating an issue – oleksiyp May 20 '19 at 06:48
  • thanks. It works by clearing the source code(gradle, test code) and then back again. – unilep May 20 '19 at 15:58
  • having the same issue here but I am sure I got the right dependency in gradle.. any help please? – Francesco Dassisis Jul 06 '22 at 12:40

0 Answers0