Unlike mocks, fake implementations give us full command over all of our dependencies in unit exams, permitting you to precisely and thoroughly try reactive moves.

Unlike mocks, fake implementations give us full command over all of our dependencies in unit exams, permitting you to precisely and thoroughly try reactive moves.

At OkCupid, we quite often utilize the Mockito library for creating mocks in our dependencies used in your JUnit assessments. This permits all of us to conveniently mock return principles beyond doubt methods, or even validate an approach ended up being known as on one in our dependencies, nonetheless it can supply complications because integration involving the element under ensure that you the dependencies much more complex.

On this page, we’re browsing walk through a constraint discovered utilizing a mocking library, and talk about exactly how we were able to work through that restriction through our very own fake execution. Let’s start with setting the period when it comes down to problem.

Establishing The Stage

Before we discuss the complications, let’s verify we see the element under examination. We’ll be looking at a ViewModel definitely accountable for making a network request and showing some effects.

Under may be the snippet for your ViewModel, however if you’d want to see the associated tuition, you will find all of them within gist. Observe that in this ViewModel, when it’s produced, we request profile ideas in the init means:

Once the ViewModel is created, we’ll produce a running condition to the LiveData. Next, we’ll request a profile, and publish a ProfileViewState if the name succeeds or fails.

That is everything we require for our element under test. Next we can test it.

Testing The ViewModel

We’ll start out with one good situation examination to ensure that as soon as we request a user, a loading condition was emitted with an information state. Let’s see what that test looks like:

If you’d like to see the utilization of .testObserver() available they inside gist .

The Test Fails

To your surprise, this examination will do not succeed! We’re mocking an effective information request, therefore we should anticipate which our observedStates provides two entries: one when it comes down to running condition, and another when it comes to successful facts state. Upon running the exam, the most important assertion fails. Our test states that observedStates.size is one, additionally the price will be the facts state.

Put simply, our examination was not capable examine that a running state took place.

How It Happened?

Let’s consider what’s various in our product test than real rule. In our product examination, we’re moving Scheduler.trampoline() from RxJava which helps to really make the community request run as if they had been sequential. With regards to this test, its just as if the circle request succeeds quickly after the ViewModel is done.

After that, after our very own ViewModel is made, we implement an examination observer regarding ViewModel.state LiveData, which can be currently in a loaded data condition. This means the running state occurred too much back the amount of time – we can’t notice a LiveData earlier’s created, therefore there is not a chance to confirm a loading state actually happened.

This issue is actually as a result of the mocking collection, which tells the mock repository to return details right away. Rather, we can make our own fake utilization of a ProfileRepository we have actually complete power over, and that can control that emission of data assure all of our device test catches the running condition.

Promoting A Fake

To create a fake implementation, we start with generating another class that implements all of our interface. https://datingmentor.org/escort/rialto/ Keep in mind, we don’t wish all of our phony to return facts right away, for the reason that it will merely cause the same challenge. As an alternative, since we’re making use of RxJava, we could put into action our very own artificial in a manner that uses a BehaviorSubject behind the scenes that we can get a handle on.

The implementation you use here may change if you use coroutines, but the principle remains the same: we don’t wish come back from fetchProfile() with information straight away. We need to make sure the artificial implementation regulates correctly when that information is produced.

Controlling Data Pollutants

Since our very own artificial execution is utilizing a BehaviorSubject due to the fact root data source, we could generate our very own public approach to give off to they once we like:

Updating Examinations To Validate Running State

Since we now have a system set up where we’ve fine-grained control over when data is released from our repository, we could control this to accurately check our very own loading condition. We’ll follow this recipe:

  1. Build all of our fake repository and ViewModel aspect
  2. Since the fake cannot produce data straight away, I will be capable verify that individuals come in a running state.
  3. We are able to control the artificial execution to produce information.
  4. Last, we can verify our ViewModel has become in a loaded information county.


Mocking libraries supply a simple solution for promoting dependencies to utilize inside our Android os unit exams, but come at the expense of having limitations for managing the conduct of the dependencies. By leveraging connects and our personal phony execution that is available within our rule base, there is full control over the dependencies and we also can use that to manage the emission of information for reactive passes to completely unit check all of our elements.

I hope your receive this beneficial! When you have additional samples of utilizing a fake compared to a mock, let me know on Twitter.

Enthusiastic about employed by OkCupid? We’re employing!

Be the first to comment

Leave a comment

Your email address will not be published.