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

Migration from Junit 4 to Junit 5 #1381

Closed
sbernard31 opened this issue Jan 23, 2023 · 5 comments
Closed

Migration from Junit 4 to Junit 5 #1381

sbernard31 opened this issue Jan 23, 2023 · 5 comments
Labels
housekeeping Refactoring, cleaning code or API

Comments

@sbernard31
Copy link
Contributor

I was working on rewriting some tests to be able to test more easily the new transport abstraction layer (#1025).

I was reading some stuff on Junit 4 and I asked my self what is news in Junit 5 ?

After reading the documentation, It seems that a lot changes ...
So before to involve time in test refactoring based on Junit4, we should maybe migrate to Junit5 first ? 🤔

Here some article which gives some reason to migrate to Junit 5 :

@sbernard31 sbernard31 added the discussion Discussion about anything label Jan 23, 2023
@sbernard31
Copy link
Contributor Author

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

@sbernard31
Copy link
Contributor Author

Different between Junit4 and 5 are explained here.

I started a branch to experiment how this impacts Leshan tests code base.
See : #1391

Most painful tasks for us :

1. change in assert methods

Import should be changed, that's OK.
Less funny argument order changes ...

Assertions and assumptions in JUnit Jupiter accept the failure message as their last argument instead of the first one.

Hopefully our tests code are not so clean and don't use so much failure message 😅

2. Testing exceptions

@Test(expected = IllegalArgumentException.class) syntax does not exist anymore.
It should be replaced by :

 assertThrowsExactly(IllegalArgumentException.class, () -> {
    // code to tests
});

We use it a lot but this no brain and repetitive task.

3. Parameterized Tests

@RunWith(Parameterized.class) which allow to parameterize test at class level, does not exist anymore.

If should be replace by @ParameterizedTest (which is at method/test level)
If parameterize tests at class level is needed, the tricks is to use something like that.

We use it often and this is clearly the less "no brain" task as you need to rewrite a lot about how you pass arguments.

@sbernard31 sbernard31 added housekeeping Refactoring, cleaning code or API and removed discussion Discussion about anything labels Jan 27, 2023
@sbernard31
Copy link
Contributor Author

leshan-core that I used for experiment was the big one.
All other except leshan-integration-tests was easy to migrate. (done in PR #1391)

I will try leshan-integration-teststhis afternoon.

@sbernard31
Copy link
Contributor Author

leshan-integration-tests is migrated too.
We was using LockstepEndpoint from californium tests maven module, so I had to duplicate it to be able to update it with Junit5 API.
(So I also removed all dependency to californium tests modules)

One remaining question about Hamcrest ?

Junit4 depends on Hamcrest.
Junit5 doesn't. (but nothing prevent to use it)

So, I added manually the Hamcrest dependency because we are using it a little in leshan-integrations-tests for :

  • 1 call like this : assertThat(acl.getInstances().keySet(), hasItems(0, 1));
  • lots of calls like : assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
  • and also some call in LockstepEndpoint class.

I asking myself if we should keep Hamcrest and maybe trying to use it more?
or this is not so useful and better to remove the dependency ?

@sbernard31
Copy link
Contributor Author

PR #1391 is integrated in master, so we are using now Junit5 in master.

About Hamcrest question, let's talk about at #1390 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
housekeeping Refactoring, cleaning code or API
Projects
None yet
Development

No branches or pull requests

1 participant