Mockito not returning expected value
Mobile Technologies
Mobile Computing
3 years ago
_x000D_
_x000D_
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 = 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 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 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 + "]";
}
}
User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.
manpreet
Best Answer
3 years ago
_x000D_ Instead of calling a static factory method, inject the factory (maybe in the constructor) and use it in the class. That way you are decoupling the class and you will be able to inject a mock instead of a real implementation during the testing phase. Little example: class EmployeeFactory{ public Employee getEmployee(){...} } class Check{ private EmployeeFactory factory; public Check(EmployeeFactory factory){ this.factory = factory; public String myCheck() { List