Why JUnit Vintage
- Instead of updating all existing tests, test extensions, and custom build test infrastructure to migrate to JUnit Jupiter. JUnit Vintage test engine allows you to execute current tests based on JUnit 3 and JUnit 4 using the JUnit Platform infrastructure.
- Since all classes and annotations specific to JUnit Jupiter reside under a new org.junit.jupiter base package, having JUnit 4 and JUnit Jupiter in the classpath does not lead to any conflicts. Thus maintaining existing JUnit 4 tests alongside JUnit Jupiter tests is safe.
- Developers can migrate to JUnit Jupiter at their own pace as the JUnit team will continue to provide maintenance and bug fix releases for the JUnit 4.x baseline.
How Does JUnit Vintage Works
Tests written using JUnit 3 and JUnit 4 will work on the JUnit Platform. You have to just include the junit-vintage dependency in your build, and everything else will work.
You can very simply add junit-vintage-engine as a dependency in Gradle and Maven like :
Maven: pom.xml
Program
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit.vintage.version}</version>
</dependency>

You can also try this code with Online Java Compiler
Run Code
Gradle: build.gradle
Program
dependencies {
testCompile("junit:junit:${junit4Version}")
testRuntime("org.junit.vintage:junit-vintage-engine:${junitVintageVersion}")
}

You can also try this code with Online Java Compiler
Run Code
Example
We will understand the use of junit-vintage-engine with an example on application from com.makotojava.learn.junit package. The Application and test file both run on JUnit5, with JUnit 3 and JUnit 4 that use JUnit Vintage Engine.
You can clone the example repository like this:
git clone https://github.com/makotogo/HelloJUnit5Part2
As explained earlier, the application contains pom.xml and build.gradle files with all the dependencies for the junit-vintage-engine.
JUnit 4 Test
The first example shows part of one of the JUnit 4 tests from the example application located in the src/test/java tree, in the com.makotojava.learn.junit4 package.
Program
package com.makotojava.learn.junit4;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.List;
import javax.sql.DataSource;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import com.makotojava.learn.junit.Person;
import com.makotojava.learn.junit.PersonDaoBean;
import com.makotojava.learn.junit.PersonGenerator;
import com.makotojava.learn.junit.PersonTestEnum;
import com.makotojava.learn.junit.TestSpringConfiguration;
import com.makotojava.learn.junit.TestSpringConfigurationEmptyDb;
/**
* Test class for PersonDaoBean.
*
* Uses JUnit 4 API
* /
public class PersonDaoBeanTest {
private ApplicationContext ctx;
private PersonDaoBean classUnderTest;
@Before
public void setUp() throws Exception {
ctx = new AnnotationConfigApplicationContext(TestSpringConfiguration.class);
classUnderTest = ctx.getBean(PersonDaoBean.class);
}
@After
public void tearDown() throws Exception {
DataSource dataSource = (DataSource) ctx.getBean("dataSource");
if (dataSource instanceof EmbeddedDatabase) {
((EmbeddedDatabase) dataSource).shutdown();
}
}
@Test
public void findAll() {
assertNotNull(classUnderTest);
List<Person> people = classUnderTest.findAll();
assertNotNull(people);
assertFalse(people.isEmpty());
assertEquals(PersonTestEnum.values().length, people.size());
}

You can also try this code with Online Java Compiler
Run Code
Output

JUnit 3 Test
The second example shows a JUnit 3 test from the sample application located in the src/test/java tree, in the com.makotojava.learn.junit3 package.
Program
package com.makotojava.learn.junit3;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import com.makotojava.learn.junit.Person;
import com.makotojava.learn.junit.PersonDaoBean;
import com.makotojava.learn.junit.PersonGenerator;
import com.makotojava.learn.junit.PersonTestEnum;
import com.makotojava.learn.junit.TestSpringConfiguration;
import com.makotojava.learn.junit.TestSpringConfigurationEmptyDb;
import junit.framework.TestCase;
/**
* Test class for PersonDaoBean.
*
* Uses JUnit 3 API
*/
public class PersonDaoBeanTest extends TestCase {
private ApplicationContext ctx;
private PersonDaoBean classUnderTest;
@Override
protected void setUp() throws Exception {
ctx = new AnnotationConfigApplicationContext(TestSpringConfiguration.class);
classUnderTest = ctx.getBean(PersonDaoBean.class);
}
@Override
protected void tearDown() throws Exception {
DataSource dataSource = (DataSource) ctx.getBean("dataSource");
if (dataSource instanceof EmbeddedDatabase) {
((EmbeddedDatabase) dataSource).shutdown();
}
}
public void testFindAll() {
assertNotNull(classUnderTest);
List<Person> people = classUnderTest.findAll();
assertNotNull(people);
assertFalse(people.isEmpty());
assertEquals(PersonTestEnum.values().length, people.size());
}

You can also try this code with Online Java Compiler
Run Code
Output

Check out JUnit Interview Questions here.
FAQs
-
Is JUnit 5 the same as Jupiter?
JUnit Jupiter is the API is used for writing tests using JUnit 5. While JUnit 5 is the project name and version that includes the separation of concerns present in all three major modules: JUnit Jupiter, JUnit Platform, and JUnit Vintage.
-
Should I use JUnit 5 or 4?
Migrating tests can be a daunting task if you've been using JUnit 4 for a while. JUnit 5 can run JUnit 3 and JUnit 4 tests using the Vintage library, so you don't need to migrate.
Still features for describing, organizing, and executing tests and lambda functions and more give good reasons to start writing tests in JUnit 5.
-
Do JUnit 4 tests need to be public?
JUnit 4 requires that all test methods be public and not return anything. JUnit 4 also requires that all test classes are public.
-
What is the use of JUnit dependency?
The junit-jupiter-api dependency provides the public API allowing us to write tests and extensions which use JUnit 5. The junit-jupiter-engine dependency ensures that the Maven Surefire Plugin will run tests that use JUnit 5.
Key Takeaways
JUnit Vintage is an essential engine that provides backward compatibility to JUnit 3 and JUnit 4. That saves resources and effort to recreate all the tests on different versions of JUnit.
Throughout this article, we have learned the fundamentals of JUnit Vintage Engine, its importance, functionality, and implementation in Eclipse. If you want to understand Java comprehensively, check out our online Java Course here.
Hence learning never stops, and there is a lot more to learn.
So head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more. Till then, Happy Learning!