pippo icon indicating copy to clipboard operation
pippo copied to clipboard

Migrate to JUnit 5

Open decebals opened this issue 6 years ago • 2 comments

decebals avatar Mar 29 '19 19:03 decebals

When migrating to JUnit 5. I have an idea to provide an annotation e.g. @PippoTest (extends JUnit @Test annotation) and provides a PippoTestClient instance.

Example:

@PippoTest
public void testEndpoint(PippoTestClient client) {
    final var httpClient = HttpClient.newHttpClient(); // using official java.net.HttpClient 

	final var httpRequest = client.newRequestBuilder("/path").GET().build();

	final var response = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString()).body();
	assertThat(response).isEqualTo(..);
}

In this case the user doesn't need to take care of e.g. http://host:port . And when providing the port then JUnit 5 parallelism can be done (otherwise we would able to verify only one test method due single port)

The port should be choosen automatically because you don't know which ports are free on public CI (e.g. Travis CI)

You can check out how it is done: https://github.com/pitschr/knx-link/blob/master/src/test/java/li/pitschmann/knx/daemon/KnxHttpDaemonTest.java

For this case it would be helpful when we can start Pippo with port = 0 which simply picks up the next free port (otherwise we need a dirty workaround to ensure the thread safety; how I resolved it can be seen here: method startPippo() on MockHttpDaemon ).

The @PippoTest may come with some values / settings. Feel free to reach out me if you want know more details.

pitschr avatar Apr 20 '19 18:04 pitschr

Thanks for your sugestion. I think that actual approach with PippoTest (see http://www.pippo.ro/doc/testing.html) is more simple and intuitive. More then that, java.net.HttpClient is a new class that is available only in Java 11 (Pippo is based on Java 8). But the variant with HttpClient (Java new HTTP, Apache HTTP Components, OkHttp, ...) is a very good alternative.

decebals avatar Apr 23 '19 06:04 decebals