首页 > 解决方案 > 员工 - 净工资

问题描述

编写名为“EmployeeTest”的 JUnit 测试类来测试给定 Employee 类中的方法 calNetPay

雇员.java

公共类员工{

private int eid;

private String name;

private double salary;

private double pfper;

public Employee(int eid, String name, double salary, double pfper) {

    super();

    this.eid = eid;

    this.name = name;

    this.salary = salary;

    this.pfper = pfper;

}

public double calNetPay(){

    if(pfper <= 5){

        return salary;

    } else {

        return salary-salary*(pfper/100);

    }

}

}

请告诉我做这个问题我尝试了一些方法,但不符合要求。

标签: javaunit-testingjunit

解决方案


员工测试

import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class EmployeeTest {
    Employee emp = null;
    
    @Test
    public void testCalNetPay() {
        emp = new Employee(101,"xyz",5000.0,3);
        double netPay = emp.calNetPay();
        assertEquals(5000.0,netPay,0.0);
        
        emp = new Employee(102,"abc",1000.0,6);
        netPay = emp.calNetPay();
        assertEquals(940.0,netPay,0.0);
    }
}

员工主要

import java.util.*;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;


public class EmployeeMain {
    public static void main (String[] args) {
        Result result = JUnitCore.runClasses(EmployeeTest.class);
        for (Failure failure : result.getFailures()) {
            System.out.println(failure.toString());
        }
        System.out.println(result.wasSuccessful());
    }
}

这应该工作,希望它有所帮助。


推荐阅读