JUnit 5 integration adapter for Kotlin test framework providing JUnit 5-specific assertions and test annotations
npx @tessl/cli install tessl/maven-org-jetbrains-kotlin--kotlin-test-junit5@2.2.0Kotlin Test JUnit 5 provides seamless integration between the Kotlin multiplatform test framework and JUnit 5, enabling developers to use kotlin.test assertions and annotations with JUnit 5's testing infrastructure. It serves as a bridge that maps kotlin.test annotations to their JUnit 5 equivalents and implements kotlin.test assertions using JUnit 5's assertion engine.
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5:2.2.0")org.jetbrains.kotlin, artifactId kotlin-test-junit5, version 2.2.0import kotlin.test.*The integration happens automatically through service provider mechanism. When JUnit 5 is detected in the classpath, kotlin.test assertions will delegate to JUnit 5's assertion engine.
import kotlin.test.*
class MyTest {
@Test
fun testExample() {
val result = 2 + 2
assertEquals(4, result)
assertTrue(result > 0)
assertNotNull(result)
}
@BeforeTest
fun setup() {
// Setup before each test
}
@AfterTest
fun cleanup() {
// Cleanup after each test
}
@Test
@Ignore
fun skippedTest() {
// This test will be skipped
}
}Kotlin Test JUnit 5 works through several key components:
JUnit5Asserter object that delegates kotlin.test assertions to JUnit 5 assertionsJUnit5Contributor that automatically registers the asserter when JUnit 5 is availableMaps kotlin.test annotations to their JUnit 5 equivalents for seamless integration.
/**
* Marks a function as a test - maps to org.junit.jupiter.api.Test
*/
public actual typealias Test = org.junit.jupiter.api.Test
/**
* Marks a test or suite as ignored - maps to org.junit.jupiter.api.Disabled
*/
public actual typealias Ignore = org.junit.jupiter.api.Disabled
/**
* Marks a function to be invoked before each test - maps to org.junit.jupiter.api.BeforeEach
*/
public actual typealias BeforeTest = org.junit.jupiter.api.BeforeEach
/**
* Marks a function to be invoked after each test - maps to org.junit.jupiter.api.AfterEach
*/
public actual typealias AfterTest = org.junit.jupiter.api.AfterEachProvides JUnit 5 assertion engine integration through the JUnit5Asserter object.
/**
* JUnit 5 assertion engine implementation for kotlin.test
* Delegates all kotlin.test assertions to org.junit.jupiter.api.Assertions
* Note: assertTrue methods are available through inherited default implementation
*/
public object JUnit5Asserter : Asserter {
/**
* Asserts that the specified values are equal
* @param message the message to report if the assertion fails
* @param expected the expected value
* @param actual the actual value
*/
override fun assertEquals(message: String?, expected: Any?, actual: Any?)
/**
* Asserts that the specified values are not equal
* @param message the message to report if the assertion fails
* @param illegal the value that should not equal actual
* @param actual the actual value
*/
override fun assertNotEquals(message: String?, illegal: Any?, actual: Any?)
/**
* Asserts that the specified values are the same instance
* @param message the message to report if the assertion fails
* @param expected the expected reference
* @param actual the actual reference
*/
override fun assertSame(message: String?, expected: Any?, actual: Any?)
/**
* Asserts that the specified values are not the same instance
* @param message the message to report if the assertion fails
* @param illegal the reference that should not be the same as actual
* @param actual the actual reference
*/
override fun assertNotSame(message: String?, illegal: Any?, actual: Any?)
/**
* Asserts that the specified value is not null
* @param message the message to report if the assertion fails
* @param actual the value to check
*/
override fun assertNotNull(message: String?, actual: Any?)
/**
* Asserts that the specified value is null
* @param message the message to report if the assertion fails
* @param actual the value to check
*/
override fun assertNull(message: String?, actual: Any?)
/**
* Fails the current test with the specified message
* @param message the message to report
*/
override fun fail(message: String?): Nothing
/**
* Fails the current test with the specified message and cause
* Available since Kotlin 1.4
* @param message the message to report
* @param cause the exception to set as the root cause
*/
override fun fail(message: String?, cause: Throwable?): Nothing
}Automatic registration system that enables JUnit 5 integration when available.
/**
* Provides JUnit5Asserter if org.junit.jupiter.api.Assertions class is found in the classpath
* Registered as a service provider for kotlin.test.AsserterContributor
*/
public class JUnit5Contributor : AsserterContributor {
/**
* Provides asserter instance or null if JUnit 5 is not available
* @return JUnit5Asserter if JUnit 5 is in classpath, null otherwise
*/
override fun contribute(): Asserter?
}These interfaces are defined in the kotlin.test framework and implemented by this library:
/**
* Abstracts the logic for performing assertions
* Specific implementations can use different testing frameworks
*/
public interface Asserter {
/**
* Fails the current test with the specified message
*/
fun fail(message: String?): Nothing
/**
* Fails the current test with the specified message and cause exception
* Available since Kotlin 1.4
*/
fun fail(message: String?, cause: Throwable?): Nothing
/**
* Asserts that the specified value is true
* @param lazyMessage function to return a message to report if the assertion fails
*/
fun assertTrue(lazyMessage: () -> String?, actual: Boolean): Unit
/**
* Asserts that the specified value is true
* @param message the message to report if the assertion fails
*/
fun assertTrue(message: String?, actual: Boolean): Unit
/**
* Asserts that the specified values are equal
*/
fun assertEquals(message: String?, expected: Any?, actual: Any?)
/**
* Asserts that the specified values are not equal
*/
fun assertNotEquals(message: String?, illegal: Any?, actual: Any?)
/**
* Asserts that the specified values are the same instance
*/
fun assertSame(message: String?, expected: Any?, actual: Any?)
/**
* Asserts that the specified values are not the same instance
*/
fun assertNotSame(message: String?, illegal: Any?, actual: Any?)
/**
* Asserts that the specified value is null
*/
fun assertNull(message: String?, actual: Any?)
/**
* Asserts that the specified value is not null
*/
fun assertNotNull(message: String?, actual: Any?)
}
/**
* Checks applicability and provides Asserter instance
*/
public interface AsserterContributor {
/**
* Provides Asserter instance or null depends on the current context
* @return asserter instance or null if it is not applicable now
*/
fun contribute(): Asserter?
}org.junit.jupiter:junit-jupiter-api:5.0.0+org.junit.jupiter:junit-jupiter-engine (for test execution)org.junit.platform:junit-platform-launcher (for test discovery)kotlin-test (parent kotlin test framework)META-INF/services/kotlin.test.AsserterContributor)