Chapters

Hide chapters

Android Test-Driven Development by Tutorials

Second Edition · Android 11 · Kotlin 1.5 · Android Studio 4.2.1

Section II: Testing on a New Project

Section 2: 8 chapters
Show chapters Hide chapters

Section III: TDD on Legacy Projects

Section 3: 8 chapters
Show chapters Hide chapters

1. Introduction
Written by Victoria Gonda

Welcome to the first chapter of this book. If you’re curious about learning more about testing on Android, you’re in the right place.

In this book, you’ll learn about test-driven development (TDD) on Android. You’ll do this by working step-by-step in real apps as you increase your testing knowledge.

Whether you’re a testing beginner, or ready to bring your testing skills to the next level, this book is for you. By the end, you’ll be able to fully apply the TDD process to both your greenfield and legacy apps.

Who is this book for?

This book is intended for Android developers who know at least the basics of the Android framework. You should also know enough about Kotlin that you’re comfortable working with it.

Although it’s not required, it’s helpful to have an understanding of design patterns, dependency injection and app architecture. Whether you’re new to testing and TDD, or eager to apply your existing knowledge to your apps, you’ll find the information within this book useful.

This book covers everything from the bare basics of testing knowledge to applying that knowledge to existing code using TDD. If any of this interests you, read on!

Note: If you already finished Android Apprentice, you’re at a great place to start this book. If your knowledge of Android is limited, that book is the perfect place to start. You can find it at https://www.raywenderlich.com/books/android-apprentice.

How to read this book

This book is divided into three sections, each covering a subsection of material. In these chapters, you’ll follow along by writing tests while you take on new concepts. Depending on what prior knowledge you’re coming in with, you may want to start in a different section or jump around. The sections are as follows:

  • Section 1: This is where to start if you’re new to testing or TDD.
  • Section 2: If you know the fundamentals of TDD and are ready for some hands-on practice, start here. You’ll learn how to follow TDD to add tests to new apps and features. If there’s a specific topic in this section that interests you, feel free to jump to it. You’ll be directed to a previous chapter if there’s anything you need to know that you might have missed.
  • Section 3: When everything in the previous section looks familiar, you can jump to this section to learn how you can apply your learning to a legacy project.

With that, it’s time for a sneak peek of each chapter, so you can choose where to start.

Section 1

Chapter 1: Introduction

You’re here now. You’re learning what this book covers, and where you should start.

Chapter 2: What Is a Test?

Continue to this chapter to learn about automated testing and why it’s important. This is an excellent read if you’re new to writing tests or need convincing for why writing tests is important. In this chapter, you’ll also learn what you should and should not test, and the importance of code coverage.

Chapter 3: What Is TDD?

This chapter introduces a core concept of this book: test-driven development. In it, you’ll learn the basics of this software development process and why it’s useful. You’ll do this by using TDD to create a search URL generating function. Through this hands-on experience, you’ll learn some of the tricky things that can come up when testing, and how you can approach them.

Section 2

Chapter 4: The Testing Pyramid

As you might imagine, there are multiple ways to test your app, each having a different focus. In this chapter, you’ll learn about the testing pyramid, which describes the types of tests you can write covering different layers of your app, and how many of them you should write. It’s a great intro before diving into the specifics in the next chapters.

Chapter 5: Unit Tests

This is the chapter where you’ll learn about unit tests, what they are and where you should use them. You’ll use JUnit while practicing TDD to add some logic to a new Cocktail app. In this app, you can search for drinks, “favorite” them and play a trivia game. The building blocks you’ll learn in this chapter will carry you through the rest of the book.

Chapter 6: Architecting for Testing

The architecture of your project can make or break your testing experience. In this chapter, you’ll learn about some of the most common app architectures. Along with this, you’ll learn some of the pros and cons of each architecture and how they affect your tests. You’ll finish off with some design principles that will help you when you practice the refactor step of TDD.

Chapter 7: Introduction to Mockito

