Skip to content

Commit

Permalink
test(mockito): convert mockito tests to kotlinmockito
Browse files Browse the repository at this point in the history
SUITEDEV-28852

Co-authored-by: davidSchuppa <[email protected]>
Co-authored-by: LasOri <[email protected]>
Co-authored-by: LordAndras <[email protected]>
  • Loading branch information
4 people committed Aug 24, 2021
1 parent 65e331b commit 811afc0
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,13 @@ import com.emarsys.testUtil.DatabaseTestUtils
import com.emarsys.testUtil.InstrumentationRegistry
import com.emarsys.testUtil.TimeoutUtils
import com.emarsys.testUtil.mockito.anyNotNull
import com.emarsys.testUtil.mockito.whenever
import io.kotlintest.shouldBe
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TestRule
import org.mockito.ArgumentMatchers.anyBoolean
import org.mockito.Mockito
import org.mockito.Mockito.mock
import org.mockito.Mockito.verify
import org.mockito.kotlin.any
import org.mockito.kotlin.eq
import org.mockito.kotlin.*
import java.util.*

class AbstractSqliteRepositoryTest {
Expand Down Expand Up @@ -75,14 +70,15 @@ class AbstractSqliteRepositoryTest {
LIMIT
)

dbMock = mock(CoreSQLiteDatabase::class.java)
dbMock = mock()

dbHelperMock = mock(DbHelper::class.java).apply {
whenever(readableCoreDatabase).thenReturn(dbMock)
whenever(writableCoreDatabase).thenReturn(dbMock)
dbHelperMock = mock {
on { readableCoreDatabase } doReturn dbMock
on { writableCoreDatabase } doReturn dbMock
}

repository = (mock(AbstractSqliteRepository::class.java, Mockito.CALLS_REAL_METHODS) as AbstractSqliteRepository<Any>).apply {

repository = (mock(defaultAnswer = Mockito.CALLS_REAL_METHODS) as AbstractSqliteRepository<Any>).apply {
tableName = TABLE_NAME
dbHelper = dbHelperMock
}
Expand All @@ -93,9 +89,9 @@ class AbstractSqliteRepositoryTest {
val contentValues = ContentValues().apply {
put("key", "value")
}
whenever(repository.contentValuesFromItem(anyNotNull())).thenReturn(contentValues)
whenever(repository.contentValuesFromItem(anyOrNull())).thenReturn(contentValues)

val input = anyNotNull<Any>()
val input = any<Any>()

repository.add(input)

Expand Down Expand Up @@ -127,11 +123,10 @@ class AbstractSqliteRepositoryTest {

@Test
fun testQuery_shouldReturnCorrectResult() {
val cursor = mock(Cursor::class.java).apply {
whenever(moveToFirst()).thenReturn(true)
whenever(isAfterLast).thenReturn(false, false, false, true)
val cursor = mock<Cursor> {
on { moveToFirst() } doReturn true
}

whenever(cursor.isAfterLast).thenReturn(false, false, false, true)
whenever(dbMock.query(
DISTINCT,
TABLE_NAME,
Expand Down Expand Up @@ -167,8 +162,8 @@ class AbstractSqliteRepositoryTest {

@Test
fun testQuery_shouldReturnCorrectResult_whenCursorIsEmpty() {
val cursor = mock(Cursor::class.java).apply {
whenever(moveToFirst()).thenReturn(false)
val cursor = mock<Cursor> {
on { moveToFirst() } doReturn false
}

whenever(dbMock.query(
Expand Down Expand Up @@ -233,10 +228,10 @@ class AbstractSqliteRepositoryTest {

@Test
fun testCursor_shouldBeClosed_afterException() {
val mockCursor = mock(Cursor::class.java).apply {
whenever(moveToFirst()).thenThrow(RuntimeException("TestException"))
val mockCursor = mock<Cursor> {
on { moveToFirst() } doThrow RuntimeException("TestException")
}
whenever(dbMock.query(anyBoolean(), anyNotNull(), anyNotNull(), anyNotNull(), anyNotNull(), anyNotNull(), anyNotNull(), anyNotNull(), anyNotNull())).thenReturn(mockCursor)
whenever(dbMock.query(any(), any(), any(), any(), any(), any(), any(), any(), any())).thenReturn(mockCursor)
try {
repository.query(dummySpecification)
} catch (ignored: RuntimeException) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.emarsys.core.response

import com.emarsys.testUtil.TimeoutUtils.timeoutRule
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TestRule
import org.mockito.kotlin.*

class AbstractResponseHandlerTest {
private lateinit var abstractResponseHandler: AbstractResponseHandler

@Rule
@JvmField
var timeout: TestRule = timeoutRule

@Before
fun init() {
abstractResponseHandler = mock()
}

@Test
fun testProcessResponse_shouldCallHandleResponse_whenResponseShouldBeHandled() {
whenever(abstractResponseHandler.shouldHandleResponse(any())).doReturn(true)

val responseModel = ResponseModel.Builder()
.statusCode(200)
.message("OK")
.requestModel(mock())
.build()

abstractResponseHandler.processResponse(responseModel)
verify(abstractResponseHandler).handleResponse(responseModel)
}

@Test
fun testProcessResponse_shouldNotCallHandleResponse_whenResponseShouldNotBeHandled() {
whenever(abstractResponseHandler.shouldHandleResponse(any())).doReturn(false)

val responseModel = ResponseModel.Builder()
.statusCode(200)
.message("OK")
.requestModel(mock())
.build()
abstractResponseHandler.processResponse(responseModel)
verify(abstractResponseHandler, times(0)).handleResponse(responseModel)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TestRule
import org.mockito.Mockito
import org.mockito.Mockito.*
import org.mockito.Mockito.CALLS_REAL_METHODS
import org.mockito.kotlin.mock
import org.mockito.kotlin.times
import org.mockito.kotlin.verify

class AbstractStorageTest {
private companion object {
const val VALUE = "value"
}

private lateinit var sharedPreferences: SharedPreferences
private lateinit var storage: AbstractStorage<String, SharedPreferences>
private lateinit var mockSharedPreferences: SharedPreferences
private lateinit var mockStorage: AbstractStorage<String, SharedPreferences>

@Rule
@JvmField
Expand All @@ -27,10 +29,9 @@ class AbstractStorageTest {
@Before
@Suppress("UNCHECKED_CAST")
fun setUp() {
sharedPreferences = mock(SharedPreferences::class.java)
storage = (mock(AbstractStorage::class.java, Mockito.CALLS_REAL_METHODS) as AbstractStorage<String, SharedPreferences>).apply {
ReflectionTestUtils.setInstanceField(this, "store", sharedPreferences)
}
mockSharedPreferences = mock()
mockStorage = (mock(defaultAnswer = CALLS_REAL_METHODS) as AbstractStorage<String, SharedPreferences>)
ReflectionTestUtils.setInstanceField(mockStorage, "store", mockSharedPreferences)
}

@Test(expected = java.lang.IllegalArgumentException::class)
Expand All @@ -46,62 +47,62 @@ class AbstractStorageTest {

@Test
fun testGet_shouldReturnValueFromMemory() {
storage.set(VALUE)
mockStorage.set(VALUE)

val result = storage.get()
val result = mockStorage.get()

result shouldBe VALUE
verify(storage, times(0)).readPersistedValue(sharedPreferences)
verify(mockStorage, times(0)).readPersistedValue(mockSharedPreferences)
}

@Test
fun testSet_shouldPersistValue() {
storage.set(VALUE)
mockStorage.set(VALUE)

verify(storage).persistValue(sharedPreferences, VALUE)
verify(mockStorage).persistValue(mockSharedPreferences, VALUE)
}

@Test
fun testGet_shouldReturnPersistedValueWhenImMemoryNotExists() {
val persistedValue = "persisted"
whenever(storage.readPersistedValue(sharedPreferences)).thenReturn(persistedValue)
whenever(mockStorage.readPersistedValue(mockSharedPreferences)).thenReturn(persistedValue)

storage.remove()
mockStorage.remove()

val result = storage.get()
val result = mockStorage.get()

result shouldBe persistedValue
}

@Test
fun testGet_shouldCachePersistedValue_inMemory_whenNull() {
val expected = "persistedAndStoredInMemory"
whenever(storage.readPersistedValue(sharedPreferences)).thenReturn(expected, null)
whenever(mockStorage.readPersistedValue(mockSharedPreferences)).thenReturn(expected, null)

storage.remove()
mockStorage.remove()

storage.get()
mockStorage.get()

val result = storage.get()
val result = mockStorage.get()
result shouldBe expected
}

@Test
fun testRemove_shouldRemoveInMemoryValue() {
storage.set(VALUE)
mockStorage.set(VALUE)

storage.remove()
mockStorage.remove()

val inMemoryValue = ReflectionTestUtils.getInstanceField<String>(storage, "value")
val inMemoryValue = ReflectionTestUtils.getInstanceField<String>(mockStorage, "value")

inMemoryValue shouldBe null
}

@Test
fun testRemove_shouldRemovePersistedValue() {
storage.remove()
mockStorage.remove()

verify(storage).removePersistedValue(sharedPreferences)
verify(mockStorage).removePersistedValue(mockSharedPreferences)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.emarsys.core.response

import com.emarsys.core.util.Assert

abstract class AbstractResponseHandler {

fun processResponse(responseModel: ResponseModel) {
Assert.notNull(responseModel, "ResponseModel must not be null")
if (shouldHandleResponse(responseModel)) {
handleResponse(responseModel)
}
Expand Down

0 comments on commit 811afc0

Please sign in to comment.