Please note that this article does not describe how to use Mockito itself. Contains a lot of descriptions for Android.
If you use Mockito as it is in Kotlin, you will get the following code.
val sharedPreferences = mock(SharedPreferences::class.java)
val editor = mock(SharedPreferences.Editor::class.java, RETURNS_DEEP_STUBS)
`when`(sharedPreferences.edit()).thenReturn(editor)
`when`(editor.commit()).thenReturn(true)
`when`(editor.putString(anyString(), anyString())).thenReturn(editor)
The code above is a code to mock Android's SharedPreferences.
sharedPreferences.edit ()
returns a more mocked Editor, allowing you to use commit ()
and putString ()
.
The mock ()
method that creates the mock object is okay, but the part of the when ()
method that determines the behavior of the mock object is surrounded by backticks, which is very hard to see.
This is a phenomenon that occurs because the word when
is a reserved word in Kotlin and needs to be escaped by backticks.
Of course, there is no problem as it is, but this time it is a library that makes the code part of this mock more like Kotlin, [** mockito-kotlin **](https://github.com/nhaarman/mockito- I would like to use kotlin).
Add library references in each way, such as Gradle or Maven.
//testImplementation is adapted to the environment
testImplementation "com.nhaarman:mockito-kotlin:1.5.0"
Instead of passing the class as an argument, it will be specified by generics.
val sharedPreferences = mock<SharedPreferences> {}
The options passed in the second and subsequent arguments will continue to be described in the method arguments.
val editor = mock<SharedPreferences.Editor>(defaultAnswer = RETURNS_DEEP_STUBS) { }
All the contents of withSettings ()
can also be passed as arguments.
//The arguments that can be used are as follows(1.5.0 present)
extraInterfaces: Array<KClass<out Any>>? = null
name: String? = null
spiedInstance: Any? = null
defaultAnswer: Answer<Any>? = null
serializable: Boolean = false
serializableMode: SerializableMode? = null
verboseLogging: Boolean = false
invocationListeners: Array<InvocationListener>? = null
stubOnly: Boolean = false
useConstructor: Boolean = false
outerInstance: Any? = null
stubbing: KStubbing<T>.(T) -> Unit
Do not use when
, but describe it inside the function argument{}
when creating a mock.
ʻOn {method to mock ()} specifies the method to mock the behavior, followed by
doReturn,
doAnswer,
doThrow`.
The code below is equivalent to the Mockito code introduced at the beginning of the article.
val editor = mock<SharedPreferences.Editor>(defaultAnswer = RETURNS_DEEP_STUBS) {
on { commit() } doReturn true
on { putString(anyString(), anyString()) } doReturn it
}
val sharedPreferences = mock<SharedPreferences> {
on { edit() } doReturn editor
}
//Below is the code at the beginning of the article
val sharedPreferences = mock(SharedPreferences::class.java)
val editor = mock(SharedPreferences.Editor::class.java, RETURNS_DEEP_STUBS)
`when`(sharedPreferences.edit()).thenReturn(editor)
`when`(editor.commit()).thenReturn(true)
`when`(editor.putString(anyString(), anyString())).thenReturn(editor)
doReturn
The value written continuously will be the return value of the method to be mocked.
If the return value is void
or ʻUnit, there is no problem if you return ʻUnit
.
val stringMock = mock<String> {
on { toString() } doReturn "mocked"
}
stringMock.toString() // => "mocked"
doAnswer
Write a function that takes a mocked object as a receiver and returns the type that the method is supposed to return.
It's like let
.
val stringMock = mock<String> {
on { toString() } doAnswer { "mocked" + it.hashCode() }
}
stringMock.toString() // => "mocked[Numbers]"
doThrow
Throw the exception you wrote in succession.
val stringMock = mock<String> {
on { toString() } doThrow RuntimeException()
}
stringMock.toString() // => RuntimeException
In the part of specifying the argument of the method to mock, you can use ʻany () and ʻarg That ()
as well as Mockito, so I think that the basics will not be a problem.
For other writing methods, it is easy to understand by looking at the Wiki on Github and the test code, so if you do not know how to write it, you should look at it.
Wiki: https://github.com/nhaarman/mockito-kotlin/wiki/Mocking-and-verifying Test code: https://github.com/nhaarman/mockito-kotlin/tree/2.x/tests/src/test/kotlin/test
Recommended Posts