Mockito Sample

1. Introduction

Sometimes test functional requirements are very complex, if you don't know how test your code, mock interface components, you gonna go toxic way from implements test unit cases. Hope use This project for begging write excellent codes

All Testing are working using Mockito 1.10.* dependency

Add dependencies on gradle config project file

  • Permission library (for request permissions at Android 5)
  • SharePreferenceLib library (save internal memory)
  • Glide library for structure data
  • Junit, mockito, hamcrest & powermock Libraries

Dependency Libraries' version:

glideVersion = '3.6.1'
junitVersion = '4.12'
mockitoVersion = '1.10.19'
powerMockito = '1.6.2'
hamcrestVersion = '1.3'

2. Lets Coooode!

In CameraUnitTest testing class, declare variables:

1. A mock reference to interface View class Camera, whole be testing to make all possible workflows(coverage testing)

2. An inject mock object (not reference), Presenter class camera to initialize our workflow paths

3. An object parameterizable ArgumentCaptor class using String class for catch the response

4. An initialize method Mockito workspace run each workflow path

lateinit var cameraView: CameraView

lateinit var cameraPresenter: CameraPresenter

val captorString = slot<String>() // object or Callback

fun preparate() {
    MockKAnnotations.init(this, relaxUnitFun = true)
    cameraPresenter = CameraPresenter(cameraView)

3. Build JunitTestCases

In syntexis the requirement need testing a case whose capture a picture, the realized pic is captured, visualized (display) and eliminated (remove in memory). Assuring that should be really(exactly) the same photo captured from the beginning.

fun takePic() {
    verify { cameraView.openCamera(capture(captorString)) }
    val path = captorString.captured
    verify(exactly = 1) { cameraView.loadPicture(capture(captorString)) }
    assertThat(captorString.captured, equalTo(path))
    verify(exactly = 1) { cameraView.showDefaultPicture() }

Oblivious captorString allow catch the takePicture object

Same way we check that it is the same on having been loaded for viewPicture

But if remove that pic, just verificate whether the presenter is displaying a default picture by using showDefaultPicture method.

A second example, need testing save/load pic from our SharePreference class, creating saveload unit test:

fun saveLoad() {
    val toSave = "save"
    verify { shareView.clearText() }
    verify { shareView.reLoadList() }
    verify { shareView.loadText(capture(captorString)) }
    assertThat(toSave, equalTo(captorString.captured))

4. Run JunitTestCases

First, we validate that our object value stores and if could recover, and second we validate that in our survey, this one obtaining the values storing in `repository` memory, notice that not exist a direct reference in our code of unit test

When all test cases had been coded, Go run:

5. License

by Romell Domínguez

Copyright 2018 Romell D.Z.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,


See the License for the specific language governing permissions and limitations under the License.