はじめに
以下の書籍でテストを学習していたところエラーが発生して止まったのでまとめます
問題
以下のテストを作成して実行しました
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を利用しようとしてエラーになっていました
おわりに
あとすこしでこの本も終了するので最後まで頑張って完走したいと思います
参考