jeudi 28 janvier 2016

Mockito: How can i mock private final fild? [duplicate]

This question already has an answer here:

i'm using Mockito-Framework for mocking and i try to moch the privet field. But it doesn't work.

How can i mock private final fild with Mockito?

i have the folowing source code.

public class HibernatePoiDAO {


private final HibernateDriverDAO driverDAO;
private final HibernateDriverLocationDAO driverLocationDAO;

@Autowired
public HibernatePoiDAO(final SessionFactory sessionFactory) {
    this.driverDAO = new HibernateDriverDAO(sessionFactory);
    this.driverLocationDAO = new HibernateDriverLocationDAO(sessionFactory);
}

public List<Poi> findWithin(final Float p1Lat,
                            final Float p1Lon,
                            final double radiusInMeter,
                            final Double rating,
                            final OnlineStatus onlineStatus,
                            final Integer manufacturingYear,
                            final Integer seats) {
    //I want mock this method call
    List<DriverLocation> driverLocationsWithin = driverLocationDAO.findWithin(new GeoCoordinate(p1Lat, p1Lon), radiusInMeter);


    List<Filter<Driver>> filters = this.createFilters(rating, onlineStatus, manufacturingYear, seats);
    final Map<Driver, DriverLocation> filteredDriverLocationsWithin = this.filters(filters, driverLocationsWithin);
    List<Driver> drivers = new ArrayList<Driver>(filteredDriverLocationsWithin.keySet());
    Map<Long, DriverLocation> driverLocationMap = this.toDriverLocationMap(new ArrayList<DriverLocation>(filteredDriverLocationsWithin.values()));

    return this.makePoiList(drivers, driverLocationMap);
}


}

I try so:

   final HibernateDriverLocationDAO driverLocationDAO=Mockito.mock(HibernateDriverLocationDAO.class);
            Mockito.when(driverLocationDAO.findWithin(
            Matchers.any(GeoCoordinate.class),
            Matchers.any(Double.class)
            )).thenReturn(driverLocations);


    final HibernateTaxiPoiDAO controller=new HibernateTaxiPoiDAO(this.sessionFactory);

    // WHEN
    List<Poi> pois=controller.findWithin(53.5f,9.53f,20000,5.0D,null,null,null);

but it does not work. what do i wrong?

Aucun commentaire:

Enregistrer un commentaire