Maven Integration Tests incompatibity with ProcessEngineRule

Guys:

I coding integration tests, starting a tomcat at integration-test phase in Maven. This is a small sample of my config:

<plugin>
	<groupId>org.codehaus.cargo</groupId>
	<artifactId>cargo-maven2-plugin</artifactId>
	<version>1.6.0</version>
	<configuration>
		<skip>${skipTests}</skip>
		...
		...
	</configuration>

	<executions>
		<execution>
			<id>start-container</id>
			<phase>pre-integration-test</phase>
			<goals>
				<goal>start</goal>
				<goal>deploy</goal>
			</goals>
			<configuration>
				<wait>false</wait>
			</configuration>
		</execution>
		<execution>
			<id>stop-container</id>
			<phase>post-integration-test</phase>
			<goals>
				<goal>stop</goal>
			</goals>
		</execution>
	</executions>
</plugin>

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-surefire-plugin</artifactId>
	<version>2.18.1</version>
	<configuration>
		<skip>true</skip>
	</configuration>

	<executions>
		<execution>
			<phase>integration-test</phase>
			<goals>
				<goal>test</goal>
			</goals>
			<configuration>
				<skip>false</skip>
				<includes>
					<include>**/*.java</include>
				</includes>
			</configuration>
		</execution>
	</executions>
</plugin>

By the other side, I have this test:

import com.credicuotas.enginecredito.config.EngineConstants;

/**
 * Test case starting an in-memory database-backed Process Engine.
 */
public class InMemoryH2Test {

	private static Logger logger = Logger.getLogger(InMemoryH2Test.class.getName());
	

	@Rule
	public ProcessEngineRule rule = new ProcessEngineRule();

	private static final String PROCESS_DEFINITION_KEY = "credicuotas-engine-credito";

	@Before
	public void setup() {
		init(rule.getProcessEngine());
	}

	/**
	 * Just tests if the process definition is deployable.
	 */
	@Test
	@Deployment(resources = "process.bpmn")
	@RequiredHistoryLevel(ProcessEngineConfiguration.HISTORY_ACTIVITY)
	public void test1() {
		
		logger.info("----------------------------------------------");
		Map<String, Object> varDniOK = new HashMap<String, Object>();
		varDniOK.put(EngineConstants.ENG_DNI, "31042469");
		varDniOK.put(EngineConstants.ENG_CORREO, "jgomez@gmail.com");
		varDniOK.put(EngineConstants.ENG_CLAVE, "1234");
		ProcessInstance dniOK = processEngine().getRuntimeService().startProcessInstanceByKey(PROCESS_DEFINITION_KEY, varDniOK);
		
		Integer cantidad = (Integer) rule.getHistoryService().createHistoricVariableInstanceQuery().variableName(EngineConstants.ENG_CANTIDAD_DNI).singleResult().getValue();

	}
	
	@Test
	@Deployment(resources = "process.bpmn")
	@RequiredHistoryLevel(ProcessEngineConfiguration.HISTORY_ACTIVITY)
	public void test2() {
		
		logger.info("----------------------------------------------");
		Map<String, Object> varDniOK = new HashMap<String, Object>();
		varDniOK.put(EngineConstants.ENG_DNI, "31042469");
		varDniOK.put(EngineConstants.ENG_CORREO, "jgomez@gmail.com");
		varDniOK.put(EngineConstants.ENG_CLAVE, "1234");
		ProcessInstance dniOK = processEngine().getRuntimeService().startProcessInstanceByKey(PROCESS_DEFINITION_KEY, varDniOK);
		
		Integer cantidad = (Integer) rule.getHistoryService().createHistoricVariableInstanceQuery().variableName(EngineConstants.ENG_CANTIDAD_DNI).singleResult().getValue();
		
	}
	
	@After
	public void calculateCoverageForAllTests() throws Exception {
		ProcessTestCoverage.calculate(rule.getProcessEngine());
	}

}

So far so good !!

The problem is … Adding JUnit @Rule annotation in the test and using it causes cargo maven plugin to stop Tomcat prematurely. If I execute 10 tests with JUnit commenting this lines:

@Rule
public ProcessEngineRule rule = new ProcessEngineRule();

Tomcat starts, all tests run, and Tomcat stops. Adding above line causes to Tomcat stop after finishing first test. I read a LOT about cargo maven plugin, I’ve checked all your example tests to use JUnit 3 and … it happens the same thing. It seems to be a relation between process engine process and a servlet container process…

Any ideas ?

HI @Maximiliano_Carrizo,

Can you please share a simple, executable project on github that reproduces the behavior?

Thanks,
Thorben

Hi @Maximiliano_Carrizo,

I think you shouldn’t mix unit- and integration-testing.
My advice would be that you stay with JUnit for Unit tests (using the ProcessEngineRule) and use for example Arquillian to startup the Tomcat in the integration phase, executing ITs with Arquillian in the container.
I don’t know if you use the Camunda-provided Tomcat or a vanilla one but when you use the ProcessEngineRule, it will instantiate a ProcessEngine in the JVM running the JUnit Tests and not in the Tomcat JVM. Also when a test is finished it will cleanup the database.

Cheers,
Christian

2 Likes

Christian, Thorben:

Im using Unit Test … problem is I need ANOTHER webapp started to run it !! Ha ha !!

I solved problem with a workaround … Im using BeforeClass and AfterClass to start a jetty that manually starts that server. Thanks for all !