Mocking Your Way in Software Development

Muhammad Ayaz Dzulfikar
MeetU Engineering
Published in
3 min readMar 21, 2018

One important aspect in software development is testing. We can write thousand lines of code, yet they are useless if they didn’t work as we expected. How to make sure that our code is working in the correct way? Yes, testing!

When we worked in software development, usually we have to make several class which have their own tasks. These classes may interact with each others. So, it can be very tiring to test. Imagine we have 3 class A, B, and C. Function in A will call a function in B, and that function will call a function in C. It will be hard to test the behaviour, since essentially there are lots of things to check, right? Not to mention if we have to call an API or access database, as calling API is heavily dependent on the network, and accessing database may tamper with important data. All of this boils down to one question: Can we actually isolate a class from its dependencies, and check whether its behaviour is as we expected?

Mocking

Yes, we can!

Isolating a class can be achieved by controlling its dependencies. In other words, we need to find a way to control how its dependencies. It can be achieved by mocking its dependencies.

Mocking?

Mocking here is different with insulting :P Here, mocking means simulated object that mimics the real object. However, we can control its behaviour. We can also check whether the class we test work as we want or not. Hence, we can, in a sense, isolate a class from its dependencies by mocking the dependencies. Not only that, now have more power in Unit Testing!

PowerMockito

For MeetU Engineering, we decided to use PowerMockito for Unit Testing. We can add PowerMockito in our project by adding these following lines in our app’s build.gradle:

testImplementation 'junit:junit:4.12'
testImplementation 'org.powermock:powermock-api-mockito:1.6.1'
testImplementation 'org.powermock:powermock-module-junit4-rule-agent:1.6.1'
testImplementation 'org.powermock:powermock-module-junit4-rule:1.6.1'
testImplementation 'org.powermock:powermock-module-junit4:1.6.1'

Now, let’s say we have the following class:

Take a peek at magicFunction method. We can see that it depended on a function anotherMagic from class B. Let’s try to test this magicFunction!

We need to check by mocking bar, and we need to control how anotherMagic behave. So, we can create a test like this:

Let’s see what it actually does:

  • @RunWith(PowerMockRunner.class) means we want to run unit test with PowerMockRunner.
  • Using @Before annotation means the function setUp will be run each time before each test. Also you can see how to create mock object mockBar inside that function :)
  • Next, we can specify how a mock object behave. when(mockBar.anotherMagic(anyInt()).thenReturn(0) means if we call anotherMagic with any int, then it will return 0.
  • Then, in the last 2 line, we can check whether the result is correct, and check how many times anotherMagic is called.

Actually, for setting up mock object, we can use @Mock annotation in each variable, and then use initMocks(this) in setUp. However, personally I prefer mocking them one by one in setUp, as it is more verbose in my opinion.

Conclusion

Mocking is a very important aspect in testing our code. Beside mocking object like that, we can also mock static class (however, it is quite annoying to do). Learning it is very important, and choosing the right tool will help us to do so.

--

--