It is quite important for a web framework to integrate testing as seamlessly as possible with the web framework itself. This minimizes the friction developers encounter when coding functional specs and writing tests to validate their work. For Play Java projects, we will be utilizing the popular test framework JUnit. We will be using it to do a simple unit test and to test our model and controller action. For Play Scala projects, we will be using specs2 to do a simple unit test and to test our model, a controller action, and a route mapping.
For Java, we need to take the following steps:
Create a new spec class,
ProductTest.java
, intest/
and add the following content:import static org.junit.Assert.*; import org.junit.Test; public class ProductTest { @Test public void testString() { String str = "product"; assertEquals(7, str.length()); } }
Run the first spec using Activator by running the command
test-only ProductTest
:$ activator [info] Loading project definition from /private/tmp/foo_java/project [info] Set current project to foo_java (in build file:/private/tmp/foo_java/) [foo_java] $ test-only ProductTest [info] Compiling 3 Java sources to /private/tmp/foo_java/target/scala-2.11/test-classes... [info] Passed: Total 1, Failed 0, Errors 0, Passed 1 [success] Total time: 3 s, completed 09 29, 14 8:44:31 PM
For Scala, we need to take the following steps:
Create a new Spec class,
ProductSpec.scala
, intest/
and add the following content:import org.specs2.mutable._ class ProductSpec extends Specification { "The 'product' string" should { "contain seven characters" in { "product" must have size(7) } } }
Run the first spec using Activator by running the command
test-only ProductSpec
:$ activator [info] Loading project definition from /private/tmp/foo_scala/project [info] Set current project to foo_scala (in build file:/private/tmp/foo_scala/) [foo_scala] $ test-only ProductSpec [info] ProductSpec [info] [info] The 'product' string should [info] + contain seven characters [info] [info] Total for specification ProductSpec [info] Finished in 24 ms [info] 1 example, 0 failure, 0 error [info] Passed: Total 1, Failed 0, Errors 0, Passed 1 [success] Total time: 2 s, completed 09 29, 14 12:22:57 PM