Testing

Testing Structure

The tests uses the configuration file opensrp-core/src/test/resources/test-applicationContext-opensrp.xml.
This file unlike the runtime loads both Postgres and CouchDb beans, this will to allow the existing unit tests that used CouchDb continue to run without any changes. The CouchDb beans are annotated with the annotation @Primary which means that they are the used even if there are other beans implementing an interface.

So the tests(unit and integration) thats are explicitly for postgres qualify the injected dependencies by the @Qualifier so that they specify explicitly the bean to be injected i.e in this this case Postgres bean. 

Postgres Repositories Unit Tests


Units tests have been defined that unit-test the Postgres repository package org.opensrp.repository.postgres. The repositories unit tests are in the package org.opensrp.repository.postgres in the test folder
The unit tests use JUnit(SpringJUnit4ClassRunner) for unit testing and test for various scenarios for each method in the respective repository class being tested. Each of the test extend the class org.opensrp.repository.postgres.BaseRepositoryTest. This class defines two methods

  • populateDatabase method that cleans and populates the database with seed data before the test starts. Its annotated with JUnit  annotation @Before so that it runs before each test.
  • getDatabaseScripts abstract method which returns a set of strings. Each test must implement this method, the set returned by the implementation should have a set of sql files to be executed before starting each test.


The tests use the seed data created by tests in the folder opensrp-core/src/test/resources/test-scripts/. Each script clears any existing data and then populates the test data.

Services Integration Tests

Integration tests were written for services. The tests concentrate on methods with some business logic or have pre or post processing before calling the repositories. 
The services extends org.opensrp.repository.postgres.BaseRepositoryTest so that they are also able to easily apply one or more scripts to populate seed before the tests commence.

With the repositories unit tests and units integration tests regression errors are easily reported early.