Fork me on GitHub

Advanced

NinjaRunner

NinjaRunner makes it easy to test DAOs and service objects. Service objects are generally the place where business logic executed. Each service object may be injected a lot of DAOs.

Suppose you have some service objects , without NinjaRunner , the only way to test service objects is to getInstance() of these DAOs from Guice's injector and manually constructor-injecting to the service object in NinjaTest's @Before method.

With NinjaRunner, you just add @RunWith(NinjaRunner.class) to your test class , and declare @Inject private ServiceObj serviceObj and you'll get an injected serviceObj. No more @Before methods.

Code Example:

@RunWith(NinjaRunner.class)
public class DataServiceTest  {
    @Inject private ServiceObj serviceObj;

    @Test
    public void testDataService() {
        assert (serviceObj != null);
    }
}

NinjaDaoTestBase

Sometimes you need to test a DAO method directly on a real Database, just extend NinjaDaoTestBase and instantiate the DAO class calling the getInstance(…) method from the super class and start using it in your test methods.

This helper starts the Persistence Service using the parameters of the application.conf file. You can pass the NinjaMode (test, dev, prod) or set it via command line. If no NinjaMode is passed NinjaDaoTestBase assumes NinjaMode.test as default.

Check an example:

import ninja.NinjaDaoTestBase;
import org.junit.*;

public class AbstractDaoTest extends NinjaDaoTestBase {

    private TestDao testDao;

    @Before
    public void setup(){
        //Instanting DAO using super method
        testDao = getInstance(TestDao.class);
    }


    @Test
    public void testSave() {
        MyEntity myEntity = new MyEntity();
        myEntity.setEmail("[email protected]");
        assertNull(myEntity.getId());

        //Use the DAO with a real database
        myEntity = testDao.save(myEntity);

        assertNotNull(entity.getId());
    }
}