Why You Should Have Migrated from JUnit 3 to JUnit 5 Yesterday

Daniel Beleza
3 min readApr 12, 2023
Photo by mostafa meraji on Unsplash

Introduction

If you're an Android developer, you're probably familiar with JUnit, the most popular testing framework for Java applications. But did you know that JUnit 3, which has been around since 2002, was replaced by JUnit 4 in 2006, and then by JUnit 5 in 2017? If you're still using JUnit 3 in your Android project, it's time to upgrade. In this article, we'll explore the benefits of migrating to JUnit 5 and walk you through the steps to upgrade your Android project.

Why Upgrade to JUnit 5?

According to the book "Android Programming: The Big Nerd Ranch Guide," "JUnit 5 has a number of new features and improvements over JUnit 4 and JUnit 3, including a more modern and flexible architecture, better integration with Java 8 features, and improved support for parameterized tests and dynamic tests." In addition, JUnit 5 allows for better test organization, easier test writing, and improved test execution. So, in summary:

Benefits of Migrating to JUnit 5:

  • More modern and flexible architecture
  • Better integration with Java 8 features
  • Improved support for parameterized tests and dynamic tests
  • Better test organization
  • Easier test writing
  • Improved test execution

Migrating from JUnit3 to JUnit5

Migrating from JUnit 3 to JUnit 5 in Android Studio Migrating from JUnit 3 to JUnit 5 in your Android project can seem daunting, but it's actually quite straightforward. Here are the steps to follow:

Step 1: Update your dependencies Add the following dependencies to your build.gradle file:

dependencies { 
testImplementation 'org.junit.jupiter:junit-jupiter:5.7.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.2'
}

Step 2: Update your test classes In your test classes, replace import junit.framework.TestCase; with import org.junit.jupiter.api.Test; and import junit.framework.TestSuite; with import org.junit.platform.suite.api.SelectPackages;. Update your test methods to use the @Test annotation instead of extending TestCase.

Step 3: Update your test suite Create a new class that extends org.junit.platform.runner.JUnitPlatform and annotate it with @SelectPackages to include the package that contains your test classes. Replace TestRunner with this new class in your test suite.

That's it! You've successfully migrated from JUnit 3 to JUnit 5.

Photo by Eilis Garvey on Unsplash

Conclusion

Migrating from JUnit 3 to JUnit 5 in your Android project might seem like a daunting task, but the benefits are well worth it. As the author of "Android Programming: The Big Nerd Ranch Guide" suggests, "JUnit 5 has a number of new features and improvements over JUnit 4 and JUnit 3." And with the straightforward steps we've outlined, you can upgrade your project with ease. So, what are you waiting for? Upgrade to JUnit 5 and start reaping the benefits today!

--

--

Daniel Beleza

I’m an Android developer geek, with a great passion of looking for better ways to write good code and design great apps for everyone!