Versions Compared

Key

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

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

Getting Started With Simple Unit Tests

...

Place a test like this into your /test/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 MyModuleObject obj = new MyModuleObject();
 String   String output = obj.someComplicatedCall("argument1");
   Assert Assert.assertNotNull(output);
  }
}

2. Run the test

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

...

4. Create Your Testpackage org.openmrs.module.yourmodule;

Code Block
 
/*\*
* 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 = ((MyModuleService)Context.getService(MyModuleService.class));
     MyModuleObject obj = service.getMyModuleByName(null);
     Assert.assertNull(output);
  }
}

5. Run Your Test

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

...

  1. You must have log4j referenced explicitly in your .classpath file
  2. 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 can will look something like:
      Code Block
      <?xml version="1.0" encoding="UTF-8" ?>
      <\!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
      &nbsp;
      <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
      &nbsp;
      <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>
      &nbsp;
      <logger name="org.openmrs.module.remoteformentry">
      <level value="DEBUG" />
      </logger>logger> 
      &nbsp;
      <root>
      <level value="ERROR" />
      <appender-ref ref="CONSOLE" />
      </root>
      &nbsp;
      </log4j:configuration>
      

Including Other Required Modules in JUnit Tests

...