Introduction to Micronaut 1 ~ Introduction ~ continued Perform a unit test by slightly extending the HelloController created last time. Use the Micronaut extension module Micronaut Test.
Test framework for Micronaut
You can test with either. This time I will test using JUnit 5.
Add a test method to HelloController.
HelloController.java
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import my.app.service.HelloService;
@Controller("/hello")
public class HelloController {
    HelloService helloService;
    HelloController(HelloService helloService) {
        this.helloService = helloService;
    }
    @Get(produces = MediaType.TEXT_PLAIN)
    public String index() {
        return "hello world!!";
    }
    //Added for testing with multiple parameters
    @Get(uri = "/{path}",processes = MediaType.TEXT_PLAIN)
    public String index(String path) {
        return path;
    }
    //Service class processing Added for Mock confirmation
    @Get(uri = "/compute/{number}",processes = MediaType.TEXT_PLAIN)
    public String compute(Integer number) {
        //Set the compute method to Mock during testing.
        return String.valueOf(helloService.compute(number));
    }
}
HelloService
public interface HelloService {
    public Integer compute(Integer num);
}
HelloServiceImpl
import javax.inject.Singleton;
@Singleton
public class HelloServiceImpl implements HelloService {
    @Override
    public Integer compute(Integer num) {
        return num * 4;
    }
}
build.gradle
dependencies {
・ ・ ・
    testAnnotationProcessor "io.micronaut:micronaut-inject-java"
    testCompile "io.micronaut.test:micronaut-test-junit5"
    testCompile "org.junit.jupiter:junit-jupiter-params"
    testCompile "org.mockito:mockito-core:2.24.5"
    testRuntime "org.junit.jupiter:junit-jupiter-engine"
}
HelloControllerTest.java
import io.micronaut.http.HttpRequest;
import io.micronaut.http.client.RxHttpClient;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.test.annotation.MicronautTest;
import io.micronaut.test.annotation.MockBean;
import my.app.service.HelloService;
import my.app.service.HelloServiceImpl;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import javax.inject.Inject;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;
@MicronautTest
public class HelloControllerTest {
    //Test HelloController with RxHttpClient
    @Inject
    @Client("/")
    RxHttpClient client;
    @Inject
    HelloService helloService;
    @Test
    void testHelloIndex() {
        final String result = client.toBlocking().retrieve(HttpRequest.GET("/hello"), String.class);
        assertEquals(
                "hello world!!",
                result
        );
    }
    @ParameterizedTest
    //You can pass multiple parameters and test multiple times with the same source
    @ValueSource(strings = { "racecar", "radar" })
    void testHelloIndexPath(String path) {
        final String result = client.toBlocking().retrieve(HttpRequest.GET("/hello/" + path), String.class);
        assertEquals(
                path,
                result
        );
    }
    @ParameterizedTest
    @CsvSource({"2,4", "3,9"})
    void testComputeNumToSquare(Integer num, Integer square) {
        //Set the behavior of Mock
        when(helloService.compute(num))
                .then(invocation -> Long.valueOf(Math.round(Math.pow(num, 2))).intValue());
        //Call the controller to get the result
        final Integer result = client.toBlocking().retrieve(HttpRequest.GET("/hello/compute/" + num), Integer.class);
        assertEquals(
                square,
                result
        );
        verify(helloService).compute(num);
    }
    //Mock definition using MockBean
    @MockBean(HelloServiceImpl.class)
    HelloService mathService() {
        return mock(HelloService.class);
    }
}
Since it is a unit test using JUnit, the introduction barrier seems to be low. Since Micronaut started up quickly, the test was light and good.
https://micronaut-projects.github.io/micronaut-test/latest/guide/index.html
Recommended Posts