即使从java中的测试传递值,属性为null?

时间:2015-09-05 23:09:21

标签: java unit-testing testng

我有两个提到的课程。 第一个: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");
       }
}

即使我从Test方法传递了值,也没有传递给属性。 "的System.out.println(emp.getName());" print null没有任何值。 代码中的任何问题?无法找到问题所在......

1 个答案:

答案 0 :(得分:2)

你的安装员和吸气者都错了......

修改名称设置器,例如:

   <script>

$( document ).ready(function() {

months = ['January','Februari','Mars','April','May','June','July','August','September','October','November','December'];
days = [31,28,31,30,31,30,31,31,30,31,30,31]

for(i = 1; i <= 12; i++){
    $('.months').append('<div class="month" id='+i+'><p>'+months[i-1]+'</p></div>');
}
for(j = 1; j<=31; j++){
    $('.days').append('<div class="numberDays"><p>'+j+'</p></div>');
}
$('.month').click(function(){
    var id = this.id;
    alert(id);
    //$().css({'background':'red'});
});
});
</script>

要:

name= this.name;

<强>解释

您正在对传递给方法的变量进行赋值,而不是将其赋值给对象变量。同样适用于this.name = name; 和其他字段(您在方法名称中也存在拼写错误:monthlySalary)。