2015-09-05 53 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

相關問題