JUnit Test does not work

Hi all,

I cannot manage to set up a Junit test. I used the example as laid out under GitHub - camunda/camunda-engine-unittest: Unit test template project for camunda engine

But I always get the error:

org.camunda.bpm.engine.ProcessEngineException: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
	at org.xxx.apps.myApps.initMYApp(myApps.java:77)
	at org.camunda.bpm.xxx.myprocess.StartExecutionListener.notify(StartExecutionListener.java:23)

I assume that there could be two errors: First of all the error might be with the ProcessEngineException the other one might be that the actual called function initMYApp calls a JDBC Conneciton and loads some data. Do I have to add further information in the .cfg file to access JDBC-Datasource for a Junit Test?

The test class is:

public class SimpleTestCase {

  @Rule
  public ProcessEngineRule rule = new ProcessEngineRule();
  
  @Test
  @Deployment(resources = {"myprocess.bpmn"})
  public void shouldExecuteProcess() {
  
    // Given we create a new process instance
    ProcessInstance processInstance = runtimeService().startProcessInstanceByKey("myprocess", "mybusinesskey);
    
    // Then it should be active
    assertThat(processInstance).isActive();
    // And it should be the only instance
    assertThat(processInstanceQuery().count()).isEqualTo(1);
    // And there should exist just a single task within that process instance
    assertThat(task(processInstance)).isNotNull();

    // When we complete that task
    complete(task(processInstance));
    // Then the process instance should be ended
    assertThat(processInstance).isEnded();
  }

}

The StartExecutionListener Class is

public class StartExecutionListener extends myApps  implements ExecutionListener  {
	
	public void notify(DelegateExecution execution) throws Exception {	
		 initMYApp(execution);
                 ...
    }
	
}

Then the function initMYApp is executed that is part of a separate jar that is included in the POM. The initMYApp is:

public class myApps {
	public void initMYApp(DelegateExecution execution) throws ClassNotFoundException, SQLException, NamingException {

            Vorgang myVorgang = new Vorgang();
	 try {	 
		 myVorgang.VorgangNew(LoadXX); 
                     ^ Loads something out of a JDBC Datasource
	 } catch (Exception e) {
		throw new ProcessEngineException(e);
	 }
}
}

When I start a JUnit-Test I always get the following exception:

shouldExecuteProcess(org.camunda.bpm.unittest.SimpleTestCase)  Time elapsed: 0.903 sec  <<< ERROR!
org.camunda.bpm.engine.ProcessEngineException: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
	at org.xxx.apps.myApps.initMYApp(myApps.java:77)
	at org.camunda.bpm.xxx.myprocess.StartExecutionListener.notify(StartExecutionListener.java:23)
	at org.camunda.bpm.engine.impl.bpmn.delegate.ExecutionListenerInvocation.invoke(ExecutionListenerInvocation.java:36)
	at org.camunda.bpm.engine.impl.delegate.DelegateInvocation.proceed(DelegateInvocation.java:54)
	at org.camunda.bpm.engine.impl.delegate.DefaultDelegateInterceptor.handleInvocationInContext(DefaultDelegateInterceptor.java:87)
	at org.camunda.bpm.engine.impl.delegate.DefaultDelegateInterceptor.handleInvocation(DefaultDelegateInterceptor.java:59)
	at org.camunda.bpm.engine.impl.bpmn.listener.ClassDelegateExecutionListener.notify(ClassDelegateExecutionListener.java:50)
	at org.camunda.bpm.engine.impl.bpmn.listener.ClassDelegateExecutionListener.notify(ClassDelegateExecutionListener.java:34)
	at org.camunda.bpm.engine.impl.core.instance.CoreExecution.invokeListener(CoreExecution.java:95)
	at org.camunda.bpm.engine.impl.core.operation.AbstractEventAtomicOperation.execute(AbstractEventAtomicOperation.java:52)
	at org.camunda.bpm.engine.impl.interceptor.AtomicOperationInvocation.execute(AtomicOperationInvocation.java:89)
	at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.invokeNext(CommandInvocationContext.java:125)
	at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.performNext(CommandInvocationContext.java:104)
	at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.performOperation(CommandInvocationContext.java:79)
	at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.performOperation(CommandInvocationContext.java:69)
	at org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity.performOperationSync(ExecutionEntity.java:622)
	at org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity.performOperationSync(ExecutionEntity.java:597)
	at org.camunda.bpm.engine.impl.core.operation.AbstractEventAtomicOperation.execute(AbstractEventAtomicOperation.java:58)
	at org.camunda.bpm.engine.impl.interceptor.AtomicOperationInvocation.execute(AtomicOperationInvocation.java:89)
	at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.invokeNext(CommandInvocationContext.java:125)
	at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.performNext(CommandInvocationContext.java:104)
	at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.performOperation(CommandInvocationContext.java:79)
	at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.performOperation(CommandInvocationContext.java:69)
	at org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity.performOperationSync(ExecutionEntity.java:622)
	at org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity.performOperationSync(ExecutionEntity.java:597)
	at org.camunda.bpm.engine.impl.core.operation.AbstractEventAtomicOperation.execute(AbstractEventAtomicOperation.java:58)
	at org.camunda.bpm.engine.impl.interceptor.AtomicOperationInvocation.execute(AtomicOperationInvocation.java:89)
	at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.invokeNext(CommandInvocationContext.java:125)
	at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.performNext(CommandInvocationContext.java:104)
	at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.performOperation(CommandInvocationContext.java:79)
	at org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity.performOperation(ExecutionEntity.java:611)
	at org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity.performOperation(ExecutionEntity.java:587)
	at org.camunda.bpm.engine.impl.pvm.runtime.operation.PvmAtomicOperationActivityStartCreateScope.scopeCreated(PvmAtomicOperationActivityStartCreateScope.java:39)
	at org.camunda.bpm.engine.impl.pvm.runtime.operation.PvmAtomicOperationCreateScope.execute(PvmAtomicOperationCreateScope.java:50)
	at org.camunda.bpm.engine.impl.pvm.runtime.operation.PvmAtomicOperationCreateScope.execute(PvmAtomicOperationCreateScope.java:24)
	at org.camunda.bpm.engine.impl.interceptor.AtomicOperationInvocation.execute(AtomicOperationInvocation.java:89)
	at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.invokeNext(CommandInvocationContext.java:125)
	at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.performNext(CommandInvocationContext.java:112)
	at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.performOperation(CommandInvocationContext.java:79)
	at org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity.performOperation(ExecutionEntity.java:611)
	at org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity.performOperation(ExecutionEntity.java:587)
	at org.camunda.bpm.engine.impl.pvm.runtime.PvmExecutionImpl.start(PvmExecutionImpl.java:231)
	at org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity.start(ExecutionEntity.java:432)
	at org.camunda.bpm.engine.impl.cmd.StartProcessInstanceCmd.execute(StartProcessInstanceCmd.java:55)
	at org.camunda.bpm.engine.impl.cmd.StartProcessInstanceCmd.execute(StartProcessInstanceCmd.java:31)
	at org.camunda.bpm.engine.impl.interceptor.CommandExecutorImpl.execute(CommandExecutorImpl.java:24)
	at org.camunda.bpm.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:104)
	at org.camunda.bpm.engine.impl.interceptor.ProcessApplicationContextInterceptor.execute(ProcessApplicationContextInterceptor.java:66)
	at org.camunda.bpm.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:30)
	at org.camunda.bpm.engine.impl.ProcessInstantiationBuilderImpl.executeWithVariablesInReturn(ProcessInstantiationBuilderImpl.java:156)
	at org.camunda.bpm.engine.impl.ProcessInstantiationBuilderImpl.execute(ProcessInstantiationBuilderImpl.java:122)
	at org.camunda.bpm.engine.impl.ProcessInstantiationBuilderImpl.execute(ProcessInstantiationBuilderImpl.java:118)
	at org.camunda.bpm.engine.impl.RuntimeServiceImpl.startProcessInstanceByKey(RuntimeServiceImpl.java:78)
	at org.camunda.bpm.unittest.SimpleTestCase.shouldExecuteProcess(SimpleTestCase.java:25)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
	at org.camunda.bpm.engine.test.ProcessEngineRule$1.evaluate(ProcessEngineRule.java:165)
	at org.junit.rules.RunRules.evaluate(RunRules.java:20)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
	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.junit.runners.ParentRunner.run(ParentRunner.java:309)
	at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
	at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
	at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
	at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
	at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
	at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
	at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
	at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
	at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
	at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350)
	at javax.naming.InitialContext.lookup(InitialContext.java:417)
	at org.xxx.myconnector.dto.dtoXXX.loadXXX(dtoXXX.java:83)
	at org.xxx.myconnector.XXX.loadXXX(Akte.java:44)
	at org.xxx.myconnector.Vorgang.VorgangNew(Vorgang.java:67)
	at org.xxx.apps.myApps.initMYApp(myApps.java:75)
	... 85 more

