java - 员工 - 净工资
问题描述
编写名为“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);
}
}
}
请告诉我做这个问题我尝试了一些方法,但不符合要求。
解决方案
员工测试
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());
}
}
这应该工作,希望它有所帮助。
推荐阅读
- symfony - JMS Serializer:如何在不到处添加注释的情况下传播属性的组排除?
- php - 应用过滤器时,帖子类型订单不起作用
- javascript - 如何从实时数据库中的所有 randomId 中获取特定键值,使用 javaScript 的 Firebase 函数
- sql - Oracle 查询以获取初始更改值
- javascript - Angular6 HttpClient:在获取请求时注入标头在 Chrome 和 IE11 上不起作用
- javascript - 正则表达式在 javascript 和 .NET 中给出不同的结果
- python - 提取括号python之间的年份
- c++ - Can I escape (suppress) C preprocessor macro expansion
- python - ParDo 行为的 Apache Beam 解释
- javascript - Checkbox won't retrieve value in that row