Mockito not returning expected value

Mobile Technologies Mobile Computing 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating
_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 + "]"; } }

Posted on 16 Aug 2022, this text provides information on Mobile Computing related to Mobile Technologies. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 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 employee = factory.getEmployee(); } In the mock: public class Check_Test extends TestCase { public void testMyCheck() { EmployeeFactory employeeFactoryMock = Mockito.mock(EmployeeFactory.class); Check checkObj = new Check(employeeFactoryMock); Mockito.doReturn(Collections.EMPTY_LIST).when(employeeFactoryMock).getEmployee(); String str = checkObj.myCheck(); assertEquals("", str); } }

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Important Mobile Technologies Links