Skip to content

Commit

Permalink
Tests use classes, not objects
Browse files Browse the repository at this point in the history
JUnit 5 does some reflectomagic to violate Java visibility rules when
instantiating test classes, and so breaks the singleton-ness of
Kotlin objects.
  • Loading branch information
npryce authored and dmcg committed Nov 13, 2018
1 parent fd2e166 commit 826d59a
Show file tree
Hide file tree
Showing 20 changed files with 39 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.TestFactory


object DynamicTests {
class DynamicTests {

data class Fixture(
var fruit: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.TestFactory


object FixtureSupplyingTests {
class FixtureSupplyingTests {

@TestFactory fun `supply fixture at top`() = junitTests("banana") {
context("parent had no fixture") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import java.io.FileNotFoundException
import kotlin.streams.asSequence


object FixtureTests {
class FixtureTests {

data class Fixture(
var fruit: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.TestFactory


object ImmutableTests {
class ImmutableTests {

@TestFactory fun `before and after`() = junitTests<List<String>> {
fixture { emptyList() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.oneeyedmen.minutest.junit.junitTests
import org.junit.jupiter.api.Assertions.assertEquals


object NamingTests {
class NamingTests {

@org.junit.jupiter.api.Test
fun `fully qualified name`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.TestFactory


object NullableFixtureTests {
class NullableFixtureTests {

@TestFactory fun `nullable String`() = junitTests<String?>(null) {
test("fixture is null") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.fail


object TransformTests {
class TransformTests {
@Test
fun `transforms wrap around application of before and after blocks`() {
val log = mutableListOf<String>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private fun TestContext<MutableCollection<String>>.behavesAsMutableCollection(

// Now tests can invoke the function to verify the contract in a context

object ArrayListTests : JupiterTests {
class ArrayListTests : JupiterTests {

override val tests = context<MutableCollection<String>> {
behavesAsMutableCollection("ArrayList") { ArrayList() }
Expand All @@ -43,7 +43,7 @@ object ArrayListTests : JupiterTests {
// We can reuse the contract for different collections.

// Here we use the convenience InlineJupiterTests to reduce boilerplate
object LinkedListTests : InlineJupiterTests<MutableCollection<String>>({
class LinkedListTests : InlineJupiterTests<MutableCollection<String>>({

behavesAsMutableCollection("LinkedList") { LinkedList() }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.oneeyedmen.minutest.junit.context
import org.junit.jupiter.api.Assertions.assertEquals


object DerivedContextExampleTests : JupiterTests {
class DerivedContextExampleTests : JupiterTests {

data class Fixture(val fruit: String)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import com.oneeyedmen.minutest.junit.context
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNotEquals

// Minutests are usually defined in a object.
// Implement JupiterTests to have them run by JUnit 5
object FirstMinutests : JupiterTests {
// Implement JupiterTests to run Minutests with JUnit 5
class FirstMinutests : JupiterTests {

// tests are grouped in a context
override val tests = context<Unit> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.oneeyedmen.minutest.junit.context
import org.junit.jupiter.api.Assertions.assertEquals
import java.util.*

object FixtureExampleTests : JupiterTests {
class FixtureExampleTests : JupiterTests {

// We have multiple state, so make a separate fixture class
class Fixture {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private fun TestContext<StringStack>.cantPop() = test("cant pop") {
}

// In order to give multiple sets of tests, in this example we are using JUnit @TestFactory functions
object GeneratingExampleTests {
class GeneratingExampleTests {

// JUnit will run the tests from annotated functions
@TestFactory fun `stack tests`() = junitTests<StringStack> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.oneeyedmen.minutest.junit.context
import org.junit.jupiter.api.Assertions.assertEquals


object ImmutableExampleTests : JupiterTests {
class ImmutableExampleTests : JupiterTests {

// If you like this FP stuff, you may want to test an immutable fixture.
override val tests = context<List<String>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.rules.TemporaryFolder


object JunitRulesExampleTests : JupiterTests {
class JunitRulesExampleTests : JupiterTests {

class Fixture {
// make rules part of the fixture, no need for an annotation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.oneeyedmen.minutest.junit.context
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue

object ParameterisedTests : JupiterTests {
class ParameterisedTests : JupiterTests {

override val tests = context<Unit> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import java.util.*

object SimpleStackExampleTests : JupiterTests {
class SimpleStackExampleTests : JupiterTests {

// The fixture type is the generic type of the test, here Stack<String>
override val tests = context<Stack<String>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.assertThrows
import java.util.*

object StackExampleTests : JupiterTests {
class StackExampleTests : JupiterTests {

override val tests = context<Stack<String>> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.junit.jupiter.api.Assertions.assertTrue

// A translation of FizzBuzz tested with JUnit theories -
// http://www.oneeyedmen.com/tdd-v-testing-part2.html
object TheoriesExampleTests : JupiterTests {
class TheoriesExampleTests : JupiterTests {

override val tests = context<Unit> {
(1..31).forEach { i ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.junit.runner.Description

private val log = mutableListOf<String>()

object JunitRulesTests {
class JunitRulesTests {
class TestRule : TestWatcher() {
var testDescription: String? = null

Expand Down Expand Up @@ -48,15 +48,19 @@ object JunitRulesTests {
log.add(rule.testDescription.toString())
}
}

@JvmStatic @AfterAll fun checkTestIsRun() {
assertEquals(
listOf(
"test 1",
"outer.apply rule fixture class.test 1(com.oneeyedmen.minutest.junit.JunitRulesTests)",
"test 2",
"outer.apply rule test class.test 2(com.oneeyedmen.minutest.junit.JunitRulesTests)"),
log)

companion object {
@JvmStatic
@AfterAll
fun checkTestIsRun() {
assertEquals(
listOf(
"test 1",
"outer.apply rule fixture class.test 1(com.oneeyedmen.minutest.junit.JunitRulesTests)",
"test 2",
"outer.apply rule test class.test 2(com.oneeyedmen.minutest.junit.JunitRulesTests)"),
log)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.junit.jupiter.api.Assertions.assertTrue
import java.util.*


object JupiterTestsWithPlainFixture : JupiterTests {
class JupiterTestsWithPlainFixture : JupiterTests {

override val tests = context<String> {
fixture { "banana" }
Expand All @@ -16,7 +16,7 @@ object JupiterTestsWithPlainFixture : JupiterTests {
}
}

object JupiterTestsWithGenericFixture : JupiterTests {
class JupiterTestsWithGenericFixture : JupiterTests {

override val tests = context<Stack<String>> {
fixture { Stack() }
Expand All @@ -27,7 +27,7 @@ object JupiterTestsWithGenericFixture : JupiterTests {
}
}

object JupiterTestsWithNullableFixture : JupiterTests {
class JupiterTestsWithNullableFixture : JupiterTests {

override val tests = context<String?> {
fixture { "banana" }
Expand All @@ -39,7 +39,7 @@ object JupiterTestsWithNullableFixture : JupiterTests {
}
}

object JupiterTestsWithSuppliedFixture : JupiterTests {
class JupiterTestsWithSuppliedFixture : JupiterTests {

override val tests = context("banana") {

Expand All @@ -49,7 +49,7 @@ object JupiterTestsWithSuppliedFixture : JupiterTests {
}
}

object InlineJupiterTestsTests : InlineJupiterTests<String>({
class InlineJupiterTestsTests : InlineJupiterTests<String>({
fixture { "banana" }

test("test") {
Expand Down

0 comments on commit 826d59a

Please sign in to comment.