「DAY22」Camunda流程測試

Posted on October 6, 2023 • 2 min read • 241 words
Share via

今天我們要來對Camunda流程進行功能性測試,廢話不多少,我們直接開始。

今天我們要來對Camunda流程進行功能性測試,廢話不多說,我們直接開始。

UT測試

Unit Test(UT)用於驗證單一程式碼單元的正確性,Java中,JUnit是最常用的單元測試框架,可以幫助開發人員編寫和運行測試案例。

確定測試範圍

我們針對流程分之進行測試,通過分析流程,我們只需分析幾個分支:

  1. 檢查資料完整性,有兩個分支。

編寫測試代碼

我們透過IDEA中的TEST生成功能幫我們生成一個模板進行測試,我們首先要切換到「Form_IntegrityCheck」。

https://ithelp.ithome.com.tw/upload/images/20231006/20163254HvYk79XgpB.png

點擊右鍵,選擇「Generate」。

https://ithelp.ithome.com.tw/upload/images/20231006/20163254UeGSV3SB6R.png

點選Generate > Test

https://ithelp.ithome.com.tw/upload/images/20231006/20163254rLTsypnZKQ.png

我們就可以看到,勾選要測試的方法,點選 OK。

https://ithelp.ithome.com.tw/upload/images/20231006/201632543rVjSYZ0hj.png

接著我們將Junit加入到pom.xml,透過IDEA的提示完成。

https://ithelp.ithome.com.tw/upload/images/20231006/20163254e63NPMODK6.png

失敗的測試(不要使用)

筆者對Junit不熟,試了一整天還是無法啟動,跟大家說聲抱歉,不過我還是把失敗的程式碼放到文章中,避免大家踩坑。

Form_IntegrityCheck.java

package com.example.form_IntegrityCheck;
import org.camunda.bpm.scenario.ProcessScenario;
import org.camunda.bpm.scenario.Scenario;
import org.camunda.bpm.engine.test.Deployment;
import org.camunda.bpm.engine.variable.Variables;
import org.camunda.bpm.scenario.run.ProcessRunner;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import static org.mockito.Mockito.verify;

import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
public class Form_IntegrityCheckTest {
    private static final String PROCESS_DEFINITION_KEY = "test";

    private Map<String, Object> variables;

    @Mock
    private ProcessScenario myProcess;

    @Test
    @Deployment(resources = "main.bpmn")
    public void testNeedMoreDocuments() {
        variables = Variables.createVariables();
        variables.put("id", "id");
//    variables.put("name", "name");

        ProcessRunner.ExecutableRunner starter = Scenario.run(myProcess).startByKey(PROCESS_DEFINITION_KEY, variables);

        // OK - everything prepared - let's go and execute the scenario
        Scenario scenario = starter.execute();

        // now you can do some assertions
        verify(myProcess).hasFinished("EndEvent_NeedMoreDocument");
    }

}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example.workflow</groupId>
  <artifactId>my-project</artifactId>
  <version>1.0.0-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
  </properties>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.7.9</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>

      <dependency>
        <groupId>org.camunda.bpm</groupId>
        <artifactId>camunda-bom</artifactId>
        <version>7.19.0</version>
        <scope>import</scope>
        <type>pom</type>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.2.3</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>

    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>

    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <scope>test</scope>

    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
  </dependency>
    <dependency>
      <groupId>org.camunda.bpm.extension</groupId>
      <artifactId>camunda-bpm-assert-scenario</artifactId>
      <version>0.2</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-all</artifactId>
      <version>1.8.4</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.camunda.bpm.springboot</groupId>
      <artifactId>camunda-bpm-spring-boot-starter-rest</artifactId>
    </dependency>

    <dependency>
      <groupId>org.camunda.bpm.springboot</groupId>
      <artifactId>camunda-bpm-spring-boot-starter-webapp</artifactId>
    </dependency>

    <dependency>
      <groupId>org.camunda.bpm</groupId>
      <artifactId>camunda-engine-plugin-spin</artifactId>
    </dependency>

    <dependency>
      <groupId>org.camunda.spin</groupId>
      <artifactId>camunda-spin-dataformat-all</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-api</artifactId>
      <version>5.9.3</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-test</artifactId>
      <version>3.1.4</version>
      <scope>test</scope>
    </dependency>

  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>2.7.9</version>
      </plugin>
    </plugins>
  </build>

</project>
如果有任何問題歡迎在下方留言!! 筆者頭一回寫技術文如果內容有誤或者內容的呈現上有所缺陷如果您願意歡迎在下方留言給我呦~~

anything.best

這是我的部落格,歡迎點擊閱覽喔~~會不定期更新文章

Follow me

I work on everything coding and tweet developer memes