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

Migrate test suite to JUnit5 #3919

Closed
slarse opened this issue May 11, 2021 · 18 comments
Closed

Migrate test suite to JUnit5 #3919

slarse opened this issue May 11, 2021 · 18 comments
Labels
good first issue test about adding or refactoring tests

Comments

@slarse
Copy link
Collaborator

slarse commented May 11, 2021

We currently have a little bit of JUnit5 (such as VariableTest), but mostly JUnit4 in the test suite. I think it's worth our while to start migrating entirely to JUnit5. Off the top of my head, here are some benefits that come with JUnit5:

This is also very easy to do incrementally, one test class at a time, making it a very good first issue.

Why not keep both?

While we can keep both JUnit4 and JUnit5, and one can even write test cases of both flavors in the same test class, it's just messy to do so due to all of the assertions and annotations having the same simple names. You can also be pretty confident that if a contributor comes into a test class and sees only JUnit4 tests, that contributor will write JUnit4 tests as well. The longer we wait, the more work it will be to leave JUnit4 behind!

How do I know if a test class is using JUnit4?

The easiest way to tell is to look for imports from the JUnit4 API, the most telling one being import org.junit.Test. At the moment of writing, there are 189 such imports in the test suite.

$ grep -r 'import org.junit.Test' src/test/java | wc -l
189

How do I know if a test class is using JUnit5?

The easiest way to tell is to look for imports from the JUnit5 API, the most telling one being import org.junit.jupiter.api.Test. At the moment of writing, there are 3 such imports in the test suite.

$ grep -r 'import org.junit.jupiter.api.Test' src/test/java | wc -l
3

How can I convert from JUnit4 to JUnit5?

See #3921 for an example.

Some IDEs offer auto-conversion, such as IntelliJ IDEA. But in general, remove any org.junit import that is not on the form org.junit.jupiter (e.g. import org.junit.Test should be removed), and then fix any compile or test errors by importing the appropriate classes (e.g. import org.junit.jupiter.api.Test) and methods from from the org.junit.jupiter package. It's really rather straightforward.

To check that you've removed all JUnit4 stuff from a test class, you can run the following in a bash-like terminal:

$ grep org.junit <path to the test class> | grep -v jupiter

If it prints no output, you're done!

Note about class/method visibility: While JUnit4 mandated public test classes and methods, JUnit5 allows any visibility but private. However, it's best if you leave the visibility as-is to minimize the diff.

To round off, here are a couple of nice articles about moving from JUnit4 to 5:

Scope of a single PR?

Convert one test class per PR. No more, and no less!

Objections?

If anyone is opposed to this, please raise your voice :). I don't see any harm in migrating test classes, even if we never finish up entirely. 189 also sounds like a big number, but as it's semi-automatic, it typically doesn't take more than a few minutes to migrate a test class (of course, there are exceptions, especially if there's heavy use of @Rule).

@slarse slarse added good first issue test about adding or refactoring tests labels May 11, 2021
@Rohitesh-Kumar-Jain
Copy link
Contributor

Hi @slarse,

Thanks for opening this issue, this seems a really good task that I can start in this community bonding phase.

@slarse
Copy link
Collaborator Author

slarse commented May 19, 2021

Hi @Rohitesh-Kumar-Jain,

Absolutely! I hope the instructions should be sufficient, but let me know if you need any help.

Something that I think I forgot to write in the OP is that you should do one test class per PR, to keep the PRs focused and easy to review :)

@monperrus
Copy link
Collaborator

Some IDEs offer auto-conversion, such as IntelliJ IDEA.

Super useful transformation, thanks for the link

@Rohitesh-Kumar-Jain
Copy link
Contributor

Hi @slarse,

I have one question what is spoon-pom is for, and what is pom is for.

I am actually trying to replace @Rule with @ExtendWith in this Class, for that, I believe I need to add this dependency

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-junit-jupiter</artifactId>
    <version>3.10.0</version>
    <scope>test</scope>
</dependency>

Apart from this, there are other pom files as well, what are they for, and in which pom file(s) should I be adding my dependencies while working on the Spoon core tests?

@slarse
Copy link
Collaborator Author

slarse commented May 28, 2021

Hi @Rohitesh-Kumar-Jain,

spoon-pom is the parent pom-file for the Spoon modules. If you have a look in e.g. the spoon-core pom file, you'll see that it declares the spoon-pom/pom.xml file as its parent.

We typically put test dependencies into spoon-pom/pom.xml so they are available to all inheriting pom-files.

@MartinWitt
Copy link
Collaborator

@slarse
Copy link
Collaborator Author

slarse commented Feb 11, 2022

Nicely done @MartinWitt . Closing.

@slarse slarse closed this as completed Feb 11, 2022
@MartinWitt
Copy link
Collaborator

MartinWitt commented Feb 11, 2022

Oh the closing was fast.

Shall we migrate all spoon subprojects like spoon-decompiler too?

In my opinion, yes for consistency (and JUnit5 is simply better), but I'm not sure about the ownership model for those subprojects. @monperrus, wdyt?

This question is still open. Otherwise, we can't remove the JUnit 4 dependency easy.

@slarse
Copy link
Collaborator Author

slarse commented Feb 11, 2022

Point.

The question is however not open, the answer is yes migrate them. IMO we can do one subproject per PR.

@slarse slarse reopened this Feb 11, 2022
@slarse
Copy link
Collaborator Author

slarse commented Feb 11, 2022

So, this issue then closes with the removal of the JUnit4 dependency in Spoon!

@monperrus
Copy link
Collaborator

I'm not sure about the ownership model for those subprojects. @monperrus, wdyt?

I'd say there is no ownership, it is purely best-effort open-source. If you care, you PR.

And yes, agree, it'd be great to port the submodules, one project per PR. Good news, they have light test suites.

@slarse
Copy link
Collaborator Author

slarse commented Feb 12, 2022

Good news, they have light test suites.

The one situation when that's good news :)

@monperrus
Copy link
Collaborator

closing thanks to the gritty work of @MartinWitt!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue test about adding or refactoring tests
Projects
None yet
Development

No branches or pull requests

5 participants