/micronaut-test

Repository for Test Related Utilities for Micronaut

Primary LanguageCSSApache License 2.0Apache-2.0

Spock: Maven Central JUnit 5: Maven Central

Micronaut Test

This project provides testing extension for JUnit 5 and Spock to make it easier to test Micronaut applications.

For more information see the Documentation.

Example Spock Test:

import io.micronaut.test.annotation.MicronautTest
import spock.lang.*
import javax.inject.Inject

@MicronautTest 
class MathServiceSpec extends Specification {

    @Inject
    MathService mathService // 

    @Unroll
    void "should compute #num times 4"() { 
        when:
        def result = mathService.compute(num)

        then:
        result == expected

        where:
        num | expected
        2   | 8
        3   | 12
    }
}

Example JUnit 5 Test:

import io.micronaut.test.annotation.MicronautTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import javax.inject.Inject;


@MicronautTest // <1>
class MathServiceTest {

    @Inject
    MathService mathService; // <2>


    @ParameterizedTest
    @CsvSource({"2,8", "3,12"})
    void testComputeNumToSquare(Integer num, Integer square) {
        final Integer result = mathService.compute(num); // <3>

        Assertions.assertEquals(
                square,
                result
        );
    }
}