samedi 16 mars 2019

Mockito - InjectMocks or how to get new value after method invocation

I have the following class:

public class DaoService {
   private Dao dao;
   private Map<String, Entity> map;

   public DaoService(Dao dao, Map<String, Entity> map){
       this.dao = dao;
       this.map = map;
   }

   public create(Entity entity){
      Dao.create(entity);
      map.put(Entity.getName(), entity);
   }
}

I want to test that the method invocation realy put a new element in the map and when I invoke It more with another paramter It will have size equals 2. But I need to ignore Dao.create().

I have the following test class:

@RunWith(MockitoJUnitRunner.class)
public class DaoServiceTest {
   @Mock
   Dao dao;

   @Mock
   Map<String, Entity> map = new HashMap<>();

   @InjectMocks
   DaoService service;

   @Test
   public void testCreate(){
      Entity entity = new Entity("Alex");   // name
      service.create(entity);
      assertEquals(map.size(),1);   // failNotEquals
   }

How can I do when I invoke service.create(entity) that It will ignore dao.create(entity) but will not ignore map.put(entity.getName(), entity) ?

Aucun commentaire:

Enregistrer un commentaire