From 2d6ca09a18f2ee57a49d9b1ff868fbc4d8217f0b Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Thu, 3 Aug 2017 20:44:40 -0400 Subject: [PATCH 01/22] Add Kotlin Assertion methods Closes #924 --- build.gradle | 12 ++++ junit-jupiter-api/junit-jupiter-api.gradle | 1 + .../org/junit/jupiter/api/Assertions.kt | 68 +++++++++++++++++++ .../jupiter/api/AssertionsAssertAllTests.java | 2 +- .../api/AssertionsAssertAllKotlinTests.kt | 63 +++++++++++++++++ 5 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt create mode 100644 junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/AssertionsAssertAllKotlinTests.kt diff --git a/build.gradle b/build.gradle index 679302f2c4f1..0c823cd7ec66 100644 --- a/build.gradle +++ b/build.gradle @@ -27,6 +27,7 @@ buildscript { plugins { id 'com.gradle.build-scan' version '1.9' id 'net.nemerosa.versioning' version '2.6.1' + id "org.jetbrains.kotlin.jvm" version "1.1.3-2" apply false } buildScan { @@ -225,6 +226,10 @@ allprojects { subproj -> } } +configure([project(":junit-jupiter-api"), project(":junit-jupiter-engine")]) { + apply plugin: "org.jetbrains.kotlin.jvm" +} + subprojects { subproj -> if (subproj.name in mavenizedProjects) { apply plugin: 'maven' @@ -434,6 +439,13 @@ subprojects { subproj -> replaceRegex 'Empty line between last method and class closure', /\n([\s]+)}\n}\n$/, '\n$1}\n\n}\n' replaceRegex 'Remove line breaks between consecutive closing parentheses', /\)\n[\s]+\)\n/, '))\n' } + + kotlin { + ktlint("0.9.0") + licenseHeaderFile headerText + trimTrailingWhitespace() + endWithNewline() + } } afterEvaluate { diff --git a/junit-jupiter-api/junit-jupiter-api.gradle b/junit-jupiter-api/junit-jupiter-api.gradle index 360dcd82c81c..7ec614adae58 100644 --- a/junit-jupiter-api/junit-jupiter-api.gradle +++ b/junit-jupiter-api/junit-jupiter-api.gradle @@ -1,6 +1,7 @@ dependencies { api("org.opentest4j:opentest4j:${ota4jVersion}") api(project(':junit-platform-commons')) + compileOnly("org.jetbrains.kotlin:kotlin-stdlib") } jar { diff --git a/junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt b/junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt new file mode 100644 index 000000000000..367b7451e7d1 --- /dev/null +++ b/junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt @@ -0,0 +1,68 @@ +/* + * Copyright 2015-2017 the original author or authors. + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution and is available at + * + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.junit.jupiter.api + +import org.junit.jupiter.api.function.Executable +import org.junit.platform.commons.meta.API +import org.junit.platform.commons.meta.API.Usage.Experimental +import java.util.stream.Stream + +/** + * [Stream] of functions to be executed. + */ +internal typealias ExecutableStream = Stream<() -> Unit> +internal fun ExecutableStream.convert() = map { Executable(it) } + +/** + * @see Assertions.assertAll + * @since 5.0 + */ +@API(Experimental) +fun assertAll(executables: ExecutableStream) = + Assertions.assertAll(executables.convert()) + +/** + * @see Assertions.assertAll + * @since 5.0 + */ +@API(Experimental) +fun assertAll(heading : String?, executables: ExecutableStream) = + Assertions.assertAll(heading, executables.convert()) + +/** + * @see Assertions.assertAll + * @since 5.0 + */ +@API(Experimental) +fun assertAll(vararg executables: () -> Unit) = + assertAll(executables.toList().stream()) + +/** + * @see Assertions.assertAll + * @since 5.0 + */ +@API(Experimental) +fun assertAll(heading: String?, vararg executables: () -> Unit) = + assertAll(heading, executables.toList().stream()) + +/** + * Example usage: + * ```kotlin + * val exception = assertThrows { + * throw IllegalArgumentException("Talk to a 🦆") + * } + * assertEquals("Talk to a 🦆", exception.message) + * ``` + * @see Assertions.assertThrows + * @since 5.0 + */ +@API(Experimental) +inline fun assertThrows(noinline executable : () -> Unit) : T = + Assertions.assertThrows(T::class.java, Executable(executable)) diff --git a/junit-jupiter-engine/src/test/java/org/junit/jupiter/api/AssertionsAssertAllTests.java b/junit-jupiter-engine/src/test/java/org/junit/jupiter/api/AssertionsAssertAllTests.java index a3485671f98c..34c797d4cf26 100644 --- a/junit-jupiter-engine/src/test/java/org/junit/jupiter/api/AssertionsAssertAllTests.java +++ b/junit-jupiter-engine/src/test/java/org/junit/jupiter/api/AssertionsAssertAllTests.java @@ -149,7 +149,7 @@ void assertAllWithExecutableThatThrowsBlacklistedException() { } @SafeVarargs - private static void assertExpectedExceptionTypes(MultipleFailuresError multipleFailuresError, + static void assertExpectedExceptionTypes(MultipleFailuresError multipleFailuresError, Class... exceptionTypes) { assertNotNull(multipleFailuresError, "MultipleFailuresError"); diff --git a/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/AssertionsAssertAllKotlinTests.kt b/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/AssertionsAssertAllKotlinTests.kt new file mode 100644 index 000000000000..7f10c10751cd --- /dev/null +++ b/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/AssertionsAssertAllKotlinTests.kt @@ -0,0 +1,63 @@ +/* + * Copyright 2015-2017 the original author or authors. + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution and is available at + * + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.junit.jupiter.api + +import org.junit.jupiter.api.Assertions.assertFalse +import org.junit.jupiter.api.Assertions.assertTrue +import org.opentest4j.AssertionFailedError +import org.opentest4j.MultipleFailuresError +import java.util.stream.Stream +import kotlin.reflect.KClass + +/** + * Unit tests for JUnit Jupiter {@link Assertions}. + * + * @since 5.0 + */ +class AssertionsAssertAllKotlinTests { + + // Bonus: no null check tests as these get handled by the compiler! + + @Test + fun `assertAll with functions that do not throw exceptions`() { + assertAll( + { assertTrue(true) }, + { assertFalse(false) }, + { assertTrue(true) } + ) + } + + @Test + fun `assertAll with functions that throw AssertionErrors`() { + val multipleFailuresError = assertThrows { + assertAll( + { assertFalse(true) }, + { assertFalse(true) } + ) + } + assertExpectedExceptionTypes(multipleFailuresError, AssertionFailedError::class, AssertionFailedError::class) + } + + @Test + fun `assertAll with streamOf functions that throw AssertionErrors`() { + val multipleFailuresError = assertThrows { + assertAll(Stream.of({ assertFalse(true) }, { assertFalse(true) })) + } + assertExpectedExceptionTypes(multipleFailuresError, AssertionFailedError::class, AssertionFailedError::class) + } + + companion object { + fun assertExpectedExceptionTypes( + multipleFailuresError: MultipleFailuresError, + vararg exceptionTypes: KClass) = + AssertionsAssertAllTests.assertExpectedExceptionTypes( + multipleFailuresError, *exceptionTypes.map { it.java }.toTypedArray()) + } +} From 451930ecf81b58fb132c7349434c98e59cd75674 Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Thu, 3 Aug 2017 21:12:22 -0400 Subject: [PATCH 02/22] Cleanup build and ensure kotlin sources are included in api JAR --- build.gradle | 2 +- junit-jupiter-api/junit-jupiter-api.gradle | 13 ------- .../junit-jupiter-api.gradle.kts | 39 +++++++++++++++++++ settings.gradle | 3 ++ 4 files changed, 43 insertions(+), 14 deletions(-) delete mode 100644 junit-jupiter-api/junit-jupiter-api.gradle create mode 100644 junit-jupiter-api/junit-jupiter-api.gradle.kts diff --git a/build.gradle b/build.gradle index 0c823cd7ec66..3ebf91fcd579 100644 --- a/build.gradle +++ b/build.gradle @@ -442,7 +442,7 @@ subprojects { subproj -> kotlin { ktlint("0.9.0") - licenseHeaderFile headerText + licenseHeaderFile headerFile trimTrailingWhitespace() endWithNewline() } diff --git a/junit-jupiter-api/junit-jupiter-api.gradle b/junit-jupiter-api/junit-jupiter-api.gradle deleted file mode 100644 index 7ec614adae58..000000000000 --- a/junit-jupiter-api/junit-jupiter-api.gradle +++ /dev/null @@ -1,13 +0,0 @@ -dependencies { - api("org.opentest4j:opentest4j:${ota4jVersion}") - api(project(':junit-platform-commons')) - compileOnly("org.jetbrains.kotlin:kotlin-stdlib") -} - -jar { - manifest { - attributes( - 'Automatic-Module-Name': 'org.junit.jupiter.api' - ) - } -} diff --git a/junit-jupiter-api/junit-jupiter-api.gradle.kts b/junit-jupiter-api/junit-jupiter-api.gradle.kts new file mode 100644 index 000000000000..e5521e4e90ed --- /dev/null +++ b/junit-jupiter-api/junit-jupiter-api.gradle.kts @@ -0,0 +1,39 @@ +import org.gradle.jvm.tasks.Jar +import org.jetbrains.kotlin.gradle.tasks.KotlinCompile + +dependencies { + "api"("org.opentest4j:opentest4j:${properties["ota4jVersion"]}") + "api"(project(":junit-platform-commons")) + compileOnly("org.jetbrains.kotlin:kotlin-stdlib") +} + +tasks { + "jar"(Jar::class) { + manifest { + attributes(mapOf( + "Automatic-Module-Name" to "org.junit.jupiter.api" + )) + } + } +} + +configurations { + "apiElements" { + /* + * Needed to configure kotlin to work correctly with the "java-library" plugin. + * See: + * https://docs.gradle.org/current/userguide/java_library_plugin.html#sec:java_library_known_issues + * https://youtrack.jetbrains.com/issue/KT-18497 + */ + val compileKotlin: KotlinCompile by tasks + outgoing + .variants + .getByName("classes") + .artifact(mapOf( + "file" to compileKotlin.destinationDir, + "type" to "java-classes-directory", + "builtBy" to compileKotlin + )) + } +} + diff --git a/settings.gradle b/settings.gradle index 669c93764e3e..de3f77fe7693 100644 --- a/settings.gradle +++ b/settings.gradle @@ -21,5 +21,8 @@ include 'platform-tests' // based on the project name rootProject.children.each { project -> project.buildFileName = "${project.name}.gradle" + if (!project.buildFile.isFile()) { + project.buildFileName = "${project.name}.gradle.kts" + } assert project.buildFile.isFile() } From b5279aa00656919b254a572c613e9d6e3432bdc8 Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Thu, 3 Aug 2017 21:23:21 -0400 Subject: [PATCH 03/22] Cleanup spotless problems on existing kotlin code --- .../kotlin/InstancePerClassKotlinTestCase.kt | 16 +++++++++++++++- .../kotlin/InstancePerMethodKotlinTestCase.kt | 15 ++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/engine/kotlin/InstancePerClassKotlinTestCase.kt b/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/engine/kotlin/InstancePerClassKotlinTestCase.kt index 787630e0a936..2ffaab4b92b8 100644 --- a/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/engine/kotlin/InstancePerClassKotlinTestCase.kt +++ b/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/engine/kotlin/InstancePerClassKotlinTestCase.kt @@ -1,6 +1,20 @@ +/* + * Copyright 2015-2017 the original author or authors. + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution and is available at + * + * http://www.eclipse.org/legal/epl-v10.html + */ package org.junit.jupiter.engine.kotlin -import org.junit.jupiter.api.* +import org.junit.jupiter.api.AfterAll +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.TestInstance import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS @TestInstance(PER_CLASS) diff --git a/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/engine/kotlin/InstancePerMethodKotlinTestCase.kt b/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/engine/kotlin/InstancePerMethodKotlinTestCase.kt index bbcc6e5e61d5..d6abdf21666b 100644 --- a/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/engine/kotlin/InstancePerMethodKotlinTestCase.kt +++ b/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/engine/kotlin/InstancePerMethodKotlinTestCase.kt @@ -1,6 +1,19 @@ +/* + * Copyright 2015-2017 the original author or authors. + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution and is available at + * + * http://www.eclipse.org/legal/epl-v10.html + */ package org.junit.jupiter.engine.kotlin -import org.junit.jupiter.api.* +import org.junit.jupiter.api.AfterAll +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test class InstancePerMethodKotlinTestCase { From 7c9102c094285225b941b105d955e4ec5046905d Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Thu, 3 Aug 2017 21:25:50 -0400 Subject: [PATCH 04/22] Remove duplicate kotlin plugin application --- build.gradle | 5 ----- 1 file changed, 5 deletions(-) diff --git a/build.gradle b/build.gradle index 3ebf91fcd579..f75f63cf905a 100644 --- a/build.gradle +++ b/build.gradle @@ -27,7 +27,6 @@ buildscript { plugins { id 'com.gradle.build-scan' version '1.9' id 'net.nemerosa.versioning' version '2.6.1' - id "org.jetbrains.kotlin.jvm" version "1.1.3-2" apply false } buildScan { @@ -226,10 +225,6 @@ allprojects { subproj -> } } -configure([project(":junit-jupiter-api"), project(":junit-jupiter-engine")]) { - apply plugin: "org.jetbrains.kotlin.jvm" -} - subprojects { subproj -> if (subproj.name in mavenizedProjects) { apply plugin: 'maven' From 43c956ec23fa49a4452dfb0c6881d90b13854548 Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Wed, 30 Aug 2017 16:37:49 -0400 Subject: [PATCH 05/22] Revert changes to using Gradle Kotlin DSL --- junit-jupiter-api/junit-jupiter-api.gradle | 32 +++++++++++++++ .../junit-jupiter-api.gradle.kts | 39 ------------------- settings.gradle | 3 -- 3 files changed, 32 insertions(+), 42 deletions(-) create mode 100644 junit-jupiter-api/junit-jupiter-api.gradle delete mode 100644 junit-jupiter-api/junit-jupiter-api.gradle.kts diff --git a/junit-jupiter-api/junit-jupiter-api.gradle b/junit-jupiter-api/junit-jupiter-api.gradle new file mode 100644 index 000000000000..0383faec905b --- /dev/null +++ b/junit-jupiter-api/junit-jupiter-api.gradle @@ -0,0 +1,32 @@ +dependencies { + api("org.opentest4j:opentest4j:${ota4jVersion}") + api(project(":junit-platform-commons")) + compileOnly("org.jetbrains.kotlin:kotlin-stdlib") +} + +jar { + manifest { + attributes( + 'Automatic-Module-Name': 'org.junit.jupiter.api' + ) + } +} + +configurations { + apiElements { + /* + * Needed to configure kotlin to work correctly with the "java-library" plugin. + * See: + * https://docs.gradle.org/current/userguide/java_library_plugin.html#sec:java_library_known_issues + * https://youtrack.jetbrains.com/issue/KT-18497 + */ + outgoing + .variants + .getByName("classes") + .artifact( + "file" : compileKotlin.destinationDir, + "type" : "java-classes-directory", + "builtBy" : compileKotlin + ) + } +} diff --git a/junit-jupiter-api/junit-jupiter-api.gradle.kts b/junit-jupiter-api/junit-jupiter-api.gradle.kts deleted file mode 100644 index e5521e4e90ed..000000000000 --- a/junit-jupiter-api/junit-jupiter-api.gradle.kts +++ /dev/null @@ -1,39 +0,0 @@ -import org.gradle.jvm.tasks.Jar -import org.jetbrains.kotlin.gradle.tasks.KotlinCompile - -dependencies { - "api"("org.opentest4j:opentest4j:${properties["ota4jVersion"]}") - "api"(project(":junit-platform-commons")) - compileOnly("org.jetbrains.kotlin:kotlin-stdlib") -} - -tasks { - "jar"(Jar::class) { - manifest { - attributes(mapOf( - "Automatic-Module-Name" to "org.junit.jupiter.api" - )) - } - } -} - -configurations { - "apiElements" { - /* - * Needed to configure kotlin to work correctly with the "java-library" plugin. - * See: - * https://docs.gradle.org/current/userguide/java_library_plugin.html#sec:java_library_known_issues - * https://youtrack.jetbrains.com/issue/KT-18497 - */ - val compileKotlin: KotlinCompile by tasks - outgoing - .variants - .getByName("classes") - .artifact(mapOf( - "file" to compileKotlin.destinationDir, - "type" to "java-classes-directory", - "builtBy" to compileKotlin - )) - } -} - diff --git a/settings.gradle b/settings.gradle index de3f77fe7693..669c93764e3e 100644 --- a/settings.gradle +++ b/settings.gradle @@ -21,8 +21,5 @@ include 'platform-tests' // based on the project name rootProject.children.each { project -> project.buildFileName = "${project.name}.gradle" - if (!project.buildFile.isFile()) { - project.buildFileName = "${project.name}.gradle.kts" - } assert project.buildFile.isFile() } From d995cde8a5fdf737a96dbce9f4088a4896e8c13e Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Wed, 30 Aug 2017 16:40:51 -0400 Subject: [PATCH 06/22] Remove duck emoji from kotlin assertion documentation --- .../src/main/kotlin/org/junit/jupiter/api/Assertions.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt b/junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt index 367b7451e7d1..e5233af64fa1 100644 --- a/junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt +++ b/junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt @@ -56,9 +56,9 @@ fun assertAll(heading: String?, vararg executables: () -> Unit) = * Example usage: * ```kotlin * val exception = assertThrows { - * throw IllegalArgumentException("Talk to a 🦆") + * throw IllegalArgumentException("Talk to a duck") * } - * assertEquals("Talk to a 🦆", exception.message) + * assertEquals("Talk to a duck", exception.message) * ``` * @see Assertions.assertThrows * @since 5.0 From bc0c5e39cde2d90e7ccf79e4127175939437dadf Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Wed, 30 Aug 2017 16:54:38 -0400 Subject: [PATCH 07/22] Use kotlin-stdlib instead of kotlin-stdlib-jre8 --- junit-jupiter-engine/junit-jupiter-engine.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/junit-jupiter-engine/junit-jupiter-engine.gradle b/junit-jupiter-engine/junit-jupiter-engine.gradle index 72be8f6a1a33..1e262fc6f247 100644 --- a/junit-jupiter-engine/junit-jupiter-engine.gradle +++ b/junit-jupiter-engine/junit-jupiter-engine.gradle @@ -34,7 +34,7 @@ dependencies { testImplementation(project(path: ':junit-platform-engine', configuration: 'testArtifacts')) testImplementation("org.assertj:assertj-core:${assertJVersion}") testImplementation("org.mockito:mockito-core:${mockitoVersion}") - testImplementation("org.jetbrains.kotlin:kotlin-stdlib-jre8") + testImplementation("org.jetbrains.kotlin:kotlin-stdlib") // Include junit-platform-console so that the JUnit Gradle plugin // uses the local version of the ConsoleLauncher. From 05f037a6303b25a730daf226482251a30b367566 Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Wed, 30 Aug 2017 17:13:09 -0400 Subject: [PATCH 08/22] Add jvmTarget as 1.8 This is an attempt to stop the IncompatibleClassChangeError on JDK 9 --- build.gradle | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/build.gradle b/build.gradle index f75f63cf905a..551ea2114423 100644 --- a/build.gradle +++ b/build.gradle @@ -167,6 +167,12 @@ allprojects { subproj -> options.compilerArgs += '-parameters' } + tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) { + kotlinOptions { + jvmTarget = rootProject.sourceCompatibility + } + } + checkstyle { toolVersion = '7.6' } From e580a045068ae5d36d37ec7f8f24f5bb0cfc0cc6 Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Wed, 30 Aug 2017 18:15:06 -0400 Subject: [PATCH 09/22] Add kotlin assertThrows that takes a message --- .../org/junit/jupiter/api/Assertions.kt | 35 +++++++++++++++++-- .../api/AssertionsAssertAllKotlinTests.kt | 2 +- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt b/junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt index e5233af64fa1..a515a49b673c 100644 --- a/junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt +++ b/junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt @@ -12,6 +12,7 @@ package org.junit.jupiter.api import org.junit.jupiter.api.function.Executable import org.junit.platform.commons.meta.API import org.junit.platform.commons.meta.API.Usage.Experimental +import java.util.function.Supplier import java.util.stream.Stream /** @@ -33,7 +34,7 @@ fun assertAll(executables: ExecutableStream) = * @since 5.0 */ @API(Experimental) -fun assertAll(heading : String?, executables: ExecutableStream) = +fun assertAll(heading: String?, executables: ExecutableStream) = Assertions.assertAll(heading, executables.convert()) /** @@ -64,5 +65,35 @@ fun assertAll(heading: String?, vararg executables: () -> Unit) = * @since 5.0 */ @API(Experimental) -inline fun assertThrows(noinline executable : () -> Unit) : T = +inline fun assertThrows(noinline executable: () -> Unit): T = Assertions.assertThrows(T::class.java, Executable(executable)) + +/** + * Example usage: + * ```kotlin + * val exception = assertThrows("Should throw an Exception") { + * throw IllegalArgumentException("Talk to a duck") + * } + * assertEquals("Talk to a duck", exception.message) + * ``` + * @see Assertions.assertThrows + * @since 5.0 + */ +@API(Experimental) +inline fun assertThrows(message: String, noinline executable: () -> Unit): T = + assertThrows({ message }, executable) + +/** + * Example usage: + * ```kotlin + * val exception = assertThrows({ "Should throw an Exception" }) { + * throw IllegalArgumentException("Talk to a duck") + * } + * assertEquals("Talk to a duck", exception.message) + * ``` + * @see Assertions.assertThrows + * @since 5.0 + */ +@API(Experimental) +inline fun assertThrows(noinline message: () -> String, noinline executable: () -> Unit): T = + Assertions.assertThrows(T::class.java, Executable(executable), Supplier(message)) diff --git a/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/AssertionsAssertAllKotlinTests.kt b/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/AssertionsAssertAllKotlinTests.kt index 7f10c10751cd..7d54a188208d 100644 --- a/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/AssertionsAssertAllKotlinTests.kt +++ b/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/AssertionsAssertAllKotlinTests.kt @@ -47,7 +47,7 @@ class AssertionsAssertAllKotlinTests { @Test fun `assertAll with streamOf functions that throw AssertionErrors`() { - val multipleFailuresError = assertThrows { + val multipleFailuresError = assertThrows("Should have thrown multiple errors") { assertAll(Stream.of({ assertFalse(true) }, { assertFalse(true) })) } assertExpectedExceptionTypes(multipleFailuresError, AssertionFailedError::class, AssertionFailedError::class) From 1a0bce3aec55740bd667ca74f97e3da362e53116 Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Wed, 30 Aug 2017 21:09:06 -0400 Subject: [PATCH 10/22] Update writing-tests section to have kotlin examples --- documentation/documentation.gradle | 2 + .../src/docs/asciidoc/writing-tests.adoc | 10 ++++ .../src/test/java/example/AssertionsDemo.java | 20 ------- .../kotlin/example/AssertionsDemoKotlin.kt | 55 +++++++++++++++++++ .../src/test/kotlin/example/Person.kt | 12 ++++ 5 files changed, 79 insertions(+), 20 deletions(-) create mode 100644 documentation/src/test/kotlin/example/AssertionsDemoKotlin.kt create mode 100644 documentation/src/test/kotlin/example/Person.kt diff --git a/documentation/documentation.gradle b/documentation/documentation.gradle index 55fb5ccd767e..5a31e0496775 100644 --- a/documentation/documentation.gradle +++ b/documentation/documentation.gradle @@ -35,6 +35,7 @@ dependencies { testImplementation(project(path: ':junit-jupiter-params', configuration: 'shadow')) testImplementation(project(':junit-platform-runner')) testImplementation(project(':junit-platform-launcher')) + testImplementation("org.jetbrains.kotlin:kotlin-stdlib") // Include junit-platform-console so that the JUnit Gradle plugin // uses the local version of the ConsoleLauncher. @@ -67,6 +68,7 @@ asciidoctor { 'revnumber' : project.version, 'mainDir': project.sourceSets.main.java.srcDirs[0], 'testDir': project.sourceSets.test.java.srcDirs[0], + 'kotlinTestDir' : project.sourceSets.test.kotlin.srcDirs[0], 'testResourcesDir': project.sourceSets.test.resources.srcDirs[0], 'outdir': outputDir.absolutePath, 'source-highlighter': 'coderay@', // TODO switch to 'rouge' once supported by the html5 backend and on MS Windows diff --git a/documentation/src/docs/asciidoc/writing-tests.adoc b/documentation/src/docs/asciidoc/writing-tests.adoc index aa4ff5a0eedc..74415b276e47 100644 --- a/documentation/src/docs/asciidoc/writing-tests.adoc +++ b/documentation/src/docs/asciidoc/writing-tests.adoc @@ -89,6 +89,16 @@ are `static` methods in the `{Assertions}` class. include::{testDir}/example/AssertionsDemo.java[tags=user_guide] ---- +Junit Jupiter also comes with a few assertion methods that lend themselves well to being +used in https://kotlinlang.org/[Kotlin]. All JUnit Jupiter Kotlin assertions are top-level +functions in the `org.junit.jupiter.api` package. + +// TODO: Change to using kotlin language highlighting after switch to rouge syntax highlighter +[source,java,indent=0] +---- +include::{kotlinTestDir}/example/AssertionsDemoKotlin.kt[tags=user_guide] +---- + [[writing-tests-assertions-third-party]] ==== Third-party Assertion Libraries diff --git a/documentation/src/test/java/example/AssertionsDemo.java b/documentation/src/test/java/example/AssertionsDemo.java index afdb29806bb3..d648fc28fffa 100644 --- a/documentation/src/test/java/example/AssertionsDemo.java +++ b/documentation/src/test/java/example/AssertionsDemo.java @@ -143,23 +143,3 @@ private static String greeting() { } // end::user_guide[] // @formatter:on - -class Person { - - private final String firstName; - private final String lastName; - - Person(String firstName, String lastName) { - this.firstName = firstName; - this.lastName = lastName; - } - - String getFirstName() { - return firstName; - } - - String getLastName() { - return lastName; - } - -} diff --git a/documentation/src/test/kotlin/example/AssertionsDemoKotlin.kt b/documentation/src/test/kotlin/example/AssertionsDemoKotlin.kt new file mode 100644 index 000000000000..3f22a33ac258 --- /dev/null +++ b/documentation/src/test/kotlin/example/AssertionsDemoKotlin.kt @@ -0,0 +1,55 @@ +/* + * Copyright 2015-2017 the original author or authors. + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution and is available at + * + * http://www.eclipse.org/legal/epl-v10.html + */ +package example + +// tag::user_guide[] +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertAll +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.assertThrows + +class AssertionsDemoKotlin { + + // end::user_guide[] + val person = Person("John", "Doe") + val people = setOf(person, Person("James", "Doe")) + + // tag::user_guide[] + @Test + fun `grouped assertions`() { + assertAll("person", + { assertEquals("John", person.firstName) }, + { assertEquals("Doe", person.lastName) } + ) + } + + @Test + fun `exception testing`() { + val exception = assertThrows ("Should throw an exception") { + throw IllegalArgumentException("a message") + } + assertEquals("a message", exception.message) + } + + @Test + fun `assertions from a stream`() { + assertAll( + "people with name starting with J", + people + .stream() + .map { + // This map returns Stream<() -> Unit> + { assertTrue(it.firstName.startsWith("J")) } + } + ) + } +} +// end::user_guide[] diff --git a/documentation/src/test/kotlin/example/Person.kt b/documentation/src/test/kotlin/example/Person.kt new file mode 100644 index 000000000000..13ddf50a33be --- /dev/null +++ b/documentation/src/test/kotlin/example/Person.kt @@ -0,0 +1,12 @@ +/* + * Copyright 2015-2017 the original author or authors. + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution and is available at + * + * http://www.eclipse.org/legal/epl-v10.html + */ +package example + +data class Person(val firstName: String, val lastName: String) From ee5db7d4dc664681e45cdf98f6fac24001ff1ca4 Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Wed, 30 Aug 2017 23:00:29 -0400 Subject: [PATCH 11/22] Clarify comment in AssertionsDemoKotlin --- documentation/src/test/kotlin/example/AssertionsDemoKotlin.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/documentation/src/test/kotlin/example/AssertionsDemoKotlin.kt b/documentation/src/test/kotlin/example/AssertionsDemoKotlin.kt index 3f22a33ac258..af3f40453421 100644 --- a/documentation/src/test/kotlin/example/AssertionsDemoKotlin.kt +++ b/documentation/src/test/kotlin/example/AssertionsDemoKotlin.kt @@ -46,7 +46,7 @@ class AssertionsDemoKotlin { people .stream() .map { - // This map returns Stream<() -> Unit> + // This mapping returns Stream<() -> Unit> { assertTrue(it.firstName.startsWith("J")) } } ) From 514318ca2658f488eacacaaa264e171ca55021fc Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Thu, 31 Aug 2017 11:49:45 -0400 Subject: [PATCH 12/22] Add release-notes-5.0.0-GA with mention of Kotlin assertions --- .../docs/asciidoc/release-notes-5.0.0-GA.adoc | 28 +++++++++++++++++++ .../src/docs/asciidoc/release-notes.adoc | 2 ++ 2 files changed, 30 insertions(+) create mode 100644 documentation/src/docs/asciidoc/release-notes-5.0.0-GA.adoc diff --git a/documentation/src/docs/asciidoc/release-notes-5.0.0-GA.adoc b/documentation/src/docs/asciidoc/release-notes-5.0.0-GA.adoc new file mode 100644 index 000000000000..11de53259897 --- /dev/null +++ b/documentation/src/docs/asciidoc/release-notes-5.0.0-GA.adoc @@ -0,0 +1,28 @@ +[[release-notes-5.0.0-GA]] +=== 5.0.0-GA + +*Date of Release:* Unreleased + +*Scope:* Addition of Kotlin assertion helpers + + +[[release-notes-5.0.0-GA-junit-platform]] +==== JUnit Platform + +No changes. + + +[[release-notes-5.0.0-GA-junit-jupiter]] +==== JUnit Jupiter + +===== New Features and Improvements + +* New Kotlin friendly assertions added as top-level functions in the `org.junit.jupiter.api` package. +** `assertAll` that takes `Stream<() -> Unit>` or `vararg () -> Unit`. +** `assertThrow` that uses Kotlin reified generics. + + +[[release-notes-5.0.0-GA-junit-vintage]] +==== JUnit Vintage + +No changes. diff --git a/documentation/src/docs/asciidoc/release-notes.adoc b/documentation/src/docs/asciidoc/release-notes.adoc index a92adffe1eb9..3210929c25b1 100644 --- a/documentation/src/docs/asciidoc/release-notes.adoc +++ b/documentation/src/docs/asciidoc/release-notes.adoc @@ -23,4 +23,6 @@ include::release-notes-5.0.0-RC2.adoc[] include::release-notes-5.0.0-RC3.adoc[] +include::release-notes-5.0.0-GA.adoc[] + :numbered: From 869d4ebdc2a25602979f5145472c7961e5e343e7 Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Thu, 31 Aug 2017 14:54:10 -0400 Subject: [PATCH 13/22] Fix problem with javdoc generation under JDK 9 See https://youtrack.jetbrains.com/issue/KT-20025 --- .../src/main/kotlin/org/junit/jupiter/api/Assertions.kt | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt b/junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt index a515a49b673c..fdeb19cabd8f 100644 --- a/junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt +++ b/junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt @@ -96,4 +96,11 @@ inline fun assertThrows(message: String, noinline execut */ @API(Experimental) inline fun assertThrows(noinline message: () -> String, noinline executable: () -> Unit): T = - Assertions.assertThrows(T::class.java, Executable(executable), Supplier(message)) + Assertions.assertThrows(T::class.java, Executable(executable), Supplier { + /* + * This is a hacky workaround due to a bug in how the JDK 9 JavaDoc code generator interacts with the + * generated Kotlin Bytecode. + * https://youtrack.jetbrains.com/issue/KT-20025 + */ + message() + }) From c638df6048d9aee3666f8b293485d7eacb4d811b Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Thu, 31 Aug 2017 15:15:37 -0400 Subject: [PATCH 14/22] Add a sanity test for the Kotlin assertThrows method --- .../jupiter/api/AssertionsAssertAllKotlinTests.kt | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/AssertionsAssertAllKotlinTests.kt b/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/AssertionsAssertAllKotlinTests.kt index 7d54a188208d..9f41e4af96dc 100644 --- a/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/AssertionsAssertAllKotlinTests.kt +++ b/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/AssertionsAssertAllKotlinTests.kt @@ -11,6 +11,8 @@ package org.junit.jupiter.api import org.junit.jupiter.api.Assertions.assertFalse import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.AssertionTestUtils.assertMessageStartsWith +import org.junit.jupiter.api.AssertionTestUtils.expectAssertionFailedError import org.opentest4j.AssertionFailedError import org.opentest4j.MultipleFailuresError import java.util.stream.Stream @@ -53,6 +55,17 @@ class AssertionsAssertAllKotlinTests { assertExpectedExceptionTypes(multipleFailuresError, AssertionFailedError::class, AssertionFailedError::class) } + @Test + fun `assertThrows that does not have exception thrown does not throw KotlinNullPointer`() { + val assertionMessage = "This will not throw an exception" + val error = assertThrows("assertThrows did not throw the correct exception") { + assertThrows(assertionMessage) { } + // This should never execute + expectAssertionFailedError() + } + assertMessageStartsWith(error, assertionMessage) + } + companion object { fun assertExpectedExceptionTypes( multipleFailuresError: MultipleFailuresError, From 6bf57fa06fd853a5f226dc6b114abdbd5c7e9662 Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Thu, 31 Aug 2017 15:42:00 -0400 Subject: [PATCH 15/22] Cleanup JavaDocs to point out additional Kotlin assertions that exist --- .../src/main/java/org/junit/jupiter/api/Assertions.java | 3 +++ .../org/junit/jupiter/api/AssertionsAssertAllKotlinTests.kt | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/junit-jupiter-api/src/main/java/org/junit/jupiter/api/Assertions.java b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/Assertions.java index fd958065912c..ffa74c9bbcd7 100644 --- a/junit-jupiter-api/src/main/java/org/junit/jupiter/api/Assertions.java +++ b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/Assertions.java @@ -30,6 +30,9 @@ * {@code Assertions} is a collection of utility methods that support asserting * conditions in tests. * + *

