我有下面提到的 2 个类(class)。 第一个:EmployeeDetails
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 没有任何值。 代码有什么问题吗?找不到问题所在...
最佳答案
你的 setter 和 getter 是错误的......
修改名称 setter ,例如来自:
name= this.name;
致:
this.name = name;
说明:
您正在对传递给方法的变量进行赋值,而不是将其赋值给对象变量。同样适用于 monthlySalary
以及其他字段(您在方法名称中也遇到了拼写错误:setMonthSalry()
)。
关于java - 即使通过java中的测试传递值,属性也为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32418503/