...
- Unit tests are meant to be very small and efficient.
- Each method (aka each test) should only validate one logical piece of code.
- Method names should be long and descriptive
- Methods should have an Assert.* call in it
Example:
Code Block |
---|
public void getAllUsers_shouldNotReturnNull() throws Exception {
Assert.assertNotNull(Context.getUserService().getAllUsers());
}
|
When in a BaseContextSensitiveTest type of Test class, some test data is available to you automatically. See the standardTestDataset.xml and initialInMemoryTestDataSet.xml files for content. See "Setting Up the Expected Database" below for how to add your own test data.
Code Block |
---|
public class MyTests extends BaseContextSensitiveTest {
public void getPatientByName_shouldReturnValidPatients()
{
List patients = Context.getPatientService().getPatientsByName("bob"); Assert.assertEquals(34, patients.get(0).getPatientId());
}
}
|
...
1) Add an @should annotation to the object you're testing:
Code Block |
---|
package org.openmrs;
**
* This does blah blah blah...
*/
public class MyObject {
.......
**
* This method does something blah blah blah
* @param arg this does something
* @return the Object
* @should do x given some non null
*/
public Object someComplicatedCall(String arg) throws Exception {
...does something interesting here...
}
}
|
2) Use the Eclipse plugin to generate a test method in the matching Test class:
Code Block |
---|
package org.openmrs;
**
* Tests against the java pojo MyObject
*/
public class MyObjectTest {
**
* Get the X output of ComplicatedCall when given a non null argument
*/
@Test
@Verifies(value = "should do x given some non null", method = "someComplicatedCall(String)")
public void someComplicatedCall_shouldDoXGivenSomeNonNull() throws Exception {
MyObject obj = new MyObject();
String output = obj.someComplicatedCall("someArgValue");
Assert.assertNotNull(output);
}
}
|
...
1) Add an @should annotation to the service interface you're testing:
Code Block |
---|
package org.openmrs.api;
**
* This service does blah blah blah...
*/
public class MyService {
.......
**
* This method does something blah blah blah
* @param fname
* @param lname
* @return patients
* @should get patients given valid first name and last name
*/
public List<Patient> findPatient(String fname, String lname) throws Exception { ...does something interesting here... }
}
|
1) Use the Eclipse plugin to generate a test method in the matching Test class in /test/api/org/openmrs/api:
Code Block |
---|
package org.openmrs.api;
**
* Tests on the methods found in MyService
*/
public class MyServiceTest extends BaseContextSensitiveTest {
**
* Check that output looks right for the short patients
*/
@Test
@Verifies(value = "should get patients given valid first name and last name", method = "findPatients(String,String)")
public void findPatients_shouldGetPatientsGivenValidFirstNameAndLastName() throws Exception {
initializeInMemoryDatabase();
executeDataSet("org/openmrs/test/api/myservice/include/patients.xml");
authenticate();
List<Patient> patients = Context.getPatientService().getPatients("John Doe", null, null, false);
Assert.assertEquals(5, patients.size());
}
...
}
|
...
It's easiest to generate the xml off of your current openmrs mysql database. Use the org.openmrs.test.CreateInitialDataSet file to generate it.
You can write data sets by hand as well. Look at unit test data sets in the source code repository for examples.
To load your data set, call
Code Block |
---|
executeDataSet("YourTestDataSyncCreateTest.xml"); // You may wish to add this executeDataSet call to your method annotated with "@Before" so that it runs before each test
|
...
- you're not using an in-memory hsql database and you want to look at the output
you need to check something that happens after the transaction has closed.
Case #1 should only be a temporary test and should not be committed. Case #2 must have a cleanup test if it is to be run with other tests. This cleanup test should delete all the data in the db so that future tests' integrity is not compromised.Code Block @Test @Rollback(false) public void firstTestThatDoesntRollbackThings() { iLeaveDataInTheDatabase(); } @Test @Rollback(false) public void secondTestToCleanUpAfterTheFirst() { try { doSomeMoreTestsHereOnTheNonRolledBackData(); } finally { deleteAllData(); } }
See UserServiceTest.java #shouldCreateUserWhoIsPatientAlready() for an example.
JUnit Environment Variables
...