KT-24353 add support for junit 5 in kotlin.test
This commit is contained in:
committed by
Ilya Gorbunov
parent
6967028fdb
commit
84ee97cae9
@@ -17,6 +17,8 @@ The library consists of the modules:
|
||||
- `kotlin-test` – a JVM implementation of assertions from `kotlin-test-common`;
|
||||
- `kotlin-test-junit` – provides an implementation of [Asserter] on top of JUnit
|
||||
and maps the test annotations from `kotlin-test-annotations-common` to JUnit test annotations;
|
||||
- `kotlin-test-junit5` – provides an implementation of [Asserter] on top of JUnit 5
|
||||
and maps the test annotations from `kotlin-test-annotations-common` to JUnit 5 test annotations;
|
||||
- `kotlin-test-testng` – provides an implementation of [Asserter] on top of TestNG
|
||||
and maps the test annotations from `kotlin-test-annotations-common` to TestNG test annotations;
|
||||
- `kotlin-test-js` – a JS implementation of common test assertions and annotations
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
description = 'Kotlin Test JUnit 5'
|
||||
|
||||
apply plugin: 'kotlin-platform-jvm'
|
||||
|
||||
test {
|
||||
useJUnitPlatform()
|
||||
}
|
||||
|
||||
configureJvm6Project(project)
|
||||
configureDist(project)
|
||||
configurePublishing(project)
|
||||
|
||||
project.ext["jpsLibraryPath"] = rootProject.distLibDir
|
||||
|
||||
def junit5Version = "5.2.0"
|
||||
|
||||
dependencies {
|
||||
expectedBy project(':kotlin-test:kotlin-test-annotations-common')
|
||||
compile project(':kotlin-test:kotlin-test-jvm')
|
||||
|
||||
compile("org.junit.jupiter:junit-jupiter-api:$junit5Version")
|
||||
testRuntime("org.junit.jupiter:junit-jupiter-engine:$junit5Version")
|
||||
}
|
||||
|
||||
jar {
|
||||
manifestAttributes(manifest, project, 'Test')
|
||||
}
|
||||
|
||||
artifacts {
|
||||
archives sourcesJar
|
||||
archives javadocJar
|
||||
}
|
||||
|
||||
dist {
|
||||
from (jar, sourcesJar)
|
||||
}
|
||||
|
||||
compileKotlin {
|
||||
kotlinOptions.freeCompilerArgs = ["-Xallow-kotlin-package", "-module-name", project.name]
|
||||
}
|
||||
|
||||
compileTestKotlin {
|
||||
kotlinOptions.freeCompilerArgs = ["-Xallow-kotlin-package"]
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
|
||||
@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
|
||||
@file:JvmPackageName("kotlin.test.junit5.annotations")
|
||||
package kotlin.test
|
||||
|
||||
public actual typealias Test = org.junit.jupiter.api.Test
|
||||
public actual typealias Ignore = org.junit.jupiter.api.Disabled
|
||||
public actual typealias BeforeTest = org.junit.jupiter.api.BeforeEach
|
||||
public actual typealias AfterTest = org.junit.jupiter.api.AfterEach
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package kotlin.test.junit5
|
||||
|
||||
import org.junit.jupiter.api.Assertions
|
||||
import kotlin.test.*
|
||||
|
||||
/**
|
||||
* Provides [JUnitAsserter] if `org.junit.Assert` is found in the classpath.
|
||||
*/
|
||||
class JUnit5Contributor : AsserterContributor {
|
||||
override fun contribute(): Asserter? {
|
||||
return if (hasJUnitInClassPath) JUnit5Asserter else null
|
||||
}
|
||||
|
||||
private val hasJUnitInClassPath = try {
|
||||
Class.forName("org.junit.jupiter.api.Assertions")
|
||||
true
|
||||
} catch (_: ClassNotFoundException) {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements `kotlin.test` assertions by delegating them to `org.junit.jupiter.api.Assertions` class.
|
||||
*/
|
||||
object JUnit5Asserter : Asserter {
|
||||
override fun assertEquals(message: String?, expected: Any?, actual: Any?) {
|
||||
Assertions.assertEquals(expected, actual, message)
|
||||
}
|
||||
|
||||
override fun assertNotEquals(message: String?, illegal: Any?, actual: Any?) {
|
||||
Assertions.assertNotEquals(illegal, actual, message)
|
||||
}
|
||||
|
||||
override fun assertSame(message: String?, expected: Any?, actual: Any?) {
|
||||
Assertions.assertSame(expected, actual, message)
|
||||
}
|
||||
|
||||
override fun assertNotSame(message: String?, illegal: Any?, actual: Any?) {
|
||||
Assertions.assertNotSame(illegal, actual, message)
|
||||
}
|
||||
|
||||
override fun assertNotNull(message: String?, actual: Any?) {
|
||||
Assertions.assertNotNull(actual, message ?: "actual value is null")
|
||||
}
|
||||
|
||||
override fun assertNull(message: String?, actual: Any?) {
|
||||
Assertions.assertNull(actual, message ?: "actual value is not null")
|
||||
}
|
||||
|
||||
override fun fail(message: String?): Nothing {
|
||||
Assertions.fail<Any>(message)
|
||||
// should not get here
|
||||
throw AssertionError(message)
|
||||
}
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
kotlin.test.junit5.JUnit5Contributor
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package kotlin.test.junit5.tests
|
||||
|
||||
import org.junit.jupiter.api.Assertions
|
||||
import kotlin.test.*
|
||||
import java.util.concurrent.*
|
||||
import kotlin.test.junit5.JUnit5Asserter
|
||||
|
||||
class JUnitContributorTest {
|
||||
@Test
|
||||
fun smokeTest() {
|
||||
assertSame(JUnit5Asserter, kotlin.test.asserter)
|
||||
Assertions.assertEquals(JUnit5Asserter::class.java.simpleName, kotlin.test.asserter.javaClass.simpleName)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parallelThreadGetsTheSameAsserter() {
|
||||
val q = ArrayBlockingQueue<Any>(1)
|
||||
|
||||
Thread {
|
||||
q.put(asserter)
|
||||
}.start()
|
||||
|
||||
assertSame(kotlin.test.asserter, q.take())
|
||||
}
|
||||
|
||||
}
|
||||
@@ -100,6 +100,7 @@ include ":kotlin-build-common",
|
||||
":kotlin-test:kotlin-test-annotations-common",
|
||||
":kotlin-test:kotlin-test-jvm",
|
||||
":kotlin-test:kotlin-test-junit",
|
||||
":kotlin-test:kotlin-test-junit5",
|
||||
":kotlin-test:kotlin-test-testng",
|
||||
":kotlin-test:kotlin-test-js",
|
||||
":kotlin-test:kotlin-test-js:kotlin-test-js-it",
|
||||
@@ -188,6 +189,7 @@ project(':kotlin-test:kotlin-test-common').projectDir = "$rootDir/libraries/kotl
|
||||
project(':kotlin-test:kotlin-test-annotations-common').projectDir = "$rootDir/libraries/kotlin.test/annotations-common" as File
|
||||
project(':kotlin-test:kotlin-test-jvm').projectDir = "$rootDir/libraries/kotlin.test/jvm" as File
|
||||
project(':kotlin-test:kotlin-test-junit').projectDir = "$rootDir/libraries/kotlin.test/junit" as File
|
||||
project(':kotlin-test:kotlin-test-junit5').projectDir = "$rootDir/libraries/kotlin.test/junit5" as File
|
||||
project(':kotlin-test:kotlin-test-testng').projectDir = "$rootDir/libraries/kotlin.test/testng" as File
|
||||
project(':kotlin-test:kotlin-test-js').projectDir = "$rootDir/libraries/kotlin.test/js" as File
|
||||
project(':kotlin-test:kotlin-test-js:kotlin-test-js-it').projectDir = "$rootDir/libraries/kotlin.test/js/it" as File
|
||||
|
||||
Reference in New Issue
Block a user