Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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

...

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 and 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

...

  • 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>
    

...