Hey,
today I’m gonna write a few sentences about JUnit tests for OpenMRS modules that use the Context object. I’m sorry that I can’t go into detail, but I don’t have much time since I have to learn for several exams 
So make sure you check out the links I put in resources!
1 Classpath
This one is very important, it held me up for several days. Before writing your tests you should include all libs from openmrs-1.6.x (for example) in your module. If you created your module from the Basic Module you should delete it’s libs, because they would interfere with our new libs.
Now we need three more libs that we can build from the openmrs source:
- openmrs-api-1.6.2.13894-dev.jar
- tests-openmrs-api-1.6.2.13894-dev.jar
- web-openmrs-api-1.6.2.13894-dev.jar
There is an Ant-task to do that. In the resources is a link to my classpath file as an example.
Justin also told me, that I don’t have to copy all those lib files from openmrs-1.6.x into my modules svn folder. There is a way to create ‘link’ that folder to the openmrs-1.6.x folder in svn, but I haven’t tried that yet.
2 Writing your test
Now you can write your tests as ever. But you have to extend BaseModuleContextSensitiveTest class. Here is an example:
public class SQLQueryTest extends BaseModuleContextSensitiveTest {
@Test
public void testConceptQuery() {
AdministrationService adminService = Context.getAdministrationService();
Assert.assertNotNull(adminService);
Object conc = adminService.executeSQL("SELECT count(*) FROM obs", false);
Assert.assertNotNull(conc);
}
}
3 More help
For more help visit the first link mentioned below!
Resources
Module Unit Testing – More information about unit testing from the OpenMRS wiki
OpenMRS-1.6.x libs – All the libs you need
Example Classpath File – My classpath file