dilRox
dilRox

Reputation: 71

properties are null even pass values from the test in java?

I have 2 class mentioned bellow . first one :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;  
    }

Second one :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;            
    }

This is my test class

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");
       }
}

Even if I have passed values from Test method values are not pass to the properties . " System.out.println(emp.getName());" print null without any value. any issue in the code ? couldn't find what is the issue ...

Upvotes: 0

Views: 44

Answers (1)

Nir Alfasi
Nir Alfasi

Reputation: 53535

Your setters and getters are wrong...

Modify the name setter, for example, from:

name= this.name;

To:

this.name = name;

Explanation:

You're doing the assignment to the variable that is passed to the method instead of assigning it to the object variable. Same applies for monthlySalary and maybe other fields (you got a spelling mistake in the method name there as well: setMonthSalry()).

Upvotes: 2

Related Questions