When you create a new android project
junit4 is the dependency you get automatically. However, JUnit5 is the next generation of JUnit so why not migrate the default JUnit4 dependency to the shiny new JUnit5 one? Then we can have a look at some of the fancy features of JUnit5.
Note: I’ll only focus on the migration of a newly created application. If you have a project that already has JUnit4 tests then you’d have more steps to fully migrate to JUnit5.
JUnit5 migration from JUnit4
Since we don’t have any test in a newly created android application (other than the
assertEquals(4, 2 + 2) example test) we can go ahead and change the dependency to
When we synch the Gradle we’ll able to replace the @Test annotation and assert imports.
However, after removing the old imports and trying to run the test it’ll fail with the following error.
The second and final touch to make the JUnit5 work is adding the following code in the
android scope in the build.gradle.
Note: Have a look at the “Android Plugin DSL Reference” of the testOptions. Great resource.
ExampleUnitTest should pass.
Some fancy features of JUnit5
When you want to feed your test with multiple values the @ParameterizedTest is what you are looking for. The test you’ve implemented runs for each source you provide. The following test runs 6 times. 4 times for the string values + 1 time for null + 1 time for empty string.
Another useful, simple, and powerful annotation for parameterized tests is the @EnumSource. I have 2 sample codes snipped for this “Source of arguments”.
One method to run them all
When we have an enum class that is so common to have a shared rule for all of them. The @EnumSource is the annotation we want to test those kinds of scenarios.
The test above is for the property of the enum class. We could have a test for another function that uses the enum class as well. The main idea is to cover all the enum values in a single test.
Just a Subset of an Enum Class
We have the same enum class with an additional property.
JUnit5 helps us to target specific enum values to test in an intuitive way.
All we have to do is to add the string values of the enum names.
Check out the @MethodSource to pass multiple parameters and also objects of classes that are not supported by @ValueSource.
This is an important addition to JUnit4 since it gives more control to testers with the
Executable s. (It looks much better on Kotlin, of course. 😉) The execution order is my personal favorite. I still need to work more with it to be able to say more about its performance or its advantages.
Here is a sample I think would be useful.
Let’s assume we have a view model that has a UI state of a current page. The rule we want to test is the page increments are happening until the current page is 10. The order is important. Let’s see how the dynamic test would help us out.
So, we could’ve written this code with a simple @Test annotation as well however when the flows and logics get complicated, dealing with the
DynamicTest s would be easier. It looks pretty clean to me.
As I said, I need to do more experiments with the dynamic test but it looks promising.
Now you know how to start writing JUnit5 tests for your newly created android project. You also know the @ParameterizedTest annotation is super useful and easy to write. Finally, if you want to level up your unit testing game, you know the dynamic test is the way to go on JUnit5.
Don’t forget to clap and subscribe. Leave a comment about what you think about unit testing in android development and also add what I could have added to this article.