jeudi 27 septembre 2018

Mockito: Mock a method called in an other private class method

I have a service Class that I'm trying to test using Mockito, JUnit and JaCoCo. This class has some public method that uses other private method in the same class for some business logic : both of them (public and private) use DAO methods, that I usually mock while testing service layer. The problem is that while the DAO called from the public method return what I set in the test class, the DAO method from the private method called inside the public one return an empty value (an empty List in this particulare case). This is the service class:

@Service
public class EventService {

@Transactional(readOnly=true)
public List<ObjectDTO> publicMethod() {
                    List<Object> list = DAO1.call1();    // Not empty
                    return privateMethod(parameter);
}

private List<EventSummaryDTO> privateMethod() {
      List<Object> list = DAO2.call2();       // Empty
}

}

and this one is the test class:

public class ServiceTest extend AbstractTransactionalJUnit4SpringContextTests {

  @Mock
  private DAO1 mockDAO1;

  @Mock
  private DAO mockDAO2;

  @InjectMocks
  private Service service;

  @Test
  public void testcase() {

  Mockito.when(mockDAO1.call1()).thenReturn(list);
  Mockito.when(mockDAO2.call2()).thenReturn(list);

  List<ObjectDTO> list = service.publicMethod();
  list.size() // Empty

  }
}

How can I let the private method's call to return something different from nothing!?

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire