Leave a rating/review
Our joke app needs to have a button thats makes a new joke appear each time it is clicked right? so, thats what we want to verify with our first test.
Create a new test under your properties.
@Test
fun onLaunchButtonIsDisplayed(){
}
I’m going to scroll down so you guys can see. I’m going to close the project navigator.
First we want to mock our repository and return a network request that has a set json response. We dont actually want to make any network calls with out tests.
declareMock<Repository> {
whenever(getJoke())
.thenReturn(Single.just(Joke(
faker.idNumber().valid(),
faker.lorem().sentence())))
}
Next we need to open our activity and perform the verification that we discussed which is to verify the button is displayed.
To lauch our acitivity we are going to use a class named ActivityScenario:
ActivityScenario.launch(MainActivity::class.java)
Finally we will use espresso to test:
onView(withId(R.id.buttonNewJoke))
.check(matches(isDisplayed()))
So, here is what we did.
As I just said we used the ActivityScenario class to launch our main activity, which is where our buttonNewJoke will be. This ActivityScenario class is part of the AndroidX.test dependencies.
And then here we are using espresso to check that on this activity we have a button with an id called buttonNewJoke and we are checking that it is displayed in our ui.
You will notice that there is an error here, our test wont compile because we dont yet have our button to display a new joke. We will do that just now. Open the project pane, scroll up.
Close the java package inside resources, layout. Activity_main.xml
We’re going to add our new button. Switch to your code view.
And in here im going to create a new button.
Im just going to copy and paste the code because I trust that you guys already know how to create a new button.
We added a button with an id of buttonnNewJoke which is the id that we wnated here, with a width of wrap_content and visibility of gone.
Don’t worry if Android complains our button not having constraints. Remember that we are just worried about having enough code to be able to compile our test. Notice that we’ve’ a visibility gone. Remember that we want our test to fail if we have teh default visibility which is visible our test will pass and we dont want that we want our test to fail.
Before you execute your test it will be better if you turn off all of the animations on your emulator this will make your test run a little bit faster.
To do that simply go to the settings of your emulator, open the settings on your device. Make sure to have developer mode enabled. If you dont have developer mode enabled open about emulated device, and in build number click it several times.
4 steps away, 3 steps away, 2 steps away, one step away. Now we are in developer mode, advanced, developer options, scroll down and in the drawing section turn off window animation scale, turn off, turn off window transition animation scale and also turn off the animator duration scale to off.
Now again this is not like mandatory but it will make your ui tests run much faster and it will also prevent some errors from happending. So just to be safe turn off this 3 options you dont need them.
Go back to your code, execute your test. You will notice it wil take longer than unit tests and integration tests because it is a ui tests and it needs to launch the activity and needs to perform many actions. Well in our case is just one action but you get the idea. You may see that the device is launched, and several actions you wrote are executed.
Go back to your test and you will see it fails.
And this is our error, expected is displayed on the screen to the user but got a button with visibility equals gone.
This error is basically saying that we wanted to see a button but that button wasn’t visible. Thats kind of expected because we are doing test driven development and our button was hidden. So, go back to your code to make our test pass we’re’ simply going to change this line,well not change it we are just going to remove.
Execute your test again and the test will pass.