Group Group Group Group Group Group Group Group Group

Test-Driven Development Tutorial for Android: Getting Started | Ray Wenderlich


#1

Learn the basics of test-driven development, or TDD, and discover how to use TDD effectively when developing your Android apps!


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/7109-test-driven-development-tutorial-for-android-getting-started

#2

awesome tutorial, i’m trying the work manager tutorial and testing that particular component is very hard, since the big dependency in the android sdk and async nature of the work manager, where can i find some help, tutorial or any resource for testing code with dependency on the work manager?
thanks in advance


#3

Thanks!
Oh that’s a great question! I haven’t gotten a change to work with the Work Manager yet, so I’m not sure off the top of my head. My first thoughts are working with an integration test, or maybe with Roboletric. I think what I default to trying first is abstracting what I can into helper classes and test those.

It looks like @fernandospr wrote the tutorial on Work Manager. Have you seen much on how it can be tested?


#4

Hey! I couldn’t find much info on Work Manager testing yet. However, I think the following are useful:


#5

Awesome tutorial. Let helped me understand TDD. I have a question, though. What’s the approach to follow when you already have an existing codebase? How do you start adding tests?


#6

The book Working Effectively With Legacy Code has a great section on writing tests for existing code.

tl;dr I’d say is before you start working on something in your existing code, write I tests for that behavior first, whether it’s to keep the same behavior you already have (test should start green and stay green), or writing a test for new/changed behavior.


#7

Thanks for the tip. Let me checkout that book.


#8

When running your tests, I’m getting this error:

Wanted but not invoked:
victoryRepository.getVictoryCount();
-> at com.raywenderlich.android.smallvictories.VictoryViewModelTest.incrementVictoryCountCallsRepository(VictoryViewModelTest.kt:50)
Actually, there were zero interactions with this mock.


#9

Oh no! Thanks for pointing this out, @igorganapolsky. I’ll see if I can reproduce this. Are you seeing in the final project attachment, or while walking through the tutorial (I’m guessing you’ve reached the heading “Writing a UI Test” when you’re seeing this error?)


#10

Everything works now with latest lib versions.


#11

@igorganapolsky Glad you fixed it! :]


#12

I completed the challenge for the reset button. Perhaps you might want to include that in your tutorial:
MainActivityTest
@Test
fun tappingResetChangesCount() {
// Given
onView(withId(R.id.fab))
.perform(click())

    // When
    onView(withText(R.string.action_reset))
            .perform(click())

    // Then
    onView(allOf(withId(R.id.textVictoryCount), withText("0")))
            .check(matches(isDisplayed()))
}

VictoryViewModelTest
@Test
fun resetClearsCount() {
// Given
val count = 5
stubVictoryRepositoryGetVictoryCount(count)

    // When
    viewModel.reset()

    // Then
    verify(mockVictoryRepository).setVictoryCount(count) // Assert
}

VictoryViewModel
fun reset() {
repository.clear()
val newCount = repository.getVictoryCount()
repository.setVictoryCount(newCount)
viewState.value = VictoryUiModel.CountUpdated(newCount)
}


#13

@igorganapolsky Thank you for sharing your solution - much appreciated! :]