bddExcel for Java

2024-09-10 13:38:25 浏览数 (3)

本文介绍如何使用Java建立bddExcel实现BDD,注意bddExcel没有用到Cucumber。

1.建立Mave项目

2.修改pom.xml

代码语言:javascript复制
<?xml version="1.0"
encoding="UTF-8"?>
<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>excellBDD</groupId>
<artifactId>excellBDD</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<cucumber.version>4.2.6</cucumber.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>  
       <groupId>com.excelbdd</groupId>  
       <artifactId>excelbdd-java</artifactId>  
       <version>1.3.0</version>
</dependency>

<dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
 <version>5.8.2</version>
 <scope>test</scope>
 </dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
 <configuration>
<encoding>UTF-8</encoding>
<source>11</source>
<target>11</target>
 </configuration>
</plugin>
</plugins>
</build>
</project>

3. 将bdd.xlsx放入项目主目录srctestresources

BDDExcel默认Excel存放目录

注意:Parameter Name暂时不支持中文

4. 建立普通JUnit文件

代码语言:javascript复制
package excellBDD;
           
import java.util.Map;
           
import org.junit.jupiter.api.Assertions;    
import org.junit.jupiter.params.ParameterizedTest;
           
import com.excelbdd.ExcelBDDSource;
import com.excelbdd.TestWizard;
           
public class ExcellBDD {
       private Account account;
      
       @ParameterizedTest
       @ExcelBDDSource(file = "bdd.xlsx", sheet = "Sheet1")
       void testBDDExcel(MapparameterMap) {
              TestWizard w = new TestWizard(parameterMap);
              Double initBalance = w.getDouble("初始余额");
              Double save= w.getDouble("存入额");
              Double Balance =w.getDouble("账户余额");
              this.account= new Account();
              this.account.deposit(initBalance);
              this.account.deposit(save);
              Assertions.assertEquals(Balance,this.account.getBalance());
       }
}

根据代码完善产品代码

代码语言:javascript复制
package excellBDD;
           
public class Account {
       private Double balance = 0.0;
       public Double getBalance(){
              return this.balance;
       }
       public Double deposit(Double value) {
              return this.balance =value;
       }
      
       public Double withdraw(Double value) throws Throwable {
              if (value>this.balance)
                     throw new Throwable("余额不足");
              else
                     return this.balance-=value;
       }
}

0 人点赞