我尝试为Camunda Spring Boot应用程序运行JUnit测试。我关注了Testing:
JUnit 4
使用JUnit 4风格编写单元测试,必须使用ProcessEngineRule。通过此规则,可以通过getter获得流程引擎和服务。与ProcessEngineTestCase一样(请参见上文),包括此规则将在类路径上查找默认配置文件。使用相同的配置资源时,流程引擎会静态地缓存在多个单元测试中。
以下代码段显示了使用JUnit 4测试风格以及ProcessEngineRule用法的示例。
public class MyBusinessProcessTest { @Rule public ProcessEngineRule processEngineRule = new ProcessEngineRule(); @Test @Deployment public void ruleUsageExample() { RuntimeService runtimeService = processEngineRule.getRuntimeService(); runtimeService.startProcessInstanceByKey("ruleUsage"); TaskService taskService = processEngineRule.getTaskService(); Task task = taskService.createTaskQuery().singleResult(); assertEquals("My Task", task.getName()); taskService.complete(task.getId()); assertEquals(0, runtimeService.createProcessInstanceQuery().count()); } }
但是我得到一个错误:
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [activiti.cfg.xml]; nested exception is java.io.FileNotFoundException: class path resource [activiti.cfg.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:344)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
at org.camunda.bpm.engine.impl.cfg.BeansConfigurationHelper.parseProcessEngineConfiguration(BeansConfigurationHelper.java:35)
at org.camunda.bpm.engine.impl.cfg.BeansConfigurationHelper.parseProcessEngineConfigurationFromResource(BeansConfigurationHelper.java:50)
at org.camunda.bpm.engine.ProcessEngineConfiguration.createProcessEngineConfigurationFromResource(ProcessEngineConfiguration.java:305)
at org.camunda.bpm.engine.ProcessEngineConfiguration.createProcessEngineConfigurationFromResource(ProcessEngineConfiguration.java:301)
at org.camunda.bpm.engine.impl.test.TestHelper.getProcessEngine(TestHelper.java:428)
at org.camunda.bpm.engine.test.ProcessEngineRule.initializeProcessEngine(ProcessEngineRule.java:175)
at org.camunda.bpm.engine.test.ProcessEngineRule.apply(ProcessEngineRule.java:154)
at org.junit.rules.RunRules.applyAll(RunRules.java:26)
at org.junit.rules.RunRules.<init>(RunRules.java:15)
at org.junit.runners.BlockJUnit4ClassRunner.withTestRules(BlockJUnit4ClassRunner.java:400)
at org.junit.runners.BlockJUnit4ClassRunner.withRules(BlockJUnit4ClassRunner.java:356)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:278)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
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:538)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: java.io.FileNotFoundException: class path resource [activiti.cfg.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330)
... 27 more
我可以添加activiti.cfg.xml
,但是我的Camunda Spring Boot应用程序不会使用它。只是将文件添加到非测试源以进行测试,这是错误的。这也将违反DRY,因为我必须同步两个配置。
在我的JUnit测试中还有其他使用ProcessEngineRule
的方法吗?
答案 0 :(得分:2)
您可以使用这样的引擎实例化规则,然后Camunda不会尝试从cfg.xml
文件中进行读取:
@Rule
public final ProcessEngineRule camunda = new ProcessEngineRule(myConfiguration.buildProcessEngine());
其中myConfiguration
是您创建的ProcessEngineConfiguration
的实例。
如果您使用的是Spring Boot运行器,请尝试导入camunda-spring-boot-starter-test
依赖项,并为此使用StandaloneInMemoryTestConfiguration
帮助器:
@Rule
public final ProcessEngineRule camunda = new StandaloneInMemoryTestConfiguration().rule();
答案 1 :(得分:0)
您在应用程序中使用@EnableProcessApplication
吗?如果是,我认为您实际上不再需要org.camunda.bpm.engine.test.ProcessEngineRule
:
@Autowired RuntimeService runtimeService;
BpmPlatform.getDefaultProcessEngine()
但是无论如何,如果您真的想使用该规则:
编写自己的课程:
public class MyProcessEngineRule extends ProcessEngineRule {
@Override
public Statement apply(Statement base, Description description) {
// you can use this, if you use AbstractAssertions
// super.processEngine = AbstractAssertions.processEngine();
super.processEngine = processEngine();
return super.apply(base, description);
}
public static ProcessEngine processEngine() {
Map<String, ProcessEngine> processEngines = ProcessEngines.getProcessEngines();
if (processEngines.size() == 1) {
return processEngines.values().iterator().next();
} else {
throw new IllegalStateException("some usefule message");
}
}
}
并在测试类中使用@Rule public MyProcessEngineRule rule = new MyProcessEngineRule();
。
至少对我有用...