question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Kotlintest and Spring?

See original GitHub issue

I’m trying to use kotlintest along with Spring (not Spring Boot, just the standard spring-test). I’m finding it very difficult to do. Any pointers as to what I am doing wrong? I am somewhat of a Kotlin newbie as well so I could very well not be doing something correctly.

This is what I’ve tried so far:

import io.kotlintest.matchers.shouldBe
import io.kotlintest.specs.BehaviorSpec

import org.junit.ClassRule
import org.junit.Rule

import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.junit4.rules.SpringClassRule
import org.springframework.test.context.junit4.rules.SpringMethodRule

open class SomeBean {
  open fun sayHello() = "Hello"
}

@Configuration
open class TestConfig {
  @Bean
  open fun someBean(): SomeBean = SomeBean()
}

@ContextConfiguration(classes = arrayOf(TestConfig::class))
open class MyTests(var someBean: SomeBean) : BehaviorSpec() {
  @Rule
  @JvmField
  val springMethodRule: SpringMethodRule = SpringMethodRule()
	
  companion object {
    @ClassRule
    @JvmField
    val SPRING_CLASS_RULE: SpringClassRule = SpringClassRule()
  }
	
  init {
    given("A test") {
      `when`("When my test happens") {
        val hello = someBean.sayHello()
        
        then("Hello should be valid") {
          hello shouldBe "Hello"
        }
      }
    }
  }
}

Output:

java.lang.InstantiationException: MyTests
	at java.lang.Class.newInstance(Class.java:427)
	at io.kotlintest.KTestJUnitRunner.<init>(KTestJUnitRunner.kt:13)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
	at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
	at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
	at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
	at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
	at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:86)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:57)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:66)
	at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
	at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
	at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
	at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
	at com.sun.proxy.$Proxy1.processTestClass(Unknown Source)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:109)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
	at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
	at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:146)
	at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:128)
	at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:404)
	at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
	at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
	at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NoSuchMethodException: MyTests.<init>()
	at java.lang.Class.getConstructor0(Class.java:3082)
	at java.lang.Class.newInstance(Class.java:412)
	... 40 more

Another try:

import io.kotlintest.matchers.shouldBe
import io.kotlintest.specs.BehaviorSpec

import org.junit.ClassRule
import org.junit.Rule

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.junit4.rules.SpringClassRule
import org.springframework.test.context.junit4.rules.SpringMethodRule

open class SomeBean {
  open fun sayHello() = "Hello"
}

@Configuration
open class TestConfig {
  @Bean
  open fun someBean(): SomeBean = SomeBean()
}

@ContextConfiguration(classes = arrayOf(TestConfig::class))
open class MyTests : BehaviorSpec() {
  @Rule
  @JvmField
  val springMethodRule: SpringMethodRule = SpringMethodRule()

  @Autowired
  lateinit var someBean: SomeBean

  companion object {
    @ClassRule
    @JvmField
    val SPRING_CLASS_RULE: SpringClassRule = SpringClassRule()
  }

  init {
    given("A test") {
      `when`("When my test happens") {
          val hello = someBean.sayHello()

	  then("Hello should be valid") {
           hello shouldBe "Hello"
        }
      }
    }
  }
}

Output:

kotlin.UninitializedPropertyAccessException: lateinit property someBean has not been initialized
	at MyTests.getSomeBean(SpringTests.kt:31)
	at MyTests$1$1.invoke(SpringTests.kt:42)
	at MyTests$1$1.invoke(SpringTests.kt:25)
	at io.kotlintest.specs.BehaviorSpec$Given.when(BehaviorSpec.kt:27)
	at MyTests$1.invoke(SpringTests.kt:41)
	at MyTests$1.invoke(SpringTests.kt:25)
	at io.kotlintest.specs.BehaviorSpec.given(BehaviorSpec.kt:18)
	at MyTests.<init>(SpringTests.kt:40)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at java.lang.Class.newInstance(Class.java:442)
	at io.kotlintest.KTestJUnitRunner.<init>(KTestJUnitRunner.kt:13)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
	at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
	at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
	at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
	at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
	at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:86)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:57)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:66)
	at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
	at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
	at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
	at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
	at com.sun.proxy.$Proxy1.processTestClass(Unknown Source)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:109)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
	at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
	at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:146)
	at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:128)
	at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:404)
	at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
	at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
	at java.lang.Thread.run(Thread.java:748)

This works, but then its not using kotlintest - its just using standard JUnit, which isn’t what I want

@ContextConfiguration(classes = arrayOf(TestConfig::class))
class Tests {
  @Rule 
  @JvmField
  val springMethodRule: SpringMethodRule = SpringMethodRule()

  @Autowired
  private lateinit var someBean: SomeBean

  companion object {
    @ClassRule
    @JvmField
    val SPRING_CLASS_RULE: SpringClassRule = SpringClassRule()
  }
	
  @Test
  fun runTest(): Unit {
    someBean.sayHello() shouldBe "Hello"
  }
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
bondpp7commented, Feb 5, 2018

For autowiring of dependencies (which seems to be your intention), you may look into Kotlintest-Spring at https://github.com/Acomodeo/kotlintest-spring. The entire purpose of that project is to ease the use of Spring’s context within KotlinTest.

0reactions
sksamuelcommented, Mar 23, 2018

Done in master for spring 4 and spring 5.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How does kotlintest test spring boot application - Stack Overflow
Kotlintest has a very nice guide on how to setup Spring Extension to test Spring Framework. Basically, in addition to KotlinTest dependency, you ......
Read more >
Guide to Spring Boot Testing with Kotlin - Baeldung
In this tutorial, we'll explore a few testing techniques for a Kotlin based Spring Boot application. First, let's create a basic Kotlin based ......
Read more >
Testing a Spring Boot application with Kotest
In this article we'll see how to test Spring Boot apps using the Kotest Framework. We'll see some examples on how we test...
Read more >
Kotlin Spring Boot Unit Testing & Integration Testing ... - Medium
I have here made a simple CRUD application that helps to manage bank details, we will write the basic writing some basic test...
Read more >
Writing Spring tests with Kotlin
Preface Usually we write Spring projects using Java language for business development and Java for unit testing.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found