Blackbox System Tests with Quarkus and MicroProfile REST Client 📎
@Path("/hello")
public class HelloResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "hello";
}
}
A dedicated quarkus project with installed MicroProfile REST Client
extension (quarkus-rest-client
) can be used as a testbed.
A proxy interface:
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
@RegisterRestClient(baseUri = "http://localhost:8080")
public interface HelloResource {
@GET
@Path("hello")
@Produces(MediaType.TEXT_PLAIN)
String content();
}
is injectable directly into the system test:
import static org.junit.jupiter.api.Assertions.assertEquals;
import javax.inject.Inject;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.junit.jupiter.api.Test;
import io.quarkus.test.junit.QuarkusTest;
@QuarkusTest
public class HelloResourceTest {
@Inject
@RestClient
HelloResource resource;
@Test
public void hello() {
String content = resource.content();
System.out.println(content);
assertEquals(content,"hello");
}
}
See it live and from scratch: