hasta ti
hasta ti

Reputation: 21

populating lists using mockito

I have a class that I want to test.

public class MinicabManager {
public static Set<Driver> allDrivers ;
public static Set<Passenger> allPassengers;
public static List<Trip> trips;

public static Set<Driver> findFakeDrivers() {
    Set<Driver> fakeDrivers = new HashSet();
    for (Trip t : trips) {
        for (Driver d : allDrivers) {
            if (t.getDriver().getDriver() != d.getDriver()) {
                fakeDrivers.add(d);
            }
        }
    }
    return fakeDrivers;
}

I get NullPointerException when I run this test :

@RunWith(MockitoJUnitRunner.class)
public class MinicabManagerTest {
@Mock
Set<Driver> allDrivers;
@Mock
Set<Passenger> allPassengers;
@Mock
List<Trip> trips;
@InjectMocks
MinicabManager minicabManager;
@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
minicabManager = new MinicabManager();

    allDrivers = new HashSet();
    allPassengers = new HashSet();
    trips = new ArrayList();

    Driver d1 = new Driver();
    d1.setDriver("d1");
    Driver d2 = new Driver();
    d2.setDriver("d2");
    allDrivers.add(d1);
    allDrivers.add(d2);

    Passenger p1 = new Passenger();
    p1.setName("p1");
    Passenger p2 = new Passenger();
    p2.setName("p2");
    allPassengers.add(p1);
    allPassengers.add(p2);

    Trip t1 = new Trip();
    t1.setDriver(d1);
    t1.setPassengers(allPassengers);
    trips.add(t1);
}
@Test
public void testFindFakeDrivers() throws Exception {
    Set<Driver> result = minicabManager.findFakeDrivers();
}

How do I populate the list? For my understanding InjectMocks gets injected by mocked lists and set, then populated before each test cases. But I get NullPointerException

Upvotes: 1

Views: 153

Answers (1)

Yonas
Yonas

Reputation: 449

  • Don't mock objects and instantiate them normally. Either do one or the other. The @Mock annotation will create a mock object for you that you can readily use in your test.
  • Know that a mock obejct is a 'fake' object that only pretends to be of a certain type. So if you call any methods/fields on a mock object, it will really not do anything (and will always return null in case of non-void methods).
  • You don't need to specify MockitoJUnitRunner and call the initMocks() method. One of them is enough.
  • As others have mentioned, it looks like your test doesn't need mocks. So it could look something like this:
public class MinicabManagerTest {

        @Before
        void setUp() {

            Set<Driver> allDrivers = new HashSet<>();
            Set<Passenger> allPassengers = new HashSet<>();
            List<Trip> trips = new ArrayList<>();

            Driver d1 = new Driver();
            d1.setDriver("d1");
            Driver d2 = new Driver();
            d2.setDriver("d2");
            allDrivers.add(d1);
            allDrivers.add(d2);

            Passenger p1 = new Passenger();
            p1.setName("p1");
            Passenger p2 = new Passenger();
            p2.setName("p2");
            allPassengers.add(p1);
            allPassengers.add(p2);

            Trip t1 = new Trip();
            t1.setDriver(d1);
            t1.setPassengers(allPassengers);
            trips.add(t1);

            MinicabManager.allDrivers = allDrivers;
            MinicabManager.allPassengers = allPassengers;
            MinicabManager.trips = trips;
        }

        @Test
        public void testFindFakeDrivers() throws Exception {
            Set<Driver> result = MinicabManager.findFakeDrivers();
            // assertions about the result here
        }

Also, take care not to compare Strings with = and !=. Use the equals() method.

Upvotes: 1

Related Questions