Subscribe / Unsubscribe Enewsletters | Login | Register

Pencil Banner

Jump into Java microframeworks, Part 2: Ninja

Matthew Tyson | Nov. 25, 2015
Build a functional Person application with Ninja.

Testing a Ninja application

No application framework or development process is complete without infrastructure for testing. Ninja offers a variety of options for testing, including Mockito, which works as well as it does with other frameworks. More interesting is NinjaTest support, which allows for HTTP scraping against a real server. Listing 16 has a simple test of the index route that we defined in Part 1 (router.GET().route("/").with(ApplicationController.class, "index");).

Listing 16. Simple live test for app controller

public class AppControllerTest extends NinjaTest {


    public void testIndex() {

        String result = ninjaTestBrowser.makeRequest(getServerAddress() + "/");




The key here is to extend NinjaTest, which exposes the ninjaTestBrowser dependency. This allows us to make requests to the application, which will be started to host our test. We then can make assertions of the response; you can see this above with the assertion that the "add" string is present in the response.

Ninja also includes the NinjaDocTester class. Descended from NinjaTest, this class gives you all of Ninja's basic testing support, and also lets you generate HTML documentation. Together these two classes make generating automated API documentation a fairly straightforward part of the normal build test.

For more in-depth HMTL testing, Ninja has NinjaFluentLeniumTest, which again will automatically spin up the app instance for you, and run FluentLenium tests (an evolution of Selenium).


We've covered a lot of ground in this short introduction to web application development with Ninja. I focused on persistence, but also addressed routing, debugging, UI development, and testing. Ninja is a capable framework that simplifies these necessities, making it possible to get an application off the ground quickly. Working within Ninja is enjoyable, and doesn't require learning a lot of extraneous details or framework-specific technology. Ninja lets you focus on your application, while providing all the tools that you need in a robust, useful configuration.

Leave a comment below if you have any thoughts about this series, or tell me and the readers about your experience with Ninja development in general.


Previous Page  1  2  3  4  5  6  7  8 

Sign up for CIO Asia eNewsletters.