Testing applications
Wisdom provides everything you need to test your application. More specifically, it supports:
-
Unit tests - test your classes and action methods
-
In-container integration tests - test your controllers and services on a running server
-
Blackbox integration tests - emit HTTP request and check the results
-
UI tests - check the web page exposed by your application
To ease the development ot tests, Wisdom provides AssertJ, OSGi Testing Helpers for in-container tests, a HTTP fluent API for blackbox tests, and FluentLenium for UI tests.
|
Important
|
By default, tests are looking for a free port. So don’t worry about configuring the port used by your
tests. You can configure the used port by setting it explicitly with the http.port system
property.
|
Unit Tests
The unit test support lets you write Junit tests to check the behavior of your classes. These
tests cases are written
in Java classes in the src/test/java folder. Notice that test case classes must start or end with Test (this is
a Surefire convention).
Unit tests should extend the WisdomUnitTest class to access utility methods.
include::{testdir}/unit/UnitTest.java[tags=unit]
The first test is a really simple test just checking that 1+1=2. Notice the AssertJ syntax.
The second test checks the behavior of one of your action methods. As your action method can
access the HTTP method,
the call is wrapped into an Invocation call:
Action.ActionResult result = action(new Invocation() {
@Override
public Result invoke() throws Throwable {
return simple.index();
}
}).invoke();
Such a pattern lets you call your action directly, and retrieve the HTTP result. You can also
configure the HTTP
headers, parameters, form attributes, cookies, session…. The chain must end with invoke()
Action.ActionResult result = action(new Invocation() {
@Override
public Result invoke() throws Throwable {
return simple.index();
}
}).parameter("name", "clement").header("Accept", "text/html").invoke();
The ActionResult contains the HTTP Context (that may have been modified by your action method, and the HTTP result.
In-Container Tests
In-container tests let you check the behavior of your controller and services running in the server. The server is
automatically launched. These tests cases are written in Java classes in the src/test/java
folder. Notice that test
case classes must start or end with "IT" (this is a Surefire/Failsafe convention).
In-container tests must extend WisdomTest, and follow the same rules as unit tests. However, @Inject annotated
fields are injected.
include::{testdir}/unit/InContainerIT.java[tags=IT]
The @Inject annotation lets you access the real instance of your controller, service or template:
// Controller injection: @Inject HelloController controller; // Service injection: @Inject private Validator validator; // The OSGi bundle context @Inject private BundleContext context;
Once injected your test can invoke them either directly or in an invocation block to configure the HTTP context.
|
Important
|
the same server instance is used for all your tests. |
BlackBox Tests
Black box tests are emitting HTTP requests and retrieving the result. Such tests are not executed in the server. Your application is deployed within the server (it reuses the launched one by in-container tests if it’s already running).
As for in-container tests, a black box test class must start or end with "IT". These classes must
extend
org.wisdom.test.parents.WisdomBlackBoxTest.
include::{testdir}/unit/BlackBoxIT.java[tags=IT]
The test API lets you emit any type of HTTP requests and configure headers, payload, cookies… In addition, the response can be smoothly wrapped to Json Node, HTML Document (JSoup), String, or input stream.
FluentLenium Tests
UI Tests are loading pages using a browser and check the elements present on the page. Wisdom uses FluentLenium to ease the implementation of UI tests.
As for in-container tests, UI test classes must start or end with "IT". These classes must extend
org.wisdom.test.parents.WisdomFluentLeniumTest.
include::{testdir}/unit/UIIT.java[tags=IT]
You can select the browser to use by setting the -Dfluentlenium.browser property in the command line. Accepted
values are firefox, chrome, ie (Internet Explorer) and safari. If not set it uses the HTML Unit Browser
(Fluentlenium’s default) relying on Firefox.
Notice that depending on the browser you choose you may have to install additional software or specify addition values. Check the following links:
Stopping the server instance
When building a Maven multi-module project, the same instance of the server is used for all your tests. This can be annoying. Fortunately, Wisdom provides a test listener to stop the instance one the module build is completed:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.17</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<properties>
<property>
<name>listener</name>
<value>org.wisdom.test.WisdomRunListener</value>
</property>
</properties>
</configuration>
</execution>
</executions>
</plugin>
The previous configuration is automatically generated when creating a new project.