ajurasz / micronaut-test

Repository for Test Related Utilities for Micronaut

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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
        );
    }
}

About

Repository for Test Related Utilities for Micronaut

License:Apache License 2.0


Languages

Language:CSS 42.7%Language:Java 36.3%Language:Kotlin 9.1%Language:Groovy 5.2%Language:HTML 4.0%Language:Shell 1.6%Language:JavaScript 1.2%