Annyce Davis

Helping you write beautiful code

  • Home
  • Public Speaking
  • About Me
  • Courses
  • Life

Testing Tricks #4 – Improving Readability

January 9, 2016 by Annyce Davis Leave a Comment

Readability is one of the key components to creating high quality unit and integration tests. Yet, we may hinder the readability of our tests by bogging them down with needless information. Let’s take a look at an example.

Example


Here we have a simple test that is desiring to make sure the URL we used to create the Video object matches the result of calling the playCurrentVideo method in the VideoPlaybackService class. We have created a helper method that allows us to pass in the variables for the Video object creation. However, in most cases we don’t need to have all of the parameters set. In fact, the duration and displayDate have no impact on the current method under test. So we’ve added additional, needless information to the test that makes it more difficult to understand what’s going on.

Solution


We can fix this issue by updating the Video class to adhere more closely to the Builder Pattern so that the additional setting of fields is not needed. Here is the current setter used for the video’s URL.


We can update it to pass back the Video object (this) instead of just being void. Let’s see how this will help to make the test more readable and understandable.

With Builder Pattern


The end result is something much more readable and maintainable. Happy testing!

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)

Related

Filed Under: Android Tagged With: Builder Pattern, Readability, Unit Testing

Newsletter

Start a new tech group on Meetup

Follow Me

Dev Machine Setup – Mobile Development

A new machine. Now what? Time to set it all up. Inevitably I end up going back and forth from my old machine or worse yet, my memory, to figure out what needs … [Read More...]

Dagger Multibinding in Dynamic Feature Modules

We're adding the use of dynamic feature modules to our app. This will help us to improve build speeds, conditionally include features and decouple our code. As … [Read More...]

Categories

  • Android (49)
  • Career (2)
  • Communication (4)
  • Flutter (1)
  • Git (4)
  • Gradle (4)
  • Grails (23)
  • Java (8)
  • JavaScript (6)
  • Kotlin (11)
  • Life (4)
  • Public Speaking (22)
  • RxJava (1)
  • Software Development (3)
  • Twitter (3)
  • Uncategorized (11)
  • Video Course (4)

Copyright © 2021 · Beautiful Pro Theme on Genesis Framework · WordPress · Log in