Test-Driven Development in Android

Jan 24 2023 · Kotlin 1.6, Android 12, AS Bumblebee 2021.1.1

Part 1: Unit Tests

04. Set Up JUnit

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 03. Build Unit Tests Next episode: 05. Create Unit Tests

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Transcript: 04. Set Up JUnit

Find the starter project for this application in the github repo for this episode and open it in Android Studio. Wait until gradle has finished syncing your project. Once that is done run your app on your emulator and you will see a blank screen. For now we are just going to minimize this. Stop your application.

And we’ll start writing our first tests. But to start writing our tests we will use JUnit so we need to add the appropiate dependencies.

Open your project navigator, under gradle, module and in here at the end we are going to add this line:

testImplementation 'junit:junit:4.13.2'

Press sync now and wait until gradle finishes syncing all your dependencies.

Notice that it is test implementation instead of implementation because you will only use these dependencies for testing purposes. These basically means that it wont be bundled in your applications apk that your device or emulator will run.

Also, when you create new android projects this line us usually added by default, we are only adding it here manually for educational purposes.

Well, gradle has finished syncing our dependencies. And we are going to write our first unit test. To do that we are going to create a new package. It is easier to do that using the project view. And under app source we are going to create a new directory. New, directory, and type all of this:

test/java/com/raywenderlich/android/cocktails/game/model

Enter, and inside the model package which is the last package create a new kotlin class file and we are going to name it GameUnitTests.

We are creating the GameUnitTests class because we are going to write tests for the first class of our cocktail game which is going to be named Game. As simple as that.