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");
}
}
I think the error happens because on a library build there is now server runtime startet upon tests. But a runtime is needed for your integration tests.
I have a different error (Micronaut and Gradle can't get tests working) but I think it is the same cause.
Move your integration tests to the sub-project with the io.micronaut.application plugin. I solved it that way.