I know this should be possible, as others have reported it working. However, in a simple 'demo' project (Spring Initializer), I can't get the autowired property to be initialized within the test.
When I execute the test in "JpaTestApplicationTests", I can't get past receiving "lateinit property repo has not been initialized kotlin.UninitializedPropertyAccessException".
Can anyone help me understand what I'm doing wrong?
Below is my application file, bean to be autowired, and test:
Application:
package com.example.jpaTest
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
@SpringBootApplication
class JpaTestApplication
fun main(args: Array<String>) {
SpringApplication.run(JpaTestApplication::class.java, *args)
}
Bean:
package com.example.jpaTest
import org.springframework.data.repository.CrudRepository
import org.springframework.stereotype.Repository
@Repository
interface FakeRepository : CrudRepository<Fake, String>
Test:
package com.example.jpaTest
import io.kotlintest.*
import io.kotlintest.specs.*
import io.kotlintest.spring.SpringListener
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.test.context.ContextConfiguration
@ContextConfiguration(classes = [JpaTestApplication::class])
class JpaTestApplicationTests : StringSpec() {
override fun listeners() = listOf(SpringListener)
@Autowired
lateinit var repo: FakeRepository
init {
"can get by id"{
val it = Fake("Blah","testName1")
val saved = repo.save(it)
repo.findOne(saved.uuid) shouldBe saved
}
}
}
Relevant Gradle entries:
buildscript {
ext {
kotlinVersion = '1.2.61'
springBootVersion = '1.5.16.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlinVersion"
}
}
apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'
apply plugin: "kotlin-jpa"
apply plugin: 'org.springframework.boot'
test {
useJUnitPlatform()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-web')
compile('com.fasterxml.jackson.module:jackson-module-kotlin')
compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}")
compile("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile 'io.kotlintest:kotlintest-runner-junit5:3.1.8'
testCompile 'io.kotlintest:kotlintest-extensions-spring:3.1.8'
}