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

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

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

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?

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

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?

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.

Thanks for the tip. Let me checkout that book.

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.

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?)

Everything works now with latest lib versions.

1 Like

@igorganapolsky Glad you fixed it! :]

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)
}

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

This tutorial is more than six months old so questions are no longer supported at the moment for it. Thank you!