OpenMRS Worldwide Summit is fast approaching!

#OMRS15, the 1st OpenMRS Worldwide Summit, is 8-14 December. Registration is open & session ideas are being accepted. More information:

Wiki Spaces


Get Help from Others

Q&A: Ask OpenMRS »
Discussion: OpenMRS Talk »
Real-Time: IRC Chat


Skip to end of metadata
Go to start of metadata

This page describes how to create unit tests inside of a module. For details on writing unit tests in trunk, see the main Testing page.

Also check out Module Testing against multiple versions of OpenMRS to see how to configure your module poms to allow testing against different versions of OpenMRS core.

Getting Started With Simple Unit Tests

1. Create Your Test

Place a test like this into your /omod/src/test/java/org/openmrs/module/yourmodule folder.package org.openmrs.module.yourmodule;

2. Run the test

(In Eclipse) Right click on your new class and select Run As --> Unit Test

Or at command line:

Getting Started With Service Unit Tests

(or anything using the OpenMRS "Context" object) (or anything using hibernate in your module)

Check your pom.xml files

If you use the Maven archetype to create your module, see this. If you are using an older mavenized or unmavenized module, see this.

Put these into your main pom.xml (in Eclipse, open the pom choose the Dependencies tab and add a new one below those that exist):

and into your api and omod poms

Create Your Test

Run Your Test

(In Eclipse) Right click on your new class and select Run As --> Unit Test

(The BaseModuleContextSensitiveTest class will run through the Spring setup, loads in any omods on the classpath, creates the Context and ServiceContext classes required by the OpenMRS API. This startup takes a few seconds, so when you can, create simple tests that don't need Context and don't extend BaseModuleContextSensistiveTest)

Logging in JUnit Module Tests

This information is intended for older mavenized or unmavenized modules, it has not been verified using the Maven archetype

  • You must have log4j referenced explicitly in your .classpath file
  • log4j.xml's parent folder must be in your classpath in order to be found by log4j (like in /metadata or /dist)
  • The log4j.xml will look something like:

Including Other Required Modules in JUnit Tests

  • For modules using the Maven archetype, you will need to do a clean install on your dependent modules.
  • For older mavenized or unmavenized modules, package the required module into its omod file and drop it into the /lib-common folder


  • If you've added a log4j.xml file to your metadata directory and metadata is referenced in the .classpath file and you still don't see any log messages in the console during the execution of the test, make sure that the metadata directory comes before all JARs that might have a log4j.xml in them already (i.e. openmrs-api.jar)
  • If you create custom tables and map them using hibernate, an error in the <table_name>.hbm.xml can be masked and you will just get an error to the effect that your service cannot be found when you call Context.getService(<your_service>.class). If you compile the module and load it into the OpenMRS web interface, it will tell you the real error.
  • If you get an exception like org.openmrs.api.APIException: Service not found: class org.openmrs.module. ... then you may need to do one of two things. First, make sure that you have run the ant "package module" task. If you've already done that, then you probably need to fix your build file for the module you're including.
    1. Open build.xml in the module you're compiling
    2. Find the "package-jar" target
    3. Remove the line like "<exclude name="*" />"
    4. Repackage your jar file and try using it again in the other module
  • To skip the authentication username/popup when testing, place these variables into your runtime properties file: junit.username=admin


org.dbunit.dataset.NoSuchTableException: (my_table_name)

You need to make sure that (1) you have built an omod for your module, (2) thedistfolder is in your classpath, e.g. by adding this line to your.classpathfile:<classpathentry kind="lib" path="dist"/>. You may also need to refresh your eclipse Package Explorer or Navigator so it knows about the dist folder.

org.openmrs.api.APIException: Service not found: class org.openmrs.module.

First, make sure that you have run the package module Ant target. Secondly, make sure your test class extendsBaseModuleContextSensitiveTest. Second, make sure your "build" folder was on the .classpath so that Eclipse knows about your compiled classes

org.hibernate.MappingException: Unknown entity: org.openmrs.module.<module>.<Object>

Hibernate will only look for your module's Hibernate mapping files (.hbm.xml) in classpath:*.omod. You must have the reference to <classpathentry kind="lib" path="dist"/> in your module's .classpathfile (see above). NOTE: Since /dist is now a referenced library, you might also need to make sure it is present at compilation time (Ant-clean deletes it). Rearrange your build.xml to create /dist before the compilation call. You may need to do an Eclipse clean (which is different than an Ant clean) and then repeat this step.

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name '<JUnit Test Class>': Unsatisfied dependency expressed through bean property 'transactionManager': Set this property value or disable dependency checking for this bean.

(When running unit tests in Eclipse) Make sure that you are using Spring 2.5 or above. You can also addsetDependencyCheck(false) to the constructor of your test class.