Revisiting the Cocktail app, you’ll take your unit tests to the next level using Mockito. You’ll learn the basics of how to use mocks and spies to stub behavior to keep unit tests pure, completely independent from other classes. You’ll also come away understanding the difference between black-box and white-box testing.

Chapter 8: Integration

This chapter looks at testing how different objects interact or integrate. You’ll practice writing integration tests by working on a Wishlist app to keep track of the wishlists of your family and friends. While doing so, you’ll learn how to manage your interactions with the Android framework while testing.

Chapter 9: Testing the Persistence Layer

Continuing with the Wishlist app, this chapter teaches you the fundamentals of testing a persistence layer while you build out the database for this app. You’ll learn how to handle statefulness in your tests and strategies for creating randomized test data. While working with the RoomDB library, you’ll also come across the gray line of testing your code versus testing the library you’re using.

Chapter 10: Testing the Network Layer

While HTTP requests can be unpredictable, here, you’ll learn how to write predictable network tests while working on an app named Punchline that shows you random jokes to share. You’ll learn three different libraries to help you accomplish varying degrees of depth in your testing. With all these libraries, it opens up a discussion about how to choose the right libraries for your project.

Chapter 11: User Interface

Finishing out this section is all about testing your user interface. While adding the visual elements to the Punchline app, you’ll learn how to match views, perform actions and make assertions. By having these tests, you’ll be able to guard against any future regressions.

Section 3

Chapter 12: Common Legacy App Problems

Launching into this section about testing a pre-existing app, you start by learning some of the common problems you might encounter while doing this. By recognizing some familiar patterns, you’ll be prepared to tackle these situations in future chapters. Along the way, you’ll learn some reasons why these patterns may have made their way into the code.

Chapter 13: High-Level Testing With Espresso

One of the first tests you might add to a legacy application is an Espresso test. In this chapter, you’re tasked with adding features to the Coding Companion Finder app, an app to find a dog or cat to pair program with. Along the way, you’ll learn how to set boundaries and set up testing in an existing app.

Chapter 14: Hands-On Focused Refactoring

Refactoring is often used to help make an app more testable and easier to modify. This chapter guides you through refactoring part of the Coding Companion Finder app by writing tests for safety. You’ll learn how taking small steps and moving slowly can keep your test suite green as you make changes — even if that means you need to change your tests.

Chapter 15: Refactoring Your Tests

Your tests are code too, and sometimes the first tests you write aren’t the quickest or most maintainable. That’s why, in addition to working on refactoring the app code, you’ll refactor the tests of the Coding Companion Finder app. You’ll be able to isolate your tests better, making them less brittle and more helpful.

Chapter 16: Strategies for Handling Test Data

There are many ways to manage test data, and this chapter is perfect for helping you make that decision. By learning the pros and cons of each strategy, you’ll be better equipped to decide what’s best for your app.

Chapter 17: Continuous Integration & Other Related Tools

Of the many tools you can interact with within the testing world, one type is Continuous Integration (CI) frameworks. In this chapter, you’ll explore what it means to have CI for your tests, along with some tools you can use to achieve it. You’ll learn the pros and cons of different strategies so you can make the best decision for your team.

Chapter 18: Testing Around Other Components

One of the tricky parts of testing can be around interacting with other libraries and parts of the Android framework. One example you’ll see is with permissions. In this chapter, you’ll learn how to set boundaries in your test and use strategies for handling these situations, including when it might be better to not spend time on writing tests.

Appendix

Other Related Techniques

This chapter covers some techniques you can use to complement your TDD process. By using these strategies, you can choose how to define what you’ll test and build. You can also bridge the gap between technical and non-technical written specifications.

Key points

  • There are different places you can start reading this book, depending on what you already know.
  • You can jump to the chapters that interest you most.
  • Previous chapters are referenced when necessary.

Where to go from here?

You now know what this book is about, so it’s time to dive in. Feel free to continue to the next chapter to get started or skip ahead to another chapter on a topic that interests you the most.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2024 Kodeco Inc.