首頁 > Java > java教程 > 主體

使用Junit的Maven專案 - 檢查銀行帳號

王林
發布: 2023-08-29 14:33:02
轉載
1432 人瀏覽過

使用Junit的Maven项目 - 检查银行账号

所有的應用程序,無論大小,都需要經歷原始程式碼的建置、生成、編譯和運行的一系列過程。這組過程由程式設計師手動執行。然而,隨著 Apache Maven 專案的啟動,所有這些流程都可以自動化,避免手動工作。因此,maven專案是一個開源工具,用於一次建置和部署多個項目,以提供更好的專案管理。

在本文中,我們將討論用於檢查銀行帳號是否有效並使用 Junit 進行測試的 Maven 專案。

Junit 是什麼?

JUnit 是一個開源單元測試框架,被世界各地的 Java 語言組織使用。在Java語言中,每次加入新的程式碼,都需要重新執行測試案例,這樣的功能是由Junit框架來實現的。它用於用Java語言編寫和執行自動化測試案例。

用於檢查銀行帳號的 Maven 項目

每當我們處理銀行軟體或相關應用程式時,一件強制性的事情就是驗證帳號。要使帳號有效,需要滿足三個條件。

三個條件如下 -

  • 銀行帳號只能包含 14 位數字。

  • 帳號中的 14 位數字不能全部為零。

  • 帳號欄位不能為空或為空。

現在,讓我們在 Maven 專案中編寫滿足所有這三個條件的業務邏輯。

演算法

  • 步驟1 - 首先建立一個資料夾BankingAccountNoServices,其中包含名為BankingAccountNoServices.java 的Java 文件,用於編寫業務邏輯,第二個TestBankingAccountNoServices.java 用於測試業務邏輯。

  • 步驟 2 - 建立另一個文件 pom.xml,它是一個 xml 文件,包含 Maven 專案的專案和設定詳細資訊。

  • 第 3 步 - 取得正面成果的關鍵因素是在 pom.xml 檔案中記錄相關專案和設定資訊。

  • 第 4 步 - 透過滿足驗證帳號所需的所有必要條件來編寫業務邏輯。

  • 步驟 5 - 在 Test BankingAccountNoServices.java 檔案中使用 Junit 編寫單元測試案例。

在繼續操作之前應檢查 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 中的正規表示式來撰寫業務邏輯。

方法一:使用Long.parseLong

由於帳號應該是14位數字,因此我們使用Long.parseLong函數將其轉換為long類型,然後檢查三個必要條件。

範例

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;
      }
   }
}
登入後複製

在上面的程式碼中,我們首先檢查帳號是否為 null 或空,其次檢查帳號的長度是否為 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;
      }
   }
}
登入後複製

在上面的程式碼中,我們首先檢查帳號是否為 null 或空,其次檢查帳號的長度是否為 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;
      }
   }
}
登入後複製

在上面的程式碼中,我們首先檢查帳號是否為null 或空,其次檢查帳號的長度是否為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 中銀行帳號的驗證。

以上是使用Junit的Maven專案 - 檢查銀行帳號的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!