0

I met a problem with testing a method(displayEmployees), which returns a List of objects.

public class Task {
    static List<Employee> employeeList;

    public static void main(String[] args) {
        Employee employee1 = new Employee("Dobrobaba", "Irina", "Ivanovna",
                "Moskva", 1900, 6);
        Employee employee2 = new Employee("Shmal", "Anna", "Nikolaevna",
                "Krasnodar", 2017, 8);
        Employee employee3 = new Employee("Kerimova", "Niseimhalum", "Magomedmirzaevna",
                "New-York", 2017, 3);
        Employee employee4 = new Employee("Dobryden", "Yuri", "Viktorovich",
                "Auckland", 2014, 11);
        Employee employee5 = new Employee("Lopata", "Leonid", "Nikolaevich",
                "Beijing", 2014, 11);
        employeeList = new ArrayList<>(Arrays.asList(employee1, employee2, employee3, employee4, employee5));
    }

    /**
     * Prints employees' information, which have worked more than 'n' year(s) for now.
     *
     * @param n years quantity
     */
    public static List<Object> displayEmployees(int n) {
        List<Employee> requiredList = new ArrayList<>();
            employeeList.forEach(employee -> {
                if (Year.now().getValue() - employee.getEmploymentYear() >= n) {
                    requiredList.add(employee);
                }
            });
        return Collections.singletonList(requiredList);
    }
}

This is how I'm trying to make a test, comparing 2 Lists one with another:

public class TaskTest {
    public static void main(String[] args) {
        displayEmployeesTest();
    }

    public static void assertEquals(String testName, List<Object> expected, List<Object> actual) {
        if (expected.equals(actual)) {
            System.out.println(testName + " passed");
        } else {
            System.out.println(testName + " failed: expected " + expected + ", actual " + actual);
        }
    }

    private static void displayEmployeesTest() {
        List<Object> expectedList;
        Employee employee1 = new Employee("Dobrobaba", "Irina", "Ivanovna",
                "Moskva", 1900, 6);
        Employee employee2 = new Employee("Shmal", "Anna", "Nikolaevna",
                "Krasnodar", 2017, 8);
        Employee employee3 = new Employee("Kerimova", "Niseimhalum", "Magomedmirzaevna",
                "New-York", 2017, 3);
        Employee employee4 = new Employee("Dobryden", "Yuri", "Viktorovich",
                "Auckland", 2014, 11);
        Employee employee5 = new Employee("Lopata", "Leonid", "Nikolaevich",
                "Beijing", 2014, 11);
        expectedList = new ArrayList<>(Arrays.asList(employee1, employee4, employee5));

        assertEquals("displayEmployeesTest", expectedList, displayEmployees(3));
    }
}

Problem is - I get an error "java.lang.NullPointerException", running TaskTest.

What is the reason of that error occurrence? Thank you for your attention!

artshakhov
  • 107
  • 11

3 Answers3

1

To compare two lists you can use the list's equals method. However in order to use this method, the list's content class must override equals.

employeeList1.equals(employeeList2)

And your Employee class must override equals like this:

public class Employee {
    private String name;
    private int id;
    ...

    //getters and setters

    ...

    public boolean equals(Object obj) {
        if(obj == null) return false;
        if(obj instanceof Employee) {
            Employee e = (Employee) obj;
            return this.name.equals(e.name) && this.id == e.id;
        } else return false;
    }
}

It is recomended to implement equals using an IDE like eclipse or Intellij

You're getting a nullpointer because static List<Employee> employeeList is being initialized in your main method which is never called by your test.

In order to create nice and neat tests you should use Junit. Check this: http://www.vogella.com/tutorials/JUnit/article.html

Hope this helps

Martín Zaragoza
  • 1,717
  • 8
  • 19
1

You call Task.displayEmployees without ever calling Task.main so the employeeList field within Task is never initialized

Michael
  • 41,989
  • 11
  • 82
  • 128
0

if you are comparing object you created you can let the class implement the comparable interface. that has only one method which is comparTo and override the method to compare employees based on the property given. using the predefined comparTo may not server as how you want to compare employees

Roland
  • 198
  • 1
  • 13