Skip to content
pramode edited this page Jun 29, 2011 · 4 revisions

Example unit test

Under the folder "test", create MyTest.java:

  import play.test.*;
  import org.junit.*;
  import models.*;

  public class MyTest extends UnitTest {

     @Test
     public void aTest() {
          assertEquals(2, 1 + 1); // A really important thing to test
     }   

     @Test
     public void testUsers() {
        assertEquals(2, Client.count()); 
     }   

  }

Run the test:

   play test playapp

Example functional test

Create the file MyFunctionalTest.java:

  import play.test.*;
  import play.mvc.*;
  import play.mvc.Http.*;
  import org.junit.*;

  public class MyFunctionalTest extends FunctionalTest {
     
     @Test
     public void testTheHomePage() {
         Response response = GET("/foo");
         assertStatus(200, response);
     }   
     
    }

Resetting database before each test

Have a function like this in your test class:

    @Before
    public void setUp() {
       Fixtures.deleteAll();
    }

Loading values into a database (from a .yml file)

Create a .yml file (say data.yml) under the test folder:

       Client(Ramu):
         name: Ramu
         age: 35

      Client(Gopu):
         name: Gopu
         age: 30

      Client(Asok):
         name: Asok
         age: 45

And, call the function Fixtures.loadModels("data.yml"):

    @Before
    public void setUp()
    {   
        Fixtures.deleteAll();
        Fixtures.loadModels("data.yml");
    }