|
| 1 | +--- |
| 2 | +layout: page |
| 3 | +title: "Testing" |
| 4 | +category: doc |
| 5 | +date: 2016-09-07 22:48:45 |
| 6 | +order: 197 |
| 7 | +--- |
| 8 | + |
| 9 | +The [pippo-test]({{ site.codeurl }}/pippo-test) module provides JUnit4 [PippoTest]({{ site.codeurl }}/pippo-test/src/main/java/ro/pippo/test/PippoTest.java) class for starting your application in TEST mode on a randomly available HTTP port. |
| 10 | +`PippoTest` comes with a simple and intuitive API, based on [Rest-Assured](https://github.com/rest-assured/rest-assured). |
| 11 | + |
| 12 | +`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. |
| 13 | + |
| 14 | +#### How to use |
| 15 | + |
| 16 | +```java |
| 17 | +public class RouteTest extends PippoTest { |
| 18 | + |
| 19 | + @Rule |
| 20 | + public PippoRule pippoRule = new PippoRule(new PippoApplication()); |
| 21 | + |
| 22 | + @Test |
| 23 | + public void testIndex() { |
| 24 | + when(). |
| 25 | + get("/"). |
| 26 | + then(). |
| 27 | + statusCode(200); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void testHello() { |
| 32 | + Response response = get("/hello"); |
| 33 | + response.then().statusCode(200); |
| 34 | + response.then().contentType(ContentType.HTML); |
| 35 | + assertEquals("Hello World", response.asString()); |
| 36 | + } |
| 37 | + |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +Also don't forget to add `pippo-test` as dependency in your project: |
| 42 | + |
| 43 | +``` |
| 44 | +<dependency> |
| 45 | + <groupId>ro.pippo</groupId> |
| 46 | + <artifactId>pippo-test</artifactId> |
| 47 | + <version>${pippo.version}</version> |
| 48 | +</dependency> |
| 49 | +``` |
| 50 | + |
| 51 | +#### Implementation details |
| 52 | + |
| 53 | +The central piece in `pippo-test` module is [PippoRule]({{ site.codeurl }}/pippo-test/src/main/java/ro/pippo/test/PippoRule.java). |
| 54 | +`PippoRule` is a __JUnit Rule__ that start Pippo prior to test execution and stop Pippo after the tests have completed. |
| 55 | +`PippoTest` class is optionally and it not contains logic. You can create your Test class without toextend PippoTest. `PippoTest` class only extends `RestAssured`. |
| 56 | +`RestAssured` it's a Java DSL library for easy testing REST services. |
| 57 | + |
| 58 | +You can change how many instances of Pippo are created via `@Rule` and `@ClassRule` annotations(JUnit annotations). |
| 59 | +If you want one Pippo instance for __EACH__ test then you must add below code in your test class: |
| 60 | + |
| 61 | +```java |
| 62 | +@Rule |
| 63 | +public PippoRule pippoRule = new PippoRule(new PippoApplication()); |
| 64 | +``` |
| 65 | + |
| 66 | +If you want one Pippo instance for __ALL__ tests then instead of the above code line you must add: |
| 67 | + |
| 68 | +```java |
| 69 | +@ClassRule |
| 70 | +public static PippoRule pippoRule = new PippoRule(new PippoApplication()); |
| 71 | +``` |
| 72 | + |
0 commit comments