2 posts were split to a new topic: Delegate expression does not work in unit test

Okay, I found out why it was not working. It is a general issue with JUnit Testing as the database connection that is stored in JDBC must be implemented manually. For everyone who will have this issue, I enclose my BaseClass for all Unit-Tests that set ups the Unittest in a way it is done in the normal environment:

> @BeforeClass
>     public static void setUpClass() throws Exception
>     {
>         // rcarver - setup the jndi context and the datasource
>         try
>         {
>         	System.setProperty("catalina.base", "C:\\camunda\\server\\apache-tomcat-8.0.24\\");
>         	
>             // Create initial context
>             System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
>                 "org.apache.naming.java.javaURLContextFactory");
>             System.setProperty(Context.URL_PKG_PREFIXES,
>                 "org.apache.naming");
>             InitialContext ic = new InitialContext();

>             ic.createSubcontext("java:");
>             ic.createSubcontext("java:comp");
>             ic.createSubcontext("java:comp/env");
>             ic.createSubcontext("java:comp/env/jdbc");

>             Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
>             
>             SQLServerConnectionPoolDataSource testDS = new SQLServerConnectionPoolDataSource();
>             testDS.setServerName("XXX");
>             testDS.setPortNumber(1433);
>             testDS.setDatabaseName("XXX");
>             testDS.setUser("XXX")	;
>             
>             testDS.setPassword(XX);
>             testDS.setEncrypt(false);
>             testDS.setURL("jdbc:sqlserver://XXXXServer:1433;databaseName=XXX;user=XX;password=XXX;maxWaitMillis=10000;removeAbandonedTimeout=300;maxTotal=25");
>             //testDS.setIntegratedSecurity(false);
>             testDS.setTrustServerCertificate(true);
>             ic.bind("java:comp/env/jdbc/ramDataSource", testDS);

>         }
>         catch (NamingException ex)
>         {
>             
>         }

>     }