2015-09-05 48 views
0

我有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没有任何价值。 代码中的任何问题?找不到什么问题...

+0

我建议让计算年薪为静态方法。 –

+0

谢谢@ben。得到它:) – dilRox

回答

2

你的getter和setter方法是错误的...

修改名称制定者,例如,从:

name= this.name; 

要:

this.name = name; 

说明:

你正在做的赋值给传递给方法的变量,而不是分配给对象变量。同样适用于monthlySalary以及其他字段(您在方法名称中也有拼写错误:setMonthSalry())。

+0

非常感谢。这有助于我解决问题:) – dilRox

相关问题