I am using the JUnit and Mockito library to test my application. The problem is, when I am executed below code, the value is not returning empty list at run time and test is getting failed. Ideally it should be return empty list when getEmployee()
get executed
public class Check_Test extends TestCase
{
public void testMyCheck()
{
Check checkObj = new Check();
EmployeeFactory employeeFactoryMock = Mockito.mock(EmployeeFactory.class);
Mockito.doReturn(Collections.EMPTY_LIST).when(employeeFactoryMock).getEmployee();
String str = checkObj.myCheck();
assertEquals("", str);
}
}
I tried all possibilities best of my knowledge, but I am not able to pass this test case.
The below Check
class which having myCheck()
method that I need to test for empty...
public class Check
{
public String myCheck()
{
List<Employee> employee = EmployeeFactory.getInstance().getEmployee();
if (employee.isEmpty())
{
return ""; //Line No. 8 returning empty but, control is not coming here
}
else
{
return "NotEmpty"; // The control is always coming here ????
}
}
}
I am eagerly looking forward to support. Can any one please help me out, how to pass this test cases ???. How to bring the control at line No 8 through Mockito to pass the test case???
Please assume, Below two classes don't have real code, we have only binary file as JAR file, we can not modify the below code.... I am attaching this for our understanding...
public class EmployeeFactory
{
private EmployeeFactory()
{
}
public static EmployeeFactory getInstance()
{
return EmployeeFactoryHelper.INSTANCE;
}
private static class EmployeeFactoryHelper
{
public static final EmployeeFactory INSTANCE = new EmployeeFactory();
}
private static List<Employee> employees = null;
static
{
employees = Arrays.asList(
new Employee("Manish", "Kumar", true, 60),
new Employee("Siva", "Attla", true, 42),
new Employee("Anand", "Manivel", false, 51),
new Employee("Madhavi", "Govind", true, 45),
new Employee("Janani", "Chidambaram", true, 45),
new Employee("Mannu", "Krishna", false, 39),
new Employee("Karthika", "Hosamane", false, 39)
);
}
public List<Employee> getEmployee()
{
return employees;
}
}
public class Employee
{
private String firstName;
private String lastName;
private boolean workStatus;
private int age;
public Employee(String firstName, String lastName, boolean workStatus, int age)
{
super();
this.firstName = firstName;
this.lastName = lastName;
this.workStatus = workStatus;
this.age = age;
}
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public boolean isWorkStatus()
{
return workStatus;
}
public void setWorkStatus(boolean workStatus)
{
this.workStatus = workStatus;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
@Override
public String toString()
{
return "Employee [firstName=" + firstName + ", lastName=" + lastName + ", workStatus=" + workStatus + ", age=" + age + "]";
}
}