我尝试为 Camunda Spring Boot 应用程序运行 JUnit 测试。我关注了 Testing :
JUnit 4
Using the JUnit 4 style of writing unit tests, the ProcessEngineRule must be used. Through this rule, the process engine and services are available through getters. As with the ProcessEngineTestCase (see above), including this rule will look for the default configuration file on the classpath. Process engines are statically cached over multiple unit tests when using the same configuration resource.
The following code snippet shows an example of using the JUnit 4 style of testing and the usage of the 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 ,因为我必须同步两个配置。有没有其他方法可以使用
ProcessEngineRule
在我的 JUnit 测试中?
最佳答案
您可以使用这样的引擎实例化规则,然后 Camunda 不会尝试读取 cfg.xml
文件:
@Rule
public final ProcessEngineRule camunda = new ProcessEngineRule(myConfiguration.buildProcessEngine());
哪里
myConfiguration
是 ProcessEngineConfiguration
的一个实例您可以随心所欲地创建。如果您使用的是 Spring Boot 运行程序,请尝试导入
camunda-spring-boot-starter-test
依赖并使用 StandaloneInMemoryTestConfiguration
helper :@Rule
public final ProcessEngineRule camunda = new StandaloneInMemoryTestConfiguration().rule();
关于spring-boot - 如何在 Camunda Spring Boot 应用程序的 Junit 测试中使用 ProcessEngineRule?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51669610/