Monday, October 11, 2021

Running Test Suite of JUnit 5 Tests in the Same Package in Eclipse IDE (Work Around)

 I looked for a tutorial online on how to run a suite of JUnit tests in the latest Eclipse IDE (as of October 2021).

I did not find a single resources that addressed that issue with Eclipse IDE. Apparently the problem is that Eclipse does not support JUnit 5 Jupiter tests when creating a suite of JUnit 5 Jupiter tests.

When a software engineer wants to create a suite of Junit 5 Tests this is the familiar & yet unhappy dialogue window that appears:

The above is what greets most users of the IDE. As highlighted in yellow there is support for Junit 3 & 4 but not 5.

I looked at a lot of options that do not use the wizard/dialogue above & I wrote the test-suite from scratch (specifying the unit test classes in the annotations but the test suite seems to run but in the results window the tests are not registering). Eclipse simply does not work with a JUnit 5 test suite. If you want to run a Junit5 test you have to run the files individually which can be a chore when you are doing TDD (or test driven development). Hence, this work around,  for even with annotated class file specification etc. Eclipse merely seems like it is working. In other words, the JUnit test results window does not show all the test results (or even correctly), as anyone who has tried would know.

So here is a quick example illustrating the use of this suite. 

Following is your business logic that you wish to test:

[src]

[/src]

You are trying to test the add & subtract method in line 12 & 13.

So your tests are so, starting with the test for the add method:

[src]

[/src]

And then the test for the subtract method:

[src]

[/src]

Now, coming to the test suite part. Since the test suite for JUnit 5 is not supported. Just create how you would usually create a test suite. Pick JUnit 3 or JUnit 4 in the  dialogue for our test suite file AllTests.java as in below:

 

When the empty test suite file is created instead of specifying the classes in annotations, right click on the file, go to Run> Run Configurations:

As you can see instead of specifying that the AllTests.java file run as a single test, select "Run All tests in the selected project, package or source folder" as in above. Then select the package where you are storing all your unit tests for that class. This works for package level only & not across packages unfortunately.

Now when you run AllTests.java via this configuration:


Note how the runner is JUnit5 (something to watch out for in some erroneously written blogs out there).

 So now that we have our tests the directory structure looks so:


 

You can download the source from the github repo here.



No comments:

Total Pageviews

Popular Posts