1. Diferenta intre mock si spy
final DataLoadingService dataLoadingService = spy(DataLoadingService.class);
dataLoadingService.setData(data); // executata normal
when(dataLoadingService.isExecutedWithUI()).thenReturn(false); // va returna intotdeauna false
2. Captare de argumente
final ArgumentCaptor<List> argumentCaptor = ArgumentCaptor.forClass(TypeOfTheParam.class);
doAnswer(param -> {
final TypeOfTheParam argument = argumentCaptor.getValue();
// do some action
return null;
}).when(dataLoadingService).execute(argumentCaptor.capture(), any(AnotherParamType.class));
SAU (mai simplu, fara ArgumentCaptor):
doAnswer(param -> {
final ParamType arg1 = param.getArgument(0);
final AnotherParamType arg2 = param.getArgument(1);
// do some action
return null;
}).when(dataLoadingService).execute(any(ParamType.class), any(AnotherParamType.class));
3. Inject
private TypeA objectA = mock(TypeA.class);
private TypeB objectB = mock(TypeB.class);
@InjectMocks
private ContainingObject mainObject; // objectA & B will be injected there
// provided that (unique) objects of type A and B are fields inside mainObject
4. Verify
Este folosit pentru a verifica daca s-a intrat intr-o metoda a clasei mock-uite, si de cate ori.
ex:
final Runnable callback = mock(Runnable.class);
viewModel.clearAllData(callback);
verify(callback, times(1)).run(); // run este o metoda din callback (specifica unui Runnable)
// verify that method run from the callback is called exactly once
- Mock "falsifica" toate metodele unei clase
- Spy "falsifica" numai metodele declarate cu when, restul se apeleaza normal
final DataLoadingService dataLoadingService = spy(DataLoadingService.class);
dataLoadingService.setData(data); // executata normal
when(dataLoadingService.isExecutedWithUI()).thenReturn(false); // va returna intotdeauna false
2. Captare de argumente
final ArgumentCaptor<List> argumentCaptor = ArgumentCaptor.forClass(TypeOfTheParam.class);
doAnswer(param -> {
final TypeOfTheParam argument = argumentCaptor.getValue();
// do some action
return null;
}).when(dataLoadingService).execute(argumentCaptor.capture(), any(AnotherParamType.class));
SAU (mai simplu, fara ArgumentCaptor):
doAnswer(param -> {
final ParamType arg1 = param.getArgument(0);
final AnotherParamType arg2 = param.getArgument(1);
// do some action
return null;
}).when(dataLoadingService).execute(any(ParamType.class), any(AnotherParamType.class));
3. Inject
private TypeA objectA = mock(TypeA.class);
private TypeB objectB = mock(TypeB.class);
@InjectMocks
private ContainingObject mainObject; // objectA & B will be injected there
// provided that (unique) objects of type A and B are fields inside mainObject
4. Verify
Este folosit pentru a verifica daca s-a intrat intr-o metoda a clasei mock-uite, si de cate ori.
ex:
final Runnable callback = mock(Runnable.class);
viewModel.clearAllData(callback);
verify(callback, times(1)).run(); // run este o metoda din callback (specifica unui Runnable)
// verify that method run from the callback is called exactly once
Niciun comentariu:
Trimiteți un comentariu