ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Follow publication

Testing on Android using JUnit 5

We will explore a few new features for much cleaner tests

Andrew Lord
ProAndroidDev
Published in
8 min readNov 30, 2018

--

JUnit 4 has always been the standard unit testing framework for Android development, however, a production version of JUnit 5 has been available for quite a while. JUnit 5 is the next generation of JUnit testing and has quite a new structure. From a user-perspective it offers a number of useful new features and various other benefits. Often Android developers are used to waiting a while for new things from the Java-world to be an option on Android. In the case of JUnit 5, with some changes to your Gradle dependencies, you can use it right now from your Java and Kotlin unit tests.

Before we continue: Please check out the article on my blog, Lord Codes, you will find code snippets with themed syntax highlighting and much more, it is definitely my preferred way to read it! 馃憤

Testing on Android with JUnit 5

The architecture of JUnit 5 is quite a significant change from before, with the framework being split into three major components.

- JUnit Platform is the underlying layer on which testing frameworks can run on the JVM and offers up the API for different test engines.

- JUnit Jupiter defines how we write JUnit 5 tests and then contains an engine for running these tests on the platform.

- JUnit Vintage gives us an engine for running our previous JUnit 4 tests. We don鈥檛 need to worry about having to update all of our tests in one go, as we can use the vintage engine to run these and have both JUnit 4 and 5 tests within our project.

As users of the testing framework, it鈥檚 likely that the most interesting part is the new features we get access to. Nested tests allow us to put our tests into groups, increasing readability and allowing us to reduce repetition within test names. Parameterised tests are really powerful and can also reduce duplicating tests to give different inputs, the parameters can even be provided through a variety of mechanisms. Dynamic tests offer an API to generate tests on the fly using a test factory, rather than hard-coding each of the tests.

Parameterised tests run tests with different inputs

--

--

Published in ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Written by Andrew Lord

Mobile Lead @WorkiroApp . Android and iOS Engineer. Author @ http://lordcodes.com. Avid gamer and music fan.

Responses (1)

Write a response