Skip to content
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

Closed
sbernard31 opened this issue Jan 26, 2023 · 6 comments
Closed

Considering using Mockito / AssertJ / Hamcrest in Leshan #1390

sbernard31 opened this issue Jan 26, 2023 · 6 comments
Labels
discussion Discussion about anything

Comments

@sbernard31
Copy link
Contributor

sbernard31 commented Jan 26, 2023

Initially Mockito was used in Leshan but :

  • We have feeling that code written with it was not so good.
  • We didn't use it so much.

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)

@sbernard31
Copy link
Contributor Author

@jvermillard, @JaroslawLegierski, @Warmek if you have any opinions about this, do not hesitate to share ? 🙏

(maybe better alternative than mockito?)

@jvermillard
Copy link
Contributor

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.

@sbernard31
Copy link
Contributor Author

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 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.

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 TestObservationListener). Mockito could avoid us to implement this.

I'm doing some some tests and in ObserveTest I can replace current code above :

// 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
A)

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
B)

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
C)

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);

@sbernard31
Copy link
Contributor Author

sbernard31 commented Feb 9, 2023

I created a mockito branch with my tests. (see ObserveAlternativeTest class)

For now, I feel Mockito could be useful.
I also play a bit with Hamcrest and AssertJ.

Hamcrest is more a matching oriented library and AssertJ more an assertion oriented library.
In practice, we want to use it for assert in tests.
From what I see, I prefer AssertJ because it works very well with autocomplete without learning AssertJ documentation.

Here some issue, I face when I wanted to use AssertJ with Mockito argThat : mockito/mockito#2285 (comment)

(About our current Hamcrest dependencies, see : #1381 (comment))

@sbernard31 sbernard31 changed the title Considering using mockito in Leshan Considering using Mockito / AssertJ / Hamcrest in Leshan Feb 10, 2023
@sbernard31
Copy link
Contributor Author

More code using Mockito, Hamcrest, AssertJ at #1399 in parameterize_integration_test branch

@sbernard31
Copy link
Contributor Author

I largely used Mockito, AssertJ in #1425 (and Hamcrest sporadically), so I think we can close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Discussion about anything
Projects
None yet
Development

No branches or pull requests

2 participants