From 811afc073a2ff7e9e5aa5efe48ef596a5af9e2ee Mon Sep 17 00:00:00 2001 From: zkovacs Date: Tue, 24 Aug 2021 13:53:58 +0200 Subject: [PATCH] test(mockito): convert mockito tests to kotlinmockito SUITEDEV-28852 Co-authored-by: davidSchuppa <32750715+davidSchuppa@users.noreply.github.com> Co-authored-by: LasOri <24588073+LasOri@users.noreply.github.com> Co-authored-by: LordAndras <49073629+LordAndras@users.noreply.github.com> --- .../AbstractSqliteRepositoryTest.kt | 39 +++++------- .../response/AbstractResponseHandlerTest.java | 62 ------------------- .../response/AbstractResponseHandlerTest.kt | 48 ++++++++++++++ .../core/storage/AbstractStorageTest.kt | 51 +++++++-------- .../core/response/AbstractResponseHandler.kt | 3 - 5 files changed, 91 insertions(+), 112 deletions(-) delete mode 100644 core/src/androidTest/java/com/emarsys/core/response/AbstractResponseHandlerTest.java create mode 100644 core/src/androidTest/java/com/emarsys/core/response/AbstractResponseHandlerTest.kt diff --git a/core/src/androidTest/java/com/emarsys/core/database/repository/AbstractSqliteRepositoryTest.kt b/core/src/androidTest/java/com/emarsys/core/database/repository/AbstractSqliteRepositoryTest.kt index b418ea819..f54f59bf3 100644 --- a/core/src/androidTest/java/com/emarsys/core/database/repository/AbstractSqliteRepositoryTest.kt +++ b/core/src/androidTest/java/com/emarsys/core/database/repository/AbstractSqliteRepositoryTest.kt @@ -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 { @@ -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).apply { + + repository = (mock(defaultAnswer = Mockito.CALLS_REAL_METHODS) as AbstractSqliteRepository).apply { tableName = TABLE_NAME dbHelper = dbHelperMock } @@ -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() + val input = any() repository.add(input) @@ -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 { + on { moveToFirst() } doReturn true } - + whenever(cursor.isAfterLast).thenReturn(false, false, false, true) whenever(dbMock.query( DISTINCT, TABLE_NAME, @@ -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 { + on { moveToFirst() } doReturn false } whenever(dbMock.query( @@ -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 { + 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) { diff --git a/core/src/androidTest/java/com/emarsys/core/response/AbstractResponseHandlerTest.java b/core/src/androidTest/java/com/emarsys/core/response/AbstractResponseHandlerTest.java deleted file mode 100644 index baa64d12d..000000000 --- a/core/src/androidTest/java/com/emarsys/core/response/AbstractResponseHandlerTest.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.emarsys.core.response; - -import com.emarsys.core.request.model.RequestModel; -import com.emarsys.testUtil.TimeoutUtils; - -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TestRule; -import org.mockito.Mockito; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -public class AbstractResponseHandlerTest { - - private AbstractResponseHandler abstractResponseHandler; - - @Rule - public TestRule timeout = TimeoutUtils.getTimeoutRule(); - - @Before - public void init() { - abstractResponseHandler = mock(AbstractResponseHandler.class, Mockito.CALLS_REAL_METHODS); - } - - @Test(expected = NullPointerException.class) - public void testProcessResponse_responseShouldNotBeNull() { - abstractResponseHandler.processResponse(null); - } - - @Test - public void testProcessResponse_shouldCallHandleResponse_whenResponseShouldBeHandled() { - when(abstractResponseHandler.shouldHandleResponse(any(ResponseModel.class))).thenReturn(true); - ResponseModel responseModel = new ResponseModel.Builder() - .statusCode(200) - .message("OK") - .requestModel(mock(RequestModel.class)) - .build(); - - abstractResponseHandler.processResponse(responseModel); - - verify(abstractResponseHandler).handleResponse(responseModel); - } - - @Test - public void testProcessResponse_shouldNotCallHandleResponse_whenResponseShouldNotBeHandled() { - when(abstractResponseHandler.shouldHandleResponse(any(ResponseModel.class))).thenReturn(false); - ResponseModel responseModel = new ResponseModel.Builder() - .statusCode(200) - .message("OK") - .requestModel(mock(RequestModel.class)) - .build(); - - abstractResponseHandler.processResponse(responseModel); - - verify(abstractResponseHandler, times(0)).handleResponse(responseModel); - } -} \ No newline at end of file diff --git a/core/src/androidTest/java/com/emarsys/core/response/AbstractResponseHandlerTest.kt b/core/src/androidTest/java/com/emarsys/core/response/AbstractResponseHandlerTest.kt new file mode 100644 index 000000000..237a5d022 --- /dev/null +++ b/core/src/androidTest/java/com/emarsys/core/response/AbstractResponseHandlerTest.kt @@ -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) + } +} \ No newline at end of file diff --git a/core/src/androidTest/java/com/emarsys/core/storage/AbstractStorageTest.kt b/core/src/androidTest/java/com/emarsys/core/storage/AbstractStorageTest.kt index 2bd95e1fb..1858f38d5 100644 --- a/core/src/androidTest/java/com/emarsys/core/storage/AbstractStorageTest.kt +++ b/core/src/androidTest/java/com/emarsys/core/storage/AbstractStorageTest.kt @@ -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 + private lateinit var mockSharedPreferences: SharedPreferences + private lateinit var mockStorage: AbstractStorage @Rule @JvmField @@ -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).apply { - ReflectionTestUtils.setInstanceField(this, "store", sharedPreferences) - } + mockSharedPreferences = mock() + mockStorage = (mock(defaultAnswer = CALLS_REAL_METHODS) as AbstractStorage) + ReflectionTestUtils.setInstanceField(mockStorage, "store", mockSharedPreferences) } @Test(expected = java.lang.IllegalArgumentException::class) @@ -46,29 +47,29 @@ 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 } @@ -76,32 +77,32 @@ class AbstractStorageTest { @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(storage, "value") + val inMemoryValue = ReflectionTestUtils.getInstanceField(mockStorage, "value") inMemoryValue shouldBe null } @Test fun testRemove_shouldRemovePersistedValue() { - storage.remove() + mockStorage.remove() - verify(storage).removePersistedValue(sharedPreferences) + verify(mockStorage).removePersistedValue(mockSharedPreferences) } } diff --git a/core/src/main/java/com/emarsys/core/response/AbstractResponseHandler.kt b/core/src/main/java/com/emarsys/core/response/AbstractResponseHandler.kt index 8384b6cf6..b997db16b 100644 --- a/core/src/main/java/com/emarsys/core/response/AbstractResponseHandler.kt +++ b/core/src/main/java/com/emarsys/core/response/AbstractResponseHandler.kt @@ -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) }