Play Framework – Testing your application

Composing tests for your application could be an included procedure. Play helps JUNIT and gives partners and application stubs to make testing your application as simple as would be prudent.

Review

The area for tests is in the “test” envelope. There are two example test records made in the test organizer which could be utilized as layouts.

You can run tests from the Activator support.

  • To run all tests, run test.
  • To run stand out test class, run testonly took after by the name of the class.
  • To run just the tests that have fizzled, run testquick.
  • To run tests constantly, run an order with a tilde in front, i.e. ~testquick.
  • To get to test assistants, for example, Fakeapplication in support, run test:console.

Testing in Play is focused around sbt, and a full portrayal is accessible in the testing.

Utilizing JUnit

The default approach to test a Play application is with Junit.

import org.junit.test;
public class Simpletest {
@test
public void testsum() {
int a = 1 + 1;
assertequals(2, a);
}

@test
public void teststring() {
String str = "Hi world";
assertfalse(str.isempty());
}
}

Attestations & Matchers

A few engineers like to compose their affirmations in a more familiar style than Junit affirms. Prevalent libraries for other affirmation styles are incorporated for comfort.

FEST attestations:

import static org.fest.assertions.*;
import org.junit.test;

public class Festtest {

@test

public void testsum() {
int a = 1 + 1;
assertthat(a).isequalto(2);
}
}

Hamcrest matchers:

import static org.hamcrest.corematchers.*;
import static org.junit.assert.assertthat;
import org.junit.test;
public class Hamcresttest {

@test
public void teststring() {
String str = "great";
assertthat(str, allof(equalto("good"), startswith("goo")));
}
}

Mocks

Mocks are utilized to segregate unit tests against outer conditions. Case in point, if your class under test relies on upon an outside information access class, can taunt this to give control information and take out the requirement for an outer information asset.

Mockito class is in combined in your venture manufacture to aid you in utilizing mocks.

Utilizing Mockito, you can ridicule classes or interfaces like so:

import static org.mockito.mockito.*;
// Create and train mock
List<string> mockedlist = mock(list.class);
when(mockedlist.get(0)).thenreturn("first");
// check esteem
assertequals("first", mockedlist.get(0));
// verify connection
verify(mockedlist).get(0);

 

Writing Functional Tests

Play gives various classes and accommodation systems that help with functional testing. The vast majority of these could be discovered either in the play.test bundle or in the Helpers class.

You can include these routines and classes by importing the accompanying:

import play.test.*;
import static play.test.helpers.*;

FakeApplication

Play much of the time obliges a running Application as connection: it is typically given fromplay.play.application().

To give an environment to tests, Play gives a Fakeapplication class which might be arranged with a different Global protest, extra design, or even extra plugins.

Fakeapplication fakeapp = Helpers.fakeapplication();
Fakeapplication fakeappwithglobal = fakeapplication(new Globalsettings() {

@override
public void onstart(application) {
System.out.println("starting Fakeapplication");
}
});

Fakeapplication fakeappwithmemorydb = fakeapplication(inmemorydatabase("test"));

 Testing with a fake application

To run tests inside a Fakeappliction, you can do the accompanying:

@Test
public void findbyid() {
running(fakeapplication(inmemorydatabase("test")), new Runnable() {
public void run() {
Machine mac = Computer.findbyid(21l);
assertthat(macintosh.name).isequalto("macintosh");
assertthat(formatted(macintosh.introduced)).isequalto("1984-01-24");
}
});
}

You can additionally broaden Withapplication, this will consequently guarantee that a fake application is begun and halted for you:

 

public class Functionaltest amplifies Withapplication {

Testing with a server

Off and on again you need to test the genuine HTTP stack from with your test. You can do this by beginning a test server:

 

@test
public void testinserver() {
running(testserver(3333), new Runnable() {
public void run() {
assertthat(
Ws.url("http://localhost:3333").get().get(timeout).getstatus()
).isequalto(ok);
}
});
}

Testing with a program

If you need to test your application from with a Web program, you can utilize Selenium Webdriver. Play will begin the Webdriver for you, and wrap it in the advantageous API gave by Fluentlenium.

@test
public void runinbrowser() {
running(testserver(3333), HTMLUNIT, new Callback<testbrowser>() {
public void invoke(testbrowser program) {
browser.goto("http://localhost:3333");
browser.$("a").click();
assertthat(browser.url()).isequalto("http://localhost:3333/login");
}
});
}