JUnit4 vs JUnit5 with spring-boot-starter-test - java

The spring-boot-starter-test dependency adds both JUnit4 vs JUnit5 APIs
Is it OK if developers start using both and any?
Are there any problems expected when codebase have many both JUnit4 vs JUnit5 ?

I believe that is part of JUnit 5's vintage component that has backwards compatibility for JUnit 4

Related

Maven Surefire plugin executes JUnit 4 tests, even though the JUnit Vintage Engine is not in Maven the project

I have a Maven project that contains both JUnit 4 and JUnit 5 tests.
I am using the Maven Surefire plugin in version 3.0.0-M07. The following relevant dependencies are available in the Maven dependency tree (I checked with mvn dependency:tree):
org.junit.jupiter:junit-jupiter-api:jar:5.8.2:test
org.junit.platform:junit-platform-commons:jar:1.8.2:test
org.junit.jupiter:junit-jupiter-params:jar:5.8.2:test
junit:junit:jar:4.13.2:test
According to the JUnit 5 documentation, the JUnit Vintage Engine needs to be present to execute JUnit 4 tests.
However, the Maven Surefire plugin executes both the JUnit 4 and JUnit 5 tests.
Why is the Maven Surefire plugin able to execute JUnit 4 tests, even though there is no dependency on the JUnit Vintage Engine in my Maven project?
By using junit-jupiter-api maven-surefire-plugin/maven-failsafe-plugin loads the junit-vintage-engine automatically and executes JUnit 4 based tests.
The correct way is to use the junit-jupiter-engine instead of junit-jupiter-api. This is explained in the surefire documentation
An deep explanation is available here.

Equivalent for #RunWith(JUnitPlatform.class) for JUnit5

In my project I'm doing some cleanup and decided to move everything to JUnit5.
Till this time, I was using
#RunWith(JUnitPlatform.class)
Now I want to migrate it to #ExtendWith. Is there any equivalent for this JUnitPlatform.class in JUnit5?
You don't need it anymore when using junit 5.
In the junit documentation it states:
Annotating a class with #RunWith(JUnitPlatform.class) allows it to be run with IDEs and build systems that support JUnit 4 but do not yet support the JUnit Platform directly.
So since you are migrating to junit 5 I suppose your build system/IDE supports it. Hence, you don't need the annotation anymore.
Junit4 #RunWith has been replaced by #ExtendWith in JUnit5 as of content from https://www.baeldung.com/junit-5-runwith

hamcrest core, why need this?

I was using JUnit for my TDD in Java and noticed there are two components to download from JUnit.org. First of all, I thought I need JUnit component and downloaded, installed. When I compiled and tried to run my test, it was complaining about Hamcrest classes are not found. So I had to download this one again from their homepage.
So, out of curiosity, why the heck would we need two downloads for one purpose usage from the beginning? Does anyone know why hamcrest core is separate from JUnit, even though it is used by JUnit?
Thanks,
Javabug
JUnit uses Hamcrest. In the past JUnit was embedding the Hamcrest classes which lead to problems, as the projects were evolving in different cycles. In recent JUnit versions (if I'm not wrong, since 4.11) this has been changed and Hamcrest is not embedded. So if you add JUnit as dependency to your project (Maven, Gradle, etc) you will implicit get a dependency to Hamcrest.
I believe this issue on Hamcrest is somehow related to that splitting. https://github.com/hamcrest/JavaHamcrest/issues/92
The class org.junit.Assert has a dependency on Hamcrest core library. As it is part of the static method signature assertThat(), it has to be on the classpath.
If you won't like to use it, a test runtime dependency on hamcrest-core should work, if you want to use it, I recommend a test compile dependency on hamcrest-library (using maven instead of gradle it's all scope test).
There is no transient dependency to be more flexible to upgrade Hamcrest. Btw also Mockito has a Hamcrest dependency. Read more about Understanding Dependencies.
Actually, you want to use as much of the "hamcrest" stuff as possible.
Have a look at assertThat, which makes heavy use of hamcrest matcher classes.
Seriously: I am only using assertThat in my testy by now; and I never ever regretted doing so and abandoning all those other asserts altogether.

Migrating from JUnit to TestNG in IntelliJ IDEA

I found information about migrating from JUnit to TestNG in general (here for example) and about working in IntelliJ IDEA with JUnit and TestNG separately, but nothing about migrating from JUnit to TestNG in IntelliJ IDEA itself.
Can anyone describe the steps required to do this?

Equivalent of Junit 4's #RunWith annotation in Junit 3.8

What it the equivalent of using the #RunWith annotation for Junit 3.8? I've searched for a while on this, but Junit 3.8 is much older and I haven't been able to find much on the subject. We are using a 1.4 jre so Junit 4 is out of the question to use.
If I remember correctly we used to do our own TestRunner implementations that our suites extended.
Turns out we can use Junit 4 to execute tests with Junit 3.8 by using the Junit38ClassRunner which is included in Junit 4.

Categories

Resources