-
Notifications
You must be signed in to change notification settings - Fork 12
Testing
mangoo I/O ships with convenient tools for testing your application. Please note, that these utilities are not part of the core and come with a additional dependency. This is mainly because you want to set the scope of this dependency set to “test” in your maven configuration.
<dependency>
<groupId>io.mangoo</groupId>
<artifactId>mangooio-test</artifactId>
<version>1.0.0</version>
<scope>test</scope>
</dependency>
mangoo I/O provides convinent classes to support unit testing your application.
Here is an example of how a unit test with the test utilities might look like.
...
import io.mangoo.test.utils.WebRequest;
import io.mangoo.test.utils.WebResponse;
...
@Test
public void testIndex() {
//given
WebResponse response = WebRequest.get("/").execute();
//then
assertThat(response, not(nullValue()));
assertThat(response.getContentType(), equalTo(TEXT_HTML));
assertThat(response.getStatusCode(), equalTo(StatusCodes.OK));
}
The most common use case is probably a request-response test with your application. Therefore, mangoo I/O provides your with a test utility for Request and Response. You can add authentication, headers, etc. to the request. Check the fluent API of the Request object for this.
There may be situation where you need to pass the request information along to the request. For this scenarios mangoo I/O provides you with the Browser class.
WebBrowser browser = WebBrowser.open();
The browser class enables you to pass to keep the request information on the following requests. Here is an example on how this might look like.
...
import io.mangoo.test.utils.WebBrowser;
import io.mangoo.test.utils.WebRequest;
import io.mangoo.test.utils.WebResponse;
...
//given
WebBrowser browser = Browser.open();
//when
WebResponse response = browser.withUri("/dologin")
.withMethod(Methods.POST)
.execute();
//then
assertThat(response, not(nullValue()));
assertThat(response.getStatusCode(), equalTo(StatusCodes.FOUND));
//when
response = browser.withUri("/authenticationrequired")
.withDisableRedirects(true)
.withMethod(Methods.GET)
.execute();
The information from the first request, like cookies, etc. will be passed to the following request, enabling you a browser-like testing of your application.
For frontend testing mangoo I/O uses FluentLenium. Here is an example of how a FluentLenium test might look like.
package mangoo.controllers;
import static org.junit.Assert.assertTrue;
import io.mangoo.testing.MangooUnit;
import org.junit.Test;
public class FluentTest extends MangooFluent {
@Test
public void title_of_bing_should_contain_search_query_name() {
goTo("http://www.bing.com");
fill("#sb_form_q").with("FluentLenium");
submit("#sb_form_go");
assertTrue(title().contains("FluentLenium"));
}
}
manoo I/O uses a TestSuite for all unit testing. This concept allows you to start the framework once, execute all unit test and shut the framework down afterwards. For using a TestSuite you need an entry-point for the execution which extends the MangooRunner interface.
package io.mangoo;
import io.mangoo.test.MangooRunner;
public class TestSuite extends MangooRunner {
}
This just needs to be an empty class for telling Maven to use this Suite when tests are executed. You can, of course, use this class to setup your unit tests, like starting an in-memory database, etc.
Additionally the following plugin with the following configurtion needs to be added to your pom.xml to make Maven aware of your TestSuite class.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*TestSuite.java</include>
</includes>
</configuration>
</plugin>
By convention, the above TestSuite will execute all tests that ends with “*Test” in their class name. Make sure to follow this convention, otherwise your tests will not be executed.
mangoo I/O 2015-2024 | [email protected]
- Getting started
- Configuration
- Routing
- Bootstrap
- Controllers
- Dependency injection
- Templating
- Working with JSON
- Persistence
- CORS
- Authentication
- Authorization
- Scheduler
- Async
- Filters
- Forms
- Session
- Flash
- Internationalization
- Caching
- Emails
- Asset management
- Logging
- Debugging
- Testing
- Administration
- Debian init.d script
- Extensions