Live Tests NullPointerException and ApplicationContext failure after upgrading from 1.11.x to 2.3.x

Hi

I used to have a module running in version of OpenMRS 1.11.x and run some live tests on it, recently I have upgraded to OpenMRS 2.3.x and I cant run anymore the live tests… here is the error I first got

java.lang.NullPointerException
	at java.util.Hashtable.put(Hashtable.java:460)
	at java.util.Properties.setProperty(Properties.java:166)
	at org.openmrs.test.BaseContextSensitiveTest.getRuntimeProperties(BaseContextSensitiveTest.java:334)
	at org.openmrs.test.BaseContextSensitiveTest.<init>(BaseContextSensitiveTest.java:187)
	at org.openmrs.test.BaseModuleContextSensitiveTest.<init>(BaseModuleContextSensitiveTest.java:24)
	at org.openmrs.module.eptsreports.reporting.intergrated.utils.DefinitionsTest.<init>(DefinitionsTest.java:31)
	at org.openmrs.module.eptsreports.reporting.intergrated.utils.DefinitionsFGHLiveTest.<init>(DefinitionsFGHLiveTest.java:10)
	at org.openmrs.module.eptsreports.LiveTests.<init>(LiveTests.java:57)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:195)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:199)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:252)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:254)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:217)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
	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:68)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
	at org.junit.runners.Suite.runChild(Suite.java:127)
	at org.junit.runners.Suite.runChild(Suite.java:26)
	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.junit.runners.ParentRunner.run(ParentRunner.java:309)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
	at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)

From the error, I started to debug and endup in this classe BaseContextSensitiveTest and from this function getRuntimeProperties I found that this block

            String url = System.getProperty("databaseUrl");
			String username = System.getProperty("databaseUsername");
			String password = System.getProperty("databasePassword");

is returning null for all the getProperty functions. Because my livetest file inherits from BaseContextSensitiveTest at some point, I decided to @Override the function getRuntimeProperties() and set those details (url, username and password) directly in which I got now a different error: Failed to load ApplicationContext java.lang.IllegalStateException: Failed to load ApplicationContext

at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:94)
at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:72)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:212)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:200)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:252)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:254)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:217)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
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:68)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54) 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor#0' defined in class path resource [applicationContext-service.xml]: Cannot resolve reference to bean 'transactionInterceptor' while setting bean property 'transactionInterceptor'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionInterceptor' defined in class path resource [applicationContext-service.xml]: Cannot resolve reference to bean 'transactionManager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [applicationContext-service.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext-service.xml]: Initialization of bean failed; nested exception is java.lang.NullPointerException
Related cause: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'patientService' defined in class path resource [applicationContext-service.xml]: Cannot resolve reference to bean 'patientServiceTarget' while setting bean property 'target'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'patientServiceTarget' defined in class path resource [applicationContext-service.xml]: Cannot resolve reference to bean 'patientDAO' while setting bean property 'patientDAO'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'patientDAO' defined in class path resource [applicationContext-service.xml]: Initialization of bean failed; nested exception is java.lang.NullPointerException

From here I tried to bring the @ContextConfiguration

@ContextConfiguration(
        locations = {"classpath:/TestingApplicationContext.xml",
    "classpath:applicationContext-service.xml",
      "classpath*:moduleApplicationContext.xml"})

But that does not change this last error…

Any Idea how I can do this better or how to have this live tests running

does the error have any other apart ie is this the full log of this error?

Hi @herbert24, Please see the full log here

how does your applicationContext-service.xml look ie you can paste its details on pastibin.com and drop the link on this thread

The simplest solution is to ensure your test is run using the in-memory database, that is, that your test is not run with -DuseInMemoryDatabase=false so that you hit this code path instead of the one that you are currently hitting. Otherwise, it looks like you’ll need to also specify the database connection parameters, e.g., -DdatabaseUrl=, -DdatabaseUsername=, -DdatabasePassword=, -DdatabaseDriver=, and -DdatabaseDialect=.

2 Likes

Hi @psmatsinhe,

I found a workaround for this issue in order to proceed with the live tests. The solution I found was to work with the useInMemoryDatabase method which is the one exposed. All you need to do is to folow the steps below:

  1. Extend from the BaseModuleContextSensitiveTest or even from BaseContextSensitiveTest;

  2. Override the useInMemoryDatabase method as following:

    @Override public Boolean useInMemoryDatabase() { /* * ensure ~/.OpenMRS/openmrs-runtime.properties exists with your properties such as; connection.username=openmrs * connection.url=jdbc:mysql://127.0.0.1:3316/openmrs connection.password=wTV.Tpp0|Q&c */

     final Properties properties = TestUtil.getRuntimeProperties(this.getWebappName());
    
     System.setProperty("databaseUrl", properties.getProperty("connection.url"));
     System.setProperty("databaseUsername", properties.getProperty("connection.username"));
     System.setProperty("databasePassword", properties.getProperty("connection.password"));
    
     System.setProperty("databaseDriver", "com.mysql.jdbc.Driver");
     System.setProperty("databaseDialect", "org.hibernate.dialect.MySQLDialect");
    
     return false;
    

    }

  3. Override baseSetupWithStandardDataAndAuthentication() method just to make sure it is not andding data throw dbUnit():

    @Override public void baseSetupWithStandardDataAndAuthentication() throws SQLException { System.out.println(“Do not add DATA…”); }

  4. You can do the same with deleteAllData() method

And it all it should work like a charm. I hope it helps.

Cheers

1 Like

Hi @steliomo, I was already overriding this methods but with no Luck, and I realized the deleteAllData() had also to be overrided cause it was being called not sure why… It’s working now, thanks!

Hi @psmatsinhe, good it is working

Cheers