Testing
The pippo-test module provides JUnit4 PippoTest class for starting your application in TEST mode on a randomly available HTTP port.
PippoTest
comes with a simple and intuitive API, based on Rest-Assured.
Rest-Assured
is automatically configured for this instance of your application allowing you to easily make HTTP requests to your test instance and focus on unit and integration testing of your code.
How to use
public class RouteTest extends PippoTest {
@Rule
public PippoRule pippoRule = new PippoRule(new PippoApplication());
@Test
public void testIndex() {
when().
get("/").
then().
statusCode(200);
}
@Test
public void testHello() {
Response response = get("/hello");
response.then().statusCode(200);
response.then().contentType(ContentType.HTML);
assertEquals("Hello World", response.asString());
}
}
Also don’t forget to add pippo-test
as dependency in your project:
<dependency>
<groupId>ro.pippo</groupId>
<artifactId>pippo-test</artifactId>
<version>${pippo.version}</version>
</dependency>
Implementation details
The central piece in pippo-test
module is PippoRule.
PippoRule
is a JUnit Rule that start Pippo prior to test execution and stop Pippo after the tests have completed.
PippoTest
class is optionally and it not contains logic. You can create your Test class without toextend PippoTest. PippoTest
class only extends RestAssured
.
RestAssured
it’s a Java DSL library for easy testing REST services.
You can change how many instances of Pippo are created via @Rule
and @ClassRule
annotations(JUnit annotations).
If you want one Pippo instance for EACH test then you must add below code in your test class:
@Rule
public PippoRule pippoRule = new PippoRule(new PippoApplication());
If you want one Pippo instance for ALL tests then instead of the above code line you must add:
@ClassRule
public static PippoRule pippoRule = new PippoRule(new PippoApplication());