6.1 Running the Embedded Server

To run the server simply create an Application class with a static void main method. For example:

  1. import io.micronaut.runtime.Micronaut;
  2. public class Application {
  3. public static void main(String[] args) {
  4. Micronaut.run(Application.class);
  5. }
  6. }
  1. import io.micronaut.runtime.Micronaut
  2. class Application {
  3. static void main(String... args) {
  4. Micronaut.run Application.class
  5. }
  6. }
  1. import io.micronaut.runtime.Micronaut
  2. object Application {
  3. @JvmStatic
  4. fun main(args: Array<String>) {
  5. Micronaut.run(Application.javaClass)
  6. }
  7. }

To run the application from a unit test you can use the EmbeddedServer 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. @MicronautTest
  11. public class HelloControllerSpec {
  12. @Inject
  13. EmbeddedServer server; (1)
  14. @Inject
  15. @Client("/")
  16. HttpClient client; (2)
  17. @Test
  18. void testHelloWorldResponse() {
  19. String response = client.toBlocking() (3)
  20. .retrieve(HttpRequest.GET("/hello"));
  21. assertEquals("Hello World", response); //) (4)
  22. }
  23. }
  1. import io.micronaut.http.HttpRequest
  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.extensions.spock.annotation.MicronautTest
  6. import spock.lang.Specification
  7. import javax.inject.Inject
  8. @MicronautTest
  9. class HelloControllerSpec extends Specification {
  10. @Inject
  11. EmbeddedServer embeddedServer (1)
  12. @Inject
  13. @Client("/")
  14. HttpClient client (2)
  15. void "test hello world response"() {
  16. expect:
  17. client.toBlocking() (3)
  18. .retrieve(HttpRequest.GET('/hello')) == "Hello World" (4)
  19. }
  20. }
  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.Assertions.assertEquals
  7. import org.junit.jupiter.api.Test
  8. import javax.inject.Inject
  9. @MicronautTest
  10. class HelloControllerSpec {
  11. @Inject
  12. lateinit var server: EmbeddedServer (1)
  13. @Inject
  14. @field:Client("/")
  15. lateinit var client: HttpClient (2)
  16. @Test
  17. fun testHelloWorldResponse() {
  18. val rsp: String = client.toBlocking() (3)
  19. .retrieve("/hello")
  20. assertEquals("Hello World", rsp) (4)
  21. }
  22. }
1The EmbeddedServer is run and Spock’s @AutoCleanup annotation ensures the server is stopped after the specification completes.
2The EmbeddedServer interface provides the URL of the server under test which runs on a random port.
3The test uses the Micronaut http client to make the call
4The retrieve method returns the response of the controller as a String
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.