0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

KotlinでJunitをするとUnresolved reference: Testエラーが発生する

Posted at

はじめに

以下の書籍でテストを学習していたところエラーが発生して止まったのでまとめます

問題

以下のテストを作成して実行しました

JdbcTaskRepositoryTest.kt
package com.example.todolist

import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.jdbc.Sql
import org.springframework.test.context.junit4.SpringRunner

@RunWith(value = SpringRunner::class)
@SpringBootTest
class TaskRepositoryTest {

    @Autowired
    private lateinit var sut: JdbcTaskRepository

    @Test
    fun 何も作成しなければfindAllは空のリストを返すこと() {
        val got = sut.findAll()
        assertThat(got).isEmpty()
    }
}

$ ./gradlew test

すると以下のエラーが発生しました


> Task :compileTestKotlin FAILED
e: /app/src/test/kotlin/com/example/todolist/JdbcTaskRepositoryTest.kt: (4, 18): Unresolved reference: Test
e: /app/src/test/kotlin/com/example/todolist/JdbcTaskRepositoryTest.kt: (5, 18): Unresolved reference: runner
e: /app/src/test/kotlin/com/example/todolist/JdbcTaskRepositoryTest.kt: (11, 2): Unresolved reference: RunWith
e: /app/src/test/kotlin/com/example/todolist/JdbcTaskRepositoryTest.kt: (18, 6): Unresolved reference: Test

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileTestKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.compilerRunner.GradleCompilerRunnerWithWorkers$GradleKotlinCompilerWorkAction
   > Compilation error. See log for more details

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

Junitのインポートがうまくいっていないようなのでbuild.gradlw.ktsを修正する必要がありそうです

解決方法

build.gradle.ktsのdepenciesに以下を追加しました

build.gradle.kts
	testImplementation("junit:junit:4.12")

Junitの4系を利用していたのですが、おそらく5を利用しようとしてエラーになっていました

image.png

おわりに

あとすこしでこの本も終了するので最後まで頑張って完走したいと思います

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?