2

Recently I am using micronaut libraries to move some controllers which are meant to be reused. However, i cant figure out how to test those controllers. The get included in the application just fine, but on there own the tests case timesout. How can i fix this?

# build.gradle
apply plugin: "io.micronaut.library"


micronaut {
    runtime("netty")
    testRuntime("junit5")
}


dependencies {
    api("io.micronaut:micronaut-http")
    api("io.micronaut:micronaut-http-server")
    testImplementation("io.micronaut:micronaut-http-server")
    testImplementation("io.micronaut:micronaut-http-client")

...

HumanController.java

package fri.so.common;


import io.micronaut.core.io.ResourceLoader;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;

import java.io.IOException;
import java.io.InputStream;

@Controller
public class HumansController {

  private final ResourceLoader resourceLoader;

  public HumansController(ResourceLoader resourceLoader) {
    this.resourceLoader = resourceLoader;
  }

  @Get("/humans.txt")
  public InputStream humans() throws IOException {

    return resourceLoader.getResourceAsStream("classpath:humans.txt")
            .orElseThrow(() -> new IOException("Could not load humans.txt file"));
  }
}

HumanControllerTest.java

package fri.so.common;

import io.micronaut.http.client.HttpClient;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.net.MalformedURLException;
import java.net.URL;

@MicronautTest
class HumansControllerTest {

    @Test
    void testHumans() throws MalformedURLException {
        Assertions.assertTrue(true);
        String response = HttpClient.create(new URL("http://127.0.0.1:8080/")).toBlocking().retrieve("humans.txt");

    }
}
Friso Kluitenberg
  • 1,157
  • 1
  • 14
  • 34
  • Maybe you should read up on MN test: https://micronaut-projects.github.io/micronaut-test/latest/guide/index.html Using http://127.0.0.1:8080/ in a MN test is new to me. – Roar S. Apr 27 '23 at 20:25

0 Answers0