我有2班提到下面。 第一招:EmployeeDetails属性为null甚至在java中测试通过值?
package com.pacakge.emp;
public class EmployeeDetails {
private String name;
private double monthlySalary;
private int age;
//return name
public String getName()
{
return name;
}
//set the name
public void setName(String name)
{
name= this.name;
}
//get month sal
public double getMonthSal()
{
return monthlySalary;
}
//set month salary
public void setMonthSalry(double monthlySalary)
{
monthlySalary =this.monthlySalary;
}
第二个:EmpBusinessLogic
package com.pacakge.emp;
public class EmpBusinessLogic {
//calculate yearly salary of the employee
public double calculateYearlySalary(EmployeeDetails empdetails)
{
double yearlySalary;
yearlySalary =empdetails.getMonthSal()*12;
return yearlySalary;
}
这是我的测试类
package com.pacakge.emp;
import org.testng.Assert;
import org.testng.annotations.Test;
public class TestEmployeeDetails {
EmployeeDetails emp = new EmployeeDetails();
EmpBusinessLogic EmpBusinessLogic = new EmpBusinessLogic();
// Test to check yearly salary
@Test
public void testCalculateYearlySalary() {
emp.setName("saman");
emp.setAge(25);
emp.setMonthSalry(8000.0);
emp.getName();
System.out.println(emp.getName());
double salary = EmpBusinessLogic.calculateYearlySalary(emp);
Assert.assertEquals(salary, "8000");
}
}
即使我已经从试验方法值传递的值不会传递到属性。 “System.out.println(emp.getName());”打印null没有任何价值。 代码中的任何问题?找不到什么问题...
我建议让计算年薪为静态方法。 –
谢谢@ben。得到它:) – dilRox