Maven 專案與 Junit – 檢查銀行賬戶號碼
所有應用程式,無論大小,都需要經歷一系列構建、生成、編譯和執行原始碼的過程。這些過程由程式設計師手動執行。然而,隨著 Apache Maven 專案的推出,所有這些過程都可以自動化,從而避免手動操作。因此,Maven 專案是一個開源工具,用於同時構建和部署多個專案,以提供更好的專案管理。
在本文中,我們將討論 Maven 專案,用於檢查銀行賬戶號碼是否有效,並使用 Junit 進行測試。
什麼是 Junit?
JUnit 是一個開源的單元測試框架,全球各地的組織都在 Java 語言中使用它。在 Java 語言中,每次新增新程式碼時,都需要重新執行測試用例,而 Junit 框架實現了此功能。它用於編寫和執行 Java 語言中的自動化測試用例。
用於檢查銀行賬戶號碼的 Maven 專案
無論何時處理銀行軟體或相關應用程式,驗證賬戶號碼都是一項必不可少的工作。要使賬戶號碼有效,需要滿足三個條件。
這三個條件如下:
銀行賬戶號碼應僅包含 14 位數字。
賬戶號碼中的所有 14 位數字均不能為零。
賬戶號碼欄位不能為空或 null。
現在,讓我們在 Maven 專案中編寫業務邏輯,以滿足所有這三個條件。
演算法
步驟 1 – 首先建立一個名為 BankingAccountNoServices 的資料夾,其中包含名為 BankingAccountNoServices.java 的 Java 檔案(用於編寫業務邏輯)和第二個名為 TestBankingAccountNoServices.java 的檔案(用於測試業務邏輯)。
步驟 2 – 建立另一個名為 pom.xml 的檔案,這是一個 xml 檔案,包含 Maven 專案的專案和配置詳細資訊。
步驟 3 – 在 pom.xml 檔案中記錄相關的專案和配置資訊是取得積極成果的關鍵因素。
步驟 4 – 透過滿足驗證賬戶號碼所需的所有必要條件來編寫業務邏輯。
步驟 5 – 使用 Junit 在 Test BankingAccountNoServices.java 檔案中編寫單元測試用例。
在繼續操作之前,應檢視 pom.xml 檔案的內容。它在所有討論的方法中保持一致,幷包含 Maven 專案的重要配置詳細資訊。
示例
<?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>com.example.BankingAccountNoServices </groupId>
<artifactId>BankingAccountNoServices </artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>5.3.1</junit.version>
<pitest.version>1.4.3</pitest.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>maven-mutation-testing</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M1</version>
</plugin>
<plugin>
<groupId>org.pitest</groupId>
<artifactId>pitest-maven</artifactId>
<version>${pitest.version}</version>
<executions>
<execution>
<id>pit-report</id>
<phase>test</phase>
<goals>
<goal>mutationCoverage</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.pitest</groupId>
<artifactId>pitest-junit5-plugin</artifactId>
<version>0.8</version>
</dependency>
</dependencies>
<configuration>
<targetClasses>
<param>com.example.BankingAccountNoServices.* BankingAccountNoServices *</param>
</targetClasses>
<targetTests>
<param>com.example.BankingAccountNoServices.*</param>
</targetTests>
</configuration>
</plugin>
</plugins>
</build>
</project>
以上 pom.xml 程式碼包含 Maven 專案所需的所有專案和配置詳細資訊。
方法
方法 1 – 在此方法中,我們將使用 Long.parseLong 檢視業務邏輯。
方法 2 – 在此方法中,我們將使用 Character.isDigit() 函式編寫業務邏輯。
方法 3 – 在此方法中,我們將使用 Java 中的正則表示式編寫業務邏輯。
方法 1:使用 Long.parseLong
由於賬戶號碼應為 14 位數字,因此我們使用 Long.parseLong 函式將其轉換為長型別,然後檢查三個必要條件。
示例
import java.util.*;
public class BankingAccountNoServices {
public boolean isValid1(String accNo) {
if (accNo == null || accNo.equalsIgnoreCase("")) {
return false;
}
try {
Long.parseLong(accNo);
if (accNo.length() == 14) {
int c = 0;
int n = accNo.length();
for (int i = 0; i < n; i++) {
if (accNo.charAt(i) == '0') {
c += 1;
}
}
if (c == 14) {
return false;
} else {
return true;
}
} else {
return false;
}
}
catch (NumberFormatException exception) {
return false;
}
}
}
在上面的程式碼中,我們首先檢查了賬戶號碼是否為空或非空,其次,我們檢查了賬戶號碼的長度是否為 14,然後計算了其中零的數量。如果所有 14 位數字都為零,則返回 false,否則返回 true。
現在,讓我們看看使用 JUnit 的單元測試用例。
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class TestBankingAccountNoServices {
public void testForBankAccountNo() {
BankingAccountNoServices ob = new BankingAccountNoServices();
assertEquals(false, ob.isValid1(null));
assertEquals(false, ob.isValid1("8378939"));
assertEquals(true, ob.isValid1("67874864837684"));
assertEquals(true, ob.isValid1("23451234543214"));
}
}
在上面的程式碼中,我們檢查了 4 個不同的單元測試用例,用於驗證賬戶號碼。
方法 2:使用 Character.isDigit()
在此方法中,我們將使用 Character.isDigit() 函式檢查賬戶號碼。我們將檢查所有三個必要條件以驗證賬戶號碼。
示例
import java.util.*;
public class BankingAccountNoServices {
public boolean isValid2(String accNo){
if (accNo == null || accNo.equalsIgnoreCase("")) {
return false;
}
if (accNo.length() == 14) {
int c = 0;
for (int i = 0; i < accNo.length(); i++) {
if (!Character.isDigit(accNo.charAt(i))) {
return false;
}
if (accNo.charAt(i) == '0') {
c += 1;
}
}
if (c == 14) {
return false;
} else {
return true;
}
} else {
return false;
}
}
}
在上面的程式碼中,我們首先檢查了賬戶號碼是否為空或非空,其次,我們檢查了賬戶號碼的長度是否為 14,然後檢查了 accNo 變數的字元是否為數字。第三,檢查數字中是否存在零。
現在,讓我們看看使用 Junit 的單元測試用例。
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class TestBankingAccountNoServices {
public void testForBankAccountNo() {
BankingAccountNoServices ob = new BankingAccountNoServices();
assertEquals(false, ob.isValid2(""));
assertEquals(false, ob.isValid2("00000000000000"));
assertEquals(true, ob.isValid2("67874864837684"));
assertEquals(true, ob.isValid2("34324353488345"));
}
}
在上面的程式碼中,我們檢查了 4 個不同的單元測試用例,用於驗證賬戶號碼。
方法 3:使用正則表示式模式
在此方法中,我們為數字定義了一個正則表示式模式,並檢查驗證賬戶號碼所需的全部三個必要條件。
示例
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class BankingAccountNoServices {
public boolean isValid3(String accNo) {
if (accNo == null || accNo.equalsIgnoreCase("")) {
return false;
}
if (accNo.length() == 14) {
int c = 0;
String r = "[0-9]+";
Pattern p = Pattern.compile(r);
Matcher matcher = p.matcher(accNo);
if (matcher.matches()) {
for (int i = 0; i < accNo.length(); i++) {
if (accNo.charAt(i) == '0') {
c += 1;
}
}
if (c == 14) {
return false;
} else {
return true;
}
} else {
return false;
}
} else {
return false;
}
}
}
在上面的程式碼中,我們首先檢查了賬戶號碼是否為空或非空,其次,我們檢查了賬戶號碼的長度是否為 14,然後定義了一個數字的正則表示式,並使用 Pattern 和 Matcher 類檢查了三個必要條件。
現在,讓我們看看使用 JUnit 的單元測試用例。
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class TestBankingAccountNoServices {
public void testForBankAccountNo() {
BankingAccountNoServices ob = new BankingAccountNoServices();
assertEquals(false, ob.isValid3("47283"));
assertEquals(false, ob.isValid3("19037293284s32"));
assertEquals(true, ob.isValid3("67874864837684"));
assertEquals(true, ob.isValid3("34521678954632"));
}
}
在上面的程式碼中,我們檢查了 4 個不同的單元測試用例,用於驗證賬戶號碼。
結論
在本文中,我們使用 Junit 建立了一個 Maven 專案,專門用於檢查銀行賬戶號碼。我們討論了三種不同的方法來編寫用於驗證銀行賬戶號碼的業務邏輯,分別使用 Long.parseLong、Character.isDigit() 和正則表示式模式。任何一種方法都可以用於在 Java 中執行銀行賬戶號碼的驗證。
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP