Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Adding link to Unit Tests page, info I was looking for was there. There seem to be more links to this page, but less info on it. Maybe these two pages should be consolidated as part of cleanup?

...

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

Code Block

/**
* Tests all methods on MyModuleObject
*
*/
public class MyModuleObjectTest {

/**
* Make sure that MyModuleObject runs fine with a null
* parameter to FeatureX
*
* @throws Exception
*/
@Test
public void shouldExamineFeatureXOfMyModuleObject() throws Exception {
 MyModuleObject obj = new MyModuleObject();
 String output = obj.someComplicatedCall("argument1");
 Assert.assertNotNull(output);
 }
}

...

Or at command line:

Code Block

mvn test -Torg.openmrs.mymodule.MyModuleObjectTest

...

(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):

Code Block

<dependency>
    <groupId>org.openmrs.test</groupId>
    <artifactId>openmrs-test</artifactId>
    <version>${openMRSVersion}</version>
    <type>pom</type>
    <scope>test</scope>
</dependency>

and into your api pomand omod poms

Code Block

<dependency>
    <groupId>org.openmrs.api</groupId>
    <artifactId>openmrs-api</artifactId>
    <version>${openMRSVersion}</version>
    <type>test-jar</type>
    <scope>test</scope>
</dependency>

The two dependencies should be placed in the modue (api/omod) pom files for openmrs platform 1.11.x, may not need to update the parent pom file


Create Your Test

Code Block

package org.openmrs.module.yourmodule;
/**
* Tests the MyModuleService class and all of its methods
*
**/
public class MyModuleServiceTest extends BaseModuleContextSensitiveTest {

/**
* Make sure that MyService runs fine with a null
* parameter to getMyModuleObject
*
* @throws Exception
*/
@Test
public void shouldGetMyModuleObjectWithNullParameter() throws Exception {
     MyModuleService service = Context.getService(MyModuleService.class));
     MyModuleObject obj = service.getMyModuleByName(null);
     Assert.assertNull(output);
 }
}

...

(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:

    Code Block

...

  • <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
    
    <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    
    <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
    <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern"
    value="%p - %C{1}.%M(%L) |%d{ISO8601}| %m%n" />
    </layout>
    </appender>
    
    <logger name="org.openmrs.module.remoteformentry">
    <level value="DEBUG" />
    </logger> 
    
    <root>
    <level value="ERROR" />
    <appender-ref ref="CONSOLE" />
    </root>
    
    </log4j:configuration>
    


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

Extras

  • 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
    junit.password=test

...

(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.

Related Links

Unit Tests