-
Notifications
You must be signed in to change notification settings - Fork 408
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Considering using Mockito / AssertJ / Hamcrest in Leshan #1390
Comments
@jvermillard, @JaroslawLegierski, @Warmek if you have any opinions about this, do not hesitate to share ? 🙏 (maybe better alternative than mockito?) |
I stopped using Mockito on my projects, I try to focus on unit tests for testing "purely functional" pieces like encoder/decoder. For this, mocks are not helpful. For testing more "behavioral" code, where mockito could be handy for mocking some pieces, I prefer to use integration tests, using different toolsets, like Wiremock, Kafka client mocks, etc. |
I'm not sure there is an equivalent of Wiremock, Kafka client mocks which could help us for Leshan integrations tests. 🤔
I don't want to use it everywhere but in our integration tests we launch a leshan client and a leshan server and then we try to check if all behaves as expected. Often we are using Listener and/or Callback from Leshan API to ensure that all is called as expected. To do that currently we implement manually some listeners (see I'm doing some some tests and in // verify result
listener.waitForNotification(2000); // where listener is a TestObservationListener
assertTrue(listener.receivedNotify().get());
assertEquals(LwM2mSingleResource.newStringResource(15, "Europe/Paris"),
(listener.getObserveResponse()).getContent());
assertNotNull(listener.getObserveResponse().getCoapResponse());
assertThat(listener.getObserveResponse().getCoapResponse(), is(instanceOf(Response.class))); by verify(observeListener, timeout(2000).times(1)). //
onResponse(//
notNull(SingleObservation.class), //
notNull(), //
argThat(response -> {
return LwM2mSingleResource.newStringResource(15, "Europe/Paris")
.equals(response.getContent()) //
&& response.getCoapResponse() != null
&& response.getCoapResponse() instanceof Response;
}));
verifyNoMoreInteractions(observeListener); OR verify(observeListener, timeout(2000).times(1)).//
onResponse(notNull(SingleObservation.class), notNull(), responseCaptor.capture());
verifyNoMoreInteractions(observeListener);
assertEquals(LwM2mSingleResource.newStringResource(15, "Europe/Paris"), responseCaptor.getValue().getContent());
assertNotNull(responseCaptor.getValue().getCoapResponse());
assertThat(responseCaptor.getValue().getCoapResponse(), is(instanceOf(Response.class))); OR verify(observeListener, timeout(2000).times(1)). //
onResponse(//
notNull(SingleObservation.class), //
notNull(), //
argThat(response -> {
assertThat(response.getContent(),
is(LwM2mSingleResource.newStringResource(15, "Europe/Paris")));
assertThat(response.getCoapResponse(), is(notNullValue()));
assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
return true;
}));
verifyNoMoreInteractions(observeListener); |
I created a mockito branch with my tests. (see For now, I feel Mockito could be useful. Hamcrest is more a matching oriented library and AssertJ more an assertion oriented library. Here some issue, I face when I wanted to use AssertJ with Mockito (About our current Hamcrest dependencies, see : #1381 (comment)) |
More code using Mockito, Hamcrest, AssertJ at #1399 in |
I largely used Mockito, AssertJ in #1425 (and Hamcrest sporadically), so I think we can close this issue. |
Initially Mockito was used in Leshan but :
So, we decide to remove this dependency.
But :
Anyway if we do that move this will be after #1381.
(This discussion lead to question about Hamcrest and AssertJ too)
The text was updated successfully, but these errors were encountered: