Marcel Stör
Marcel Stör

Reputation: 23535

JUnit 5 AfterAll not executed on Kotlin

If I run the SubClass unit below I expect @AfterAll to be executed after the test. Yet, the output is just this:

init in super class
init in sub class
test OK

I don't understand why SuperClass#stop() is not invoked. I understand that @AfterAll requires a static method or Lifecycle.PER_CLASS but this is given.

Super class

import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.TestInstance

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
open class SuperClass {

    init {
        println("init in super class")
    }

    @AfterAll
    fun stop(){
        println("service stopped")
    }
}

Sub class

import org.junit.Test

open class SubClass : SuperClass() {

    init {
        println("init in sub class")
    }

    @Test
    fun shouldRun() {
        println("test OK")
    }
}

Upvotes: 3

Views: 2081

Answers (1)

Roland
Roland

Reputation: 23242

Use org.junit.jupiter.api.Test instead of org.junit.Test in your JUnit5-tests (check also JUnit 5 User Guide - Annotations).

The following will work as expected:

import org.junit.jupiter.api.Test

open class SubClass : SuperClass() {
  init {
    println("init in sub class")
  }

  @Test
  fun shouldRun() {
    println("test OK")
  }
}

Upvotes: 7

Related Questions