Get startedGet started for free

Mocking and Stubbing in Unit Tests

1. Mocking and Stubbing in Unit Tests

Welcome back! Now we will learn how to simulate any test scenario in unit tests.

2. Motivation: Dependencies

Recall the dependency flow from earlier. The currency exchange app depended on the return values of the European Central Bank Server. But since `getRate()` returned unpredictable values, we couldn't reliably verify our calculations!

3. Dependency management in testing

Now we'll overcome this issue by isolating and testing the calculation logic using technique called mocking. Mocking creates simulated versions or behaviors for real objects that are hard to test directly. Mocking allows us to focus on the behavior of the code being tested without worrying about external dependencies. To create mocks, we'll use the Mockito library, importing two key static methods: mock and when.

4. Mocking

A mock is like a stand-in actor in a play. It will "pretend" to be an object from another class only as far as the test is concerned. Consider the exchange app from the previous lesson, where we had a method calling the european central bank server for rates. If we can get another, more predictable object to "pretend" to be the `EuropeanCentralBank`, then we can verify our exact calculations!

5. Mocking: Currency exchange

So how would we make a "pretending" object? In the test, instead of creating a real `EuropeanCentralBankServer` object, we create a mock of it, using the mock method we imported. We then pass the mock object to the `ExchangeApp` constructor, just as we would pass a real object. We can then continue with our test setup, with one additional step: we have to program our mock. We do this by using `when` to tell the mock object exactly what call to expect (`getRateEuroTo()` with USD) and using `thenReturn` to program what output the mock should respond with. We then call the `ExhangeApp` `convertEuroTo` method as before. Finally, in the assert step, we can `assertEquals` on the exact output! This output, unlike the values of different currencies, will never change unless we program it differently.

6. Common mistake

We should never forget to program our mocks! Consider the previous test, but with the `when-thenReturn` step omitted. If we forget to program our mock, it will return a default value determined inside the Mockito library.

7. Stubbing

The test will then fail with the following message, because the mocked bank server will return a default exchange rate of 0. Programming a mock is referred to as stubbing. More formally, stubbing is providing minimal, pre-defined answers to calls made during the test, usually not responding at all to anything outside what's programmed for the test.

8. Exceptions with Mockito

Finally, Mockito can also simulate error scenarios. Instead of saying thenReturn we can say thenThrow and inside the thenThrow method create the exception object we want to throw.

9. Let's practice!

Mocking turns testing into a fun sandbox - let's play around!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.