1. import io.micronaut.runtime.Micronaut
    2. class Application {
    3. static void main(String... args) {
    4. Micronaut.run Application.class
    5. }
    6. }

    To run the application from a unit test you can use the interface:

    1. import io.micronaut.context.annotation.Property;
    2. import io.micronaut.http.HttpRequest;
    3. import io.micronaut.http.client.HttpClient;
    4. import io.micronaut.http.client.annotation.Client;
    5. import io.micronaut.runtime.server.EmbeddedServer;
    6. import io.micronaut.test.annotation.MicronautTest;
    7. import org.junit.jupiter.api.Test;
    8. import javax.inject.Inject;
    9. import static org.junit.jupiter.api.Assertions.assertEquals;
    10. @Inject
    11. EmbeddedServer server; (1)
    12. @Inject
    13. @Client("/")
    14. HttpClient client; (2)
    15. @Test
    16. void testHelloWorldResponse() {
    17. String response = client.toBlocking() (3)
    18. .retrieve(HttpRequest.GET("/hello"));
    19. assertEquals("Hello World", response); //) (4)
    20. }
    21. }
    1. import io.micronaut.context.annotation.Property
    2. import io.micronaut.http.client.HttpClient
    3. import io.micronaut.http.client.annotation.Client
    4. import io.micronaut.runtime.server.EmbeddedServer
    5. import io.micronaut.test.annotation.MicronautTest
    6. import org.junit.jupiter.api.Test
    7. import javax.inject.Inject
    8. @MicronautTest
    9. class HelloControllerSpec {
    10. @Inject
    11. lateinit var server: EmbeddedServer (1)
    12. @Inject
    13. @field:Client("/")
    14. lateinit var client: HttpClient (2)
    15. @Test
    16. fun testHelloWorldResponse() {
    17. val rsp: String = client.toBlocking() (3)
    18. .retrieve("/hello")
    19. assertEquals("Hello World", rsp) (4)
    20. }
    21. }
    Without explicit port configuration, the port will be 8080, unless the application is run under the test environment. In that case the port will be random. When the application context is started from the context of a test class, the test environment is added automatically.