Additional Kotlin assertions can be found + * as top-level functions on the {@link org.junit.jupiter.api} package. + * *

Unless otherwise noted, a failed assertion will throw an * {@link org.opentest4j.AssertionFailedError} or a subclass thereof. * diff --git a/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/AssertionsAssertAllKotlinTests.kt b/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/AssertionsAssertAllKotlinTests.kt index 9f41e4af96dc..ff040a12d7aa 100644 --- a/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/AssertionsAssertAllKotlinTests.kt +++ b/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/AssertionsAssertAllKotlinTests.kt @@ -19,7 +19,7 @@ import java.util.stream.Stream import kotlin.reflect.KClass /** - * Unit tests for JUnit Jupiter {@link Assertions}. + * Unit tests for JUnit Jupiter [org.junit.jupiter.api] top-level assertion functions. * * @since 5.0 */ From b3ab661c7b3877198bf4607399e82a87bad8e246 Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Thu, 14 Sep 2017 18:45:12 -0400 Subject: [PATCH 16/22] Cleanup Kolin Assertion PR code post merge to master --- build.gradle | 2 +- .../kotlin/example/AssertionsDemoKotlin.kt | 4 ++-- .../src/test/kotlin/example/Person.kt | 4 ++-- .../org/junit/jupiter/api/Assertions.kt | 23 ++++--------------- .../api/AssertionsAssertAllKotlinTests.kt | 4 ++-- .../kotlin/InstancePerClassKotlinTestCase.kt | 4 ++-- .../kotlin/InstancePerMethodKotlinTestCase.kt | 4 ++-- 7 files changed, 16 insertions(+), 29 deletions(-) diff --git a/build.gradle b/build.gradle index a6de216f3377..c421aef9296a 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,7 @@ buildscript { maven { url 'https://oss.sonatype.org/content/repositories/snapshots' } } dependencies { - classpath 'com.diffplug.spotless:spotless-plugin-gradle:3.5.1' + classpath 'com.diffplug.spotless:spotless-plugin-gradle:3.5.2' classpath 'com.github.ben-manes:gradle-versions-plugin:0.15.0' classpath 'org.ajoberstar:gradle-git:1.7.2' classpath 'org.ajoberstar:gradle-git-publish:0.3.0' diff --git a/documentation/src/test/kotlin/example/AssertionsDemoKotlin.kt b/documentation/src/test/kotlin/example/AssertionsDemoKotlin.kt index af3f40453421..7c69a0199f7c 100644 --- a/documentation/src/test/kotlin/example/AssertionsDemoKotlin.kt +++ b/documentation/src/test/kotlin/example/AssertionsDemoKotlin.kt @@ -2,10 +2,10 @@ * Copyright 2015-2017 the original author or authors. * * All rights reserved. This program and the accompanying materials are - * made available under the terms of the Eclipse Public License v1.0 which + * made available under the terms of the Eclipse Public License v2.0 which * accompanies this distribution and is available at * - * http://www.eclipse.org/legal/epl-v10.html + * http://www.eclipse.org/legal/epl-v20.html */ package example diff --git a/documentation/src/test/kotlin/example/Person.kt b/documentation/src/test/kotlin/example/Person.kt index 13ddf50a33be..d57ae4afd0e5 100644 --- a/documentation/src/test/kotlin/example/Person.kt +++ b/documentation/src/test/kotlin/example/Person.kt @@ -2,10 +2,10 @@ * Copyright 2015-2017 the original author or authors. * * All rights reserved. This program and the accompanying materials are - * made available under the terms of the Eclipse Public License v1.0 which + * made available under the terms of the Eclipse Public License v2.0 which * accompanies this distribution and is available at * - * http://www.eclipse.org/legal/epl-v10.html + * http://www.eclipse.org/legal/epl-v20.html */ package example diff --git a/junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt b/junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt index fdeb19cabd8f..409ffaeb6e5f 100644 --- a/junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt +++ b/junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt @@ -2,16 +2,17 @@ * Copyright 2015-2017 the original author or authors. * * All rights reserved. This program and the accompanying materials are - * made available under the terms of the Eclipse Public License v1.0 which + * made available under the terms of the Eclipse Public License v2.0 which * accompanies this distribution and is available at * - * http://www.eclipse.org/legal/epl-v10.html + * http://www.eclipse.org/legal/epl-v20.html */ +@file:API(status = EXPERIMENTAL, since = "5.0.1") package org.junit.jupiter.api +import org.apiguardian.api.API +import org.apiguardian.api.API.Status.EXPERIMENTAL import org.junit.jupiter.api.function.Executable -import org.junit.platform.commons.meta.API -import org.junit.platform.commons.meta.API.Usage.Experimental import java.util.function.Supplier import java.util.stream.Stream @@ -23,33 +24,25 @@ internal fun ExecutableStream.convert() = map { Executable(it) } /** * @see Assertions.assertAll - * @since 5.0 */ -@API(Experimental) fun assertAll(executables: ExecutableStream) = Assertions.assertAll(executables.convert()) /** * @see Assertions.assertAll - * @since 5.0 */ -@API(Experimental) fun assertAll(heading: String?, executables: ExecutableStream) = Assertions.assertAll(heading, executables.convert()) /** * @see Assertions.assertAll - * @since 5.0 */ -@API(Experimental) fun assertAll(vararg executables: () -> Unit) = assertAll(executables.toList().stream()) /** * @see Assertions.assertAll - * @since 5.0 */ -@API(Experimental) fun assertAll(heading: String?, vararg executables: () -> Unit) = assertAll(heading, executables.toList().stream()) @@ -62,9 +55,7 @@ fun assertAll(heading: String?, vararg executables: () -> Unit) = * assertEquals("Talk to a duck", exception.message) * ``` * @see Assertions.assertThrows - * @since 5.0 */ -@API(Experimental) inline fun assertThrows(noinline executable: () -> Unit): T = Assertions.assertThrows(T::class.java, Executable(executable)) @@ -77,9 +68,7 @@ inline fun assertThrows(noinline executable: () -> Unit) * assertEquals("Talk to a duck", exception.message) * ``` * @see Assertions.assertThrows - * @since 5.0 */ -@API(Experimental) inline fun assertThrows(message: String, noinline executable: () -> Unit): T = assertThrows({ message }, executable) @@ -92,9 +81,7 @@ inline fun assertThrows(message: String, noinline execut * assertEquals("Talk to a duck", exception.message) * ``` * @see Assertions.assertThrows - * @since 5.0 */ -@API(Experimental) inline fun assertThrows(noinline message: () -> String, noinline executable: () -> Unit): T = Assertions.assertThrows(T::class.java, Executable(executable), Supplier { /* diff --git a/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/AssertionsAssertAllKotlinTests.kt b/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/AssertionsAssertAllKotlinTests.kt index ff040a12d7aa..f548d73cceb1 100644 --- a/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/AssertionsAssertAllKotlinTests.kt +++ b/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/AssertionsAssertAllKotlinTests.kt @@ -2,10 +2,10 @@ * Copyright 2015-2017 the original author or authors. * * All rights reserved. This program and the accompanying materials are - * made available under the terms of the Eclipse Public License v1.0 which + * made available under the terms of the Eclipse Public License v2.0 which * accompanies this distribution and is available at * - * http://www.eclipse.org/legal/epl-v10.html + * http://www.eclipse.org/legal/epl-v20.html */ package org.junit.jupiter.api diff --git a/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/engine/kotlin/InstancePerClassKotlinTestCase.kt b/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/engine/kotlin/InstancePerClassKotlinTestCase.kt index 2ffaab4b92b8..99bd5d29f9d6 100644 --- a/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/engine/kotlin/InstancePerClassKotlinTestCase.kt +++ b/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/engine/kotlin/InstancePerClassKotlinTestCase.kt @@ -2,10 +2,10 @@ * Copyright 2015-2017 the original author or authors. * * All rights reserved. This program and the accompanying materials are - * made available under the terms of the Eclipse Public License v1.0 which + * made available under the terms of the Eclipse Public License v2.0 which * accompanies this distribution and is available at * - * http://www.eclipse.org/legal/epl-v10.html + * http://www.eclipse.org/legal/epl-v20.html */ package org.junit.jupiter.engine.kotlin diff --git a/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/engine/kotlin/InstancePerMethodKotlinTestCase.kt b/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/engine/kotlin/InstancePerMethodKotlinTestCase.kt index d6abdf21666b..b9fbd349ba86 100644 --- a/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/engine/kotlin/InstancePerMethodKotlinTestCase.kt +++ b/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/engine/kotlin/InstancePerMethodKotlinTestCase.kt @@ -2,10 +2,10 @@ * Copyright 2015-2017 the original author or authors. * * All rights reserved. This program and the accompanying materials are - * made available under the terms of the Eclipse Public License v1.0 which + * made available under the terms of the Eclipse Public License v2.0 which * accompanies this distribution and is available at * - * http://www.eclipse.org/legal/epl-v10.html + * http://www.eclipse.org/legal/epl-v20.html */ package org.junit.jupiter.engine.kotlin From efebdd8561817b18a954546ca2999e7261972f33 Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Fri, 15 Sep 2017 19:48:22 -0400 Subject: [PATCH 17/22] Change Assertions.kt to be versioned since 5.1 --- .../src/main/kotlin/org/junit/jupiter/api/Assertions.kt | 2 +- .../org/junit/jupiter/api/AssertionsAssertAllKotlinTests.kt | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt b/junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt index 409ffaeb6e5f..ffdfcf4244bc 100644 --- a/junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt +++ b/junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt @@ -7,7 +7,7 @@ * * http://www.eclipse.org/legal/epl-v20.html */ -@file:API(status = EXPERIMENTAL, since = "5.0.1") +@file:API(status = EXPERIMENTAL, since = "5.1") package org.junit.jupiter.api import org.apiguardian.api.API diff --git a/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/AssertionsAssertAllKotlinTests.kt b/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/AssertionsAssertAllKotlinTests.kt index f548d73cceb1..d54a2813ef18 100644 --- a/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/AssertionsAssertAllKotlinTests.kt +++ b/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/AssertionsAssertAllKotlinTests.kt @@ -20,8 +20,6 @@ import kotlin.reflect.KClass /** * Unit tests for JUnit Jupiter [org.junit.jupiter.api] top-level assertion functions. - * - * @since 5.0 */ class AssertionsAssertAllKotlinTests { From 2007a8969192e52ba7036e28809335ac2ce87b4e Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Fri, 6 Oct 2017 10:10:26 -0400 Subject: [PATCH 18/22] Resolve type signature change in AssertionsAssertAllKotlinTests --- .../org/junit/jupiter/api/AssertionsAssertAllKotlinTests.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/AssertionsAssertAllKotlinTests.kt b/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/AssertionsAssertAllKotlinTests.kt index d54a2813ef18..60ee0e7a3526 100644 --- a/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/AssertionsAssertAllKotlinTests.kt +++ b/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/AssertionsAssertAllKotlinTests.kt @@ -68,7 +68,7 @@ class AssertionsAssertAllKotlinTests { fun assertExpectedExceptionTypes( multipleFailuresError: MultipleFailuresError, vararg exceptionTypes: KClass) = - AssertionsAssertAllTests.assertExpectedExceptionTypes( + AssertAllAssertionsTests.assertExpectedExceptionTypes( multipleFailuresError, *exceptionTypes.map { it.java }.toTypedArray()) } } From 99b7569886eb4c9d481df07cb99e4b109e9c495a Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Thu, 9 Nov 2017 09:55:02 -0500 Subject: [PATCH 19/22] Reformat existing kotlin sources to use spaces instead of tabs --- .../kotlin/InstancePerClassKotlinTestCase.kt | 78 +++++++++--------- .../kotlin/InstancePerMethodKotlinTestCase.kt | 82 +++++++++---------- 2 files changed, 80 insertions(+), 80 deletions(-) diff --git a/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/engine/kotlin/InstancePerClassKotlinTestCase.kt b/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/engine/kotlin/InstancePerClassKotlinTestCase.kt index 0dc93e405360..99bd5d29f9d6 100644 --- a/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/engine/kotlin/InstancePerClassKotlinTestCase.kt +++ b/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/engine/kotlin/InstancePerClassKotlinTestCase.kt @@ -20,43 +20,43 @@ import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS @TestInstance(PER_CLASS) class InstancePerClassKotlinTestCase { - companion object { - @JvmField - val TEST_INSTANCES: MutableMap> = HashMap() - } - - @BeforeAll - fun beforeAll() { - increment("beforeAll") - } - - @BeforeEach - fun beforeEach() { - increment("beforeEach") - } - - @AfterEach - fun afterEach() { - increment("afterEach") - } - - @AfterAll - fun afterAll() { - increment("afterAll") - } - - @Test - fun firstTest() { - increment("test") - } - - @Test - fun secondTest() { - increment("test") - } - - private fun increment(name: String) { - TEST_INSTANCES.computeIfAbsent(this, { _ -> HashMap() }) - .compute(name, { _, oldValue -> (oldValue ?: 0) + 1 }) - } + companion object { + @JvmField + val TEST_INSTANCES: MutableMap> = HashMap() + } + + @BeforeAll + fun beforeAll() { + increment("beforeAll") + } + + @BeforeEach + fun beforeEach() { + increment("beforeEach") + } + + @AfterEach + fun afterEach() { + increment("afterEach") + } + + @AfterAll + fun afterAll() { + increment("afterAll") + } + + @Test + fun firstTest() { + increment("test") + } + + @Test + fun secondTest() { + increment("test") + } + + private fun increment(name: String) { + TEST_INSTANCES.computeIfAbsent(this, { _ -> HashMap() }) + .compute(name, { _, oldValue -> (oldValue ?: 0) + 1 }) + } } diff --git a/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/engine/kotlin/InstancePerMethodKotlinTestCase.kt b/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/engine/kotlin/InstancePerMethodKotlinTestCase.kt index 4d317a2128e7..b9fbd349ba86 100644 --- a/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/engine/kotlin/InstancePerMethodKotlinTestCase.kt +++ b/junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/engine/kotlin/InstancePerMethodKotlinTestCase.kt @@ -17,45 +17,45 @@ import org.junit.jupiter.api.Test class InstancePerMethodKotlinTestCase { - companion object { - @JvmField - val TEST_INSTANCES: MutableMap> = LinkedHashMap() - - @JvmStatic - @BeforeAll - fun beforeAll() { - increment(this, "beforeAll") - } - - @JvmStatic - @AfterAll - fun afterAll() { - increment(this, "afterAll") - } - - private fun increment(instance: Any, name: String) { - TEST_INSTANCES.computeIfAbsent(instance, { _ -> LinkedHashMap() }) - .compute(name, { _, oldValue -> (oldValue ?: 0) + 1 }) - } - } - - @BeforeEach - fun beforeEach() { - increment(this, "beforeEach") - } - - @AfterEach - fun afterEach() { - increment(this, "afterEach") - } - - @Test - fun firstTest() { - increment(this, "test") - } - - @Test - fun secondTest() { - increment(this, "test") - } + companion object { + @JvmField + val TEST_INSTANCES: MutableMap> = LinkedHashMap() + + @JvmStatic + @BeforeAll + fun beforeAll() { + increment(this, "beforeAll") + } + + @JvmStatic + @AfterAll + fun afterAll() { + increment(this, "afterAll") + } + + private fun increment(instance: Any, name: String) { + TEST_INSTANCES.computeIfAbsent(instance, { _ -> LinkedHashMap() }) + .compute(name, { _, oldValue -> (oldValue ?: 0) + 1 }) + } + } + + @BeforeEach + fun beforeEach() { + increment(this, "beforeEach") + } + + @AfterEach + fun afterEach() { + increment(this, "afterEach") + } + + @Test + fun firstTest() { + increment(this, "test") + } + + @Test + fun secondTest() { + increment(this, "test") + } } From 0fa5c983bcb0339f19f079654ebc90d11aeae987 Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Thu, 9 Nov 2017 10:03:28 -0500 Subject: [PATCH 20/22] Use jvmTarget of 1.8 for KotlinCompile --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index e8cb97b64b01..4845654327b9 100644 --- a/build.gradle +++ b/build.gradle @@ -189,7 +189,7 @@ allprojects { subproj -> tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) { kotlinOptions { - jvmTarget = rootProject.sourceCompatibility + jvmTarget = '1.8' } } From e226327eac12df1181d3fc78bef76aa2e77bfa25 Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Fri, 10 Nov 2017 14:44:44 -0500 Subject: [PATCH 21/22] Typo: Junit -> JUnit --- documentation/src/docs/asciidoc/writing-tests.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/documentation/src/docs/asciidoc/writing-tests.adoc b/documentation/src/docs/asciidoc/writing-tests.adoc index 96faf6928b07..ba1aab67f205 100644 --- a/documentation/src/docs/asciidoc/writing-tests.adoc +++ b/documentation/src/docs/asciidoc/writing-tests.adoc @@ -94,7 +94,7 @@ are `static` methods in the `{Assertions}` class. include::{testDir}/example/AssertionsDemo.java[tags=user_guide] ---- -Junit Jupiter also comes with a few assertion methods that lend themselves well to being +JUnit Jupiter also comes with a few assertion methods that lend themselves well to being used in https://kotlinlang.org/[Kotlin]. All JUnit Jupiter Kotlin assertions are top-level functions in the `org.junit.jupiter.api` package. From 6fdbf08b5bddcd7db8d43c99b9180e4c9db11f4d Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Mon, 13 Nov 2017 11:56:19 -0500 Subject: [PATCH 22/22] Make ExecutableStream private --- .../src/main/kotlin/org/junit/jupiter/api/Assertions.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt b/junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt index ffdfcf4244bc..be0aee1a2a0d 100644 --- a/junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt +++ b/junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt @@ -19,8 +19,8 @@ import java.util.stream.Stream /** * [Stream] of functions to be executed. */ -internal typealias ExecutableStream = Stream<() -> Unit> -internal fun ExecutableStream.convert() = map { Executable(it) } +private typealias ExecutableStream = Stream<() -> Unit> +private fun ExecutableStream.convert() = map { Executable(it) } /** * @see Assertions.assertAll