Skip to content

blindpirate/junit5-unroll-extension

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JUnit 5 Unroll Extension for Kotlin

Build Status Apache License 2

JUnit 5 Unroll Extension is a JUnit 5 extension which supports parameterized tests in kotlin. TL;DR:

import com.github.blindpirate.junit.extension.unroll.Param
import com.github.blindpirate.junit.extension.unroll.Unroll
import com.github.blindpirate.junit.extension.unroll.where
import java.lang.Math

class Math {
    @Unroll
    fun `max number of {0} and {1} is {2}`(
            a: Int, b: Int, c: Int, param: Param = where {
                1 _ 3 _ 3
                7 _ 4 _ 7 
                0 _ 0 _ 0
            }) {
        assert(Math.max(a, b) == c)
    }
}

yields the following result:

max number of {0} and {1} is {2}(int, int, int, com.github.blindpirate.Param) ✔
├─ max number of 0 and 0 is 0 ✔
├─ max number of 1 and 3 is 3 ✔
└─ max number of 7 and 4 is 7 ✔

How to use

Maven:

<dependency>
    <groupId>com.github.blindpirate</groupId>
    <artifactId>junit5-unroll-extension</artifactId>
    <version>0.1.2</version>
    <scope>test</scope>
</dependency>

Gradle:

repositories {
    jcenter()
}

dependencies {
    testCompile 'com.github.blindpirate:junit5-unroll-extension:0.1.2'
}
  • Add configuration to your pom.xml or build.gradle.
  • Add @Unroll to your test method. Note @Unroll can't be used together with @Test or @ParamerizedTest.
  • Put the arguments in the where function as shown above.

Why Unroll Extension

This extension is greatly inspired by Spock. If you have used Spock, you may be impressed by its powerful Data Driven Test:

class Math extends Specification {
    def "maximum of two numbers"(int a, int b, int c) {
        expect:
        Math.max(a, b) == c

        where:
        a | b | c
        1 | 3 | 3
        7 | 4 | 7 
        0 | 0 | 0
    }
}

Other frameworks have similar features:

JUnit 4 Parmeterized Tests:

@RunWith(Parameterized.class)
public class FibonacciTest {
    @Parameters
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][] {
                 { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 }  
           });
    }

    @Parameter // first data value (0) is default
    public /* NOT private */ int fInput;

    @Parameter(1)
    public /* NOT private */ int fExpected;

    @Test
    public void test() {
        assertEquals(fExpected, Fibonacci.compute(fInput));
    }
}

JUnit 5 Parameterized Tests:

@ParameterizedTest
@ValueSource(ints = { 1, 2, 3 })
void testWithValueSource(int argument) {
    assertTrue(argument > 0 && argument < 4);
}

However, you can hardly add anything other than strings and numbers into @ValueSource. Extra ArgumentsProvider and ArgumentConverter make things much more complex.

With this extension, you can have your parameters as they are.