Skip to content

SealedClassesSource for Parameterized tests for Junit5

License

Notifications You must be signed in to change notification settings

dpreussler/junit5-kotlin

Repository files navigation

Build Status

Maven Central Jitpack

junit5-kotlin

Extensions for Junit5 for Kotlin programming language

SealedClassesSource

Creates instances of sealed classes for @Parametrized tests

Usage:

@ParameterizedTest
@SealedClassesSource
fun test(item: SomeClass)

Can handle:

  • nested sealed classes
  • singletons object
  • empty constructors
  • constructors made out of primitive types

You can pass in a TypeFactory for creating custom instances like for mocking

You can extend the DefaultTypeFactory for creating custom instances while reusing the creation of constructors with single paramter constructors with basic types and empty constructors.

sealed class Fruit {
  object Orange : Fruit()
  object Banana : Fruit()
  data class Apple(color: String) : Fruit()
}

class FruitTypeFactory : DefaultTypeFactory() {
    override fun create(what: KClass<*>) = when (what) {
        Apple::class -> Fruit.Apple(
            color = "red"
        )
        else -> super.create(what)
    }
}

Filter:

You can filter out a few values by using the names attribute.

sealed class Family {
    class Mother: Family()
    class Father: Family()
    sealed class Children: Family() {
        class Son: Children()
        class Daughter: Children()
        sealed class GrandChildren: Children() {
            class GrandSon: GrandChildren()
            class GradDaughter: GrandChildren()
        }
    }
}

@ParameterizedTest
@SealedClassesSource(names = ["Mother", "Daughter", "GrandSon"])
fun test(item: Family)

Or you can turn this around by setting the mode attribute to EXCLUDE

@ParameterizedTest
@SealedClassesSource(names = ["Mother", "Daughter", "GrandSon"], mode = SealedClassesSource.Mode.EXCLUDE)
fun test(item: Family)

Get it:

dependencies {
    implementation 'de.jodamob.junit5:junit5-kotlin:0.0.3'
}

License

The MIT License (MIT)

Copyright (c) 2020 Danny Preussler

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

SealedClassesSource for Parameterized tests for Junit5

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published