新聞中心
在這里你將會看到一個應(yīng)用 POJO 類,Business logic 類和在 test runner 中運行的 test 類的 JUnit 測試的例子。

創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供南安網(wǎng)站建設(shè)、南安做網(wǎng)站、南安網(wǎng)站設(shè)計、南安網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、南安企業(yè)網(wǎng)站模板建站服務(wù),10余年南安做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。
在 C:\ > JUNIT_WORKSPACE 路徑下創(chuàng)建一個名為 EmployeeDetails.java 的 POJO 類。
public class EmployeeDetails {
private String name;
private double monthlySalary;
private int age;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the monthlySalary
*/
public double getMonthlySalary() {
return monthlySalary;
}
/**
* @param monthlySalary the monthlySalary to set
*/
public void setMonthlySalary(double monthlySalary) {
this.monthlySalary = monthlySalary;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
}EmployeeDetails 類被用于
- 取得或者設(shè)置雇員的姓名的值
- 取得或者設(shè)置雇員的每月薪水的值
- 取得或者設(shè)置雇員的年齡的值
在 C:\ > JUNIT_WORKSPACE 路徑下創(chuàng)建一個名為 EmpBusinessLogic.java 的 business logic 類
public class EmpBusinessLogic {
// Calculate the yearly salary of employee
public double calculateYearlySalary(EmployeeDetails employeeDetails){
double yearlySalary=0;
yearlySalary = employeeDetails.getMonthlySalary() * 12;
return yearlySalary;
}
// Calculate the appraisal amount of employee
public double calculateAppraisal(EmployeeDetails employeeDetails){
double appraisal=0;
if(employeeDetails.getMonthlySalary() < 10000){
appraisal = 500;
}else{
appraisal = 1000;
}
return appraisal;
}
}EmpBusinessLogic 類被用來計算
- 雇員每年的薪水
- 雇員的評估金額
在 C:\ > JUNIT_WORKSPACE 路徑下創(chuàng)建一個名為 TestEmployeeDetails.java 的準備被測試的測試案例類
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class TestEmployeeDetails {
EmpBusinessLogic empBusinessLogic =new EmpBusinessLogic();
EmployeeDetails employee = new EmployeeDetails();
//test to check appraisal
@Test
public void testCalculateAppriasal() {
employee.setName("Rajeev");
employee.setAge(25);
employee.setMonthlySalary(8000);
double appraisal= empBusinessLogic.calculateAppraisal(employee);
assertEquals(500, appraisal, 0.0);
}
// test to check yearly salary
@Test
public void testCalculateYearlySalary() {
employee.setName("Rajeev");
employee.setAge(25);
employee.setMonthlySalary(8000);
double salary= empBusinessLogic.calculateYearlySalary(employee);
assertEquals(96000, salary, 0.0);
}
}
TestEmployeeDetails 是用來測試 EmpBusinessLogic 類的方法的,它
- 測試雇員的每年的薪水
- 測試雇員的評估金額
現(xiàn)在讓我們在 C:\ > JUNIT_WORKSPACE 路徑下創(chuàng)建一個名為 TestRunner.java 的類來執(zhí)行測試案例類
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(TestEmployeeDetails.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
} 用javac編譯 Test case 和 Test Runner 類
C:\JUNIT_WORKSPACE>javac EmployeeDetails.java
EmpBusinessLogic.java TestEmployeeDetails.java TestRunner.java現(xiàn)在運行將會運行 Test Case 類中定義和提供的測試案例的 Test Runner
C:\JUNIT_WORKSPACE>java TestRunner檢查運行結(jié)果
true 網(wǎng)頁題目:創(chuàng)新互聯(lián)JUint教程:JUnit-編寫測試
本文鏈接:http://m.fisionsoft.com.cn/article/dhihgdh.html


咨詢
建站咨詢
