ERROR in org.openmrs.api.context.Context

I am trying to test the DHIS2ReportingServiceImpl.

Here is the code to test it:

public class DHIS2ReportingServiceImplTest{

protected final Log log = LogFactory.getLog( getClass() );

private DHIS2ReportingService service;

@Before
public void before()
    throws Exception{
	BasicConfigurator.configure();
    service = Context.getService( DHIS2ReportingService.class );
}

/**
 * @see DHIS2ReportingServiceImpl#evaluateReportDefinition(ReportDefinition,Period,Location)
 * @verifies evaluate Report Definition
 */
@Test
public void evaluateReportDefinition_shouldEvaluateReportDefinition() throws Exception {

	ClassPathResource resource = new ClassPathResource("reportDefinition.xml");
	JAXBContext jaxbContext = JAXBContext.newInstance(ReportTemplates.class);

	Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
	ReportTemplates reportTemplates = (ReportTemplates) jaxbUnmarshaller.unmarshal(resource.getInputStream());
	List<ReportDefinition> rds = reportTemplates.getReportDefinitions();
	DHIS2ReportingServiceImpl impl = new DHIS2ReportingServiceImpl();
	String timeperiod = "2016-06-28";
	Location location = new Location();
	location.setName("County General");
	location.setDescription("desc");
	location.setAddress1("address1");
	Period period = new MonthlyPeriod(new SimpleDateFormat("yyyy-MM-dd").parse(timeperiod));

	for (ReportDefinition rd : rds) {
		System.out.println("ReportDefinition uid = " + rd.getUid() + " name  = " + rd.getName());
		//impl.evaluateReportDefinition(rd, period, location);
               // DHIS2ReportingService service = Context.getService( DHIS2ReportingService.class );
        DataValueSet dvs = service.evaluateReportDefinition( rd, period, location );
	} 
}

}

Here I am the following error.

0 [main] ERROR org.openmrs.api.context.Context  - serviceContext is null.  Creating new ServiceContext()
14 [main] DEBUG org.openmrs.api.context.ServiceContext  - Instantiating service context

My questions are

  1. Is it correct to get DHIS2ReportingService object to test DHIS2ReportingServiceImpl class?
  2. How can I correct the error and get Context and DHIS2ReportingService objects?

I have searched for the similar tests and found DHIS2ReportingServiceDAOTest but here the tests are ignored.

  1. Yes it is okay to do it that way. As a matter of fact the object you are getting will be an instance of a class implementing the interface DHIS2ReportingService interface which in this case is DHIS2ReportingServiceImpl class.

  2. I am not sure what is going on with your test but aren’t you supposed to extend BaseModuleContextSensitiveTest class? i.e as shown below.

public class DHIS2ReportingServiceImplTest extends BaseModuleContextSensitiveTest { ... }

Generally, if you want to test against the live API, your test needs to extend BaseModuleContextSensitiveTest.

Thank You for the reply. :slight_smile:

I tried doing the same as you mentioned and I got the following error. I am not able to understand where the problem is. When I tried to debug it turns out that the following line is causing the error.

public class DHIS2ReportingServiceImplTest extends BaseModuleContextSensitiveTest {

The error stack trace is here:

java.lang.IllegalStateException: Failed to load ApplicationContext
	at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:308)
	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
	at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:220)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:301)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:303)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL [jar:file:/Users/puevigreven/.m2/repository/org/openmrs/module/reporting-api/0.9.7/reporting-api-0.9.7.jar!/org/openmrs/module/reporting/calculation/PatientDataCalculation.class]; nested exception is java.io.FileNotFoundException: class path resource [org/openmrs/calculation/BaseCalculation.class] cannot be opened because it does not exist
	at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:237)
	at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:204)
	at org.springframework.context.annotation.ComponentScanBeanDefinitionParser.parse(ComponentScanBeanDefinitionParser.java:84)
	at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:73)
	at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1335)
	at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1325)
	at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:135)
	at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:93)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:493)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:390)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:212)
	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:81)
	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1)
	at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:280)
	at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:304)
	... 25 more
Caused by: java.io.FileNotFoundException: class path resource [org/openmrs/calculation/BaseCalculation.class] cannot be opened because it does not exist
	at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:158)
	at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:45)
	at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:80)
	at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:101)
	at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:76)
	at org.springframework.core.type.filter.AbstractTypeHierarchyTraversingFilter.match(AbstractTypeHierarchyTraversingFilter.java:105)
	at org.springframework.core.type.filter.AbstractTypeHierarchyTraversingFilter.match(AbstractTypeHierarchyTraversingFilter.java:76)
	at org.springframework.core.type.filter.AbstractTypeHierarchyTraversingFilter.match(AbstractTypeHierarchyTraversingFilter.java:105)
	at org.springframework.core.type.filter.AbstractTypeHierarchyTraversingFilter.match(AbstractTypeHierarchyTraversingFilter.java:76)
	at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.isCandidateComponent(ClassPathScanningCandidateComponentProvider.java:280)
	at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:214)
	... 44 more

Hi @pralay,

The actual problem is:

java.io.FileNotFoundException: class path resource [org/openmrs/calculation/BaseCalculation.class] cannot be opened because it does not exist

So, this guy is missing. I am assuming that this is happening on the API side of your module, could you try adding the dependency bringing the missing class in your api/pom.xml?

<dependency>
  <groupId>org.openmrs.module</groupId>
  <artifactId>calculation-api</artifactId>
</dependency>
1 Like