From ae2576922c6294d24ff7af4028c90142f4337601 Mon Sep 17 00:00:00 2001 From: fabioCollini Date: Wed, 29 May 2019 09:14:21 +0200 Subject: [PATCH] Fix tests --- app/build.gradle | 1 + .../android/sample/ClearStoreMemoryTest.kt | 70 ++++++------- .../nytimes/android/sample/ClearStoreTest.kt | 98 +++++++------------ 3 files changed, 65 insertions(+), 104 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 6511021..05483dd 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -43,6 +43,7 @@ dependencies { testImplementation libraries.mockito testImplementation libraries.assertJ testImplementation libraries.junit + testImplementation libraries.coroutinesTest testCompileOnly libraries.jsr305 implementation libraries.supportRecyclerView implementation libraries.supportAppCompat diff --git a/app/src/test/java/com/nytimes/android/sample/ClearStoreMemoryTest.kt b/app/src/test/java/com/nytimes/android/sample/ClearStoreMemoryTest.kt index 792cb57..15262e0 100644 --- a/app/src/test/java/com/nytimes/android/sample/ClearStoreMemoryTest.kt +++ b/app/src/test/java/com/nytimes/android/sample/ClearStoreMemoryTest.kt @@ -2,62 +2,50 @@ package com.nytimes.android.sample import com.nytimes.android.external.store3.base.Fetcher import com.nytimes.android.external.store3.base.impl.BarCode -import com.nytimes.android.external.store3.base.impl.Store import com.nytimes.android.external.store3.base.impl.StoreBuilder import kotlinx.coroutines.runBlocking import org.assertj.core.api.Assertions.assertThat -import org.junit.Before import org.junit.Test import java.util.concurrent.atomic.AtomicInteger class ClearStoreMemoryTest { - lateinit var networkCalls: AtomicInteger - lateinit var store: Store + private val networkCalls = AtomicInteger(0) + private val store = StoreBuilder.barcode() + .fetcher(object : Fetcher { + override suspend fun fetch(key: BarCode) = networkCalls.incrementAndGet() - @Before - fun setUp() { - networkCalls = AtomicInteger(0) - store = StoreBuilder.barcode() - .fetcher(object : Fetcher { - override suspend fun fetch(key: BarCode) = networkCalls.incrementAndGet() + }) + .open() - }) - .open() + @Test + fun testClearSingleBarCode() = runBlocking { + //one request should produce one call + val barcode = BarCode("type", "key") + val result = store.get(barcode) + assertThat(networkCalls.get()).isEqualTo(1) + + store.clear(barcode) + val anotherResult = store.get(barcode) + assertThat(networkCalls.get()).isEqualTo(2) } @Test - fun testClearSingleBarCode() { - runBlocking{ - //one request should produce one call - val barcode = BarCode("type", "key") - val result = store.get(barcode) - assertThat(networkCalls.get()).isEqualTo(1) + fun testClearAllBarCodes() = runBlocking { + val b1 = BarCode("type1", "key1") + val b2 = BarCode("type2", "key2") - store.clearMemory(barcode) - val anotherResult = store.get(barcode) - assertThat(networkCalls.get()).isEqualTo(2) - } - } + //each request should produce one call + store.get(b1) + store.get(b2) + assertThat(networkCalls.get()).isEqualTo(2) - @Test - fun testClearAllBarCodes() { - runBlocking { + store.clear(b1) + store.clear(b2) - val b1 = BarCode("type1", "key1") - val b2 = BarCode("type2", "key2") - - //each request should produce one call - store.get(b1) - store.get(b2) - assertThat(networkCalls.get()).isEqualTo(2) - - store.clearMemory() - - //after everything is cleared each request should produce another 2 calls - store.get(b1) - store.get(b2) - assertThat(networkCalls.get()).isEqualTo(4) - } + //after everything is cleared each request should produce another 2 calls + store.get(b1) + store.get(b2) + assertThat(networkCalls.get()).isEqualTo(4) } } diff --git a/app/src/test/java/com/nytimes/android/sample/ClearStoreTest.kt b/app/src/test/java/com/nytimes/android/sample/ClearStoreTest.kt index fb67e2c..eca0e26 100644 --- a/app/src/test/java/com/nytimes/android/sample/ClearStoreTest.kt +++ b/app/src/test/java/com/nytimes/android/sample/ClearStoreTest.kt @@ -4,11 +4,9 @@ import com.nytimes.android.external.store3.base.Clearable import com.nytimes.android.external.store3.base.Fetcher import com.nytimes.android.external.store3.base.Persister import com.nytimes.android.external.store3.base.impl.BarCode -import com.nytimes.android.external.store3.base.impl.Store import com.nytimes.android.external.store3.base.impl.StoreBuilder import kotlinx.coroutines.runBlocking import org.assertj.core.api.Assertions.assertThat -import org.junit.Before import org.junit.Test import org.junit.runner.RunWith import org.mockito.runners.MockitoJUnitRunner @@ -21,81 +19,55 @@ val barcode2 = BarCode("type2", "key2") @RunWith(MockitoJUnitRunner::class) class ClearStoreTest { - val persister = ClearingPersister() - lateinit var networkCalls: AtomicInteger - lateinit var store: Store + private val persister = ClearingPersister() + private val networkCalls = AtomicInteger(0) + private val store= StoreBuilder.barcode() + .fetcher(object : Fetcher { + override suspend fun fetch(key: BarCode): Int { + return networkCalls.incrementAndGet() + } - @Before - fun setUp() { - networkCalls = AtomicInteger(0) - store = StoreBuilder.barcode() - .fetcher(object : Fetcher { - override suspend fun fetch(key: BarCode): Int { - return networkCalls.incrementAndGet() - } + }) + .persister(persister) + .open() - }) - .persister(persister) - .open() + @Test + fun testClearSingleBarCode() = runBlocking { + // one request should produce one call + val barcode = BarCode("type", "key") + store.get(barcode) + assertThat(networkCalls.toInt()).isEqualTo(1) + + // after clearing the memory another call should be made + store.clear(barcode) + store.get(barcode) + assertThat(persister.isClear).isTrue() + assertThat(networkCalls.toInt()).isEqualTo(2) } @Test - fun testClearSingleBarCode() { - runBlocking { - // one request should produce one call - val barcode = BarCode("type", "key") - - store.get(barcode) - assertThat(networkCalls.toInt()).isEqualTo(1) - - // after clearing the memory another call should be made - store.clear(barcode) - store.get(barcode) - assertThat(persister.isClear).isTrue() - assertThat(networkCalls.toInt()).isEqualTo(2) - } - } - - - @Test - fun testClearAllBarCodes() { - - runBlocking { - // each request should produce one call - val result = store.get(barcode1) - store.get(barcode2) - assertThat(networkCalls.toInt()).isEqualTo(2) - - store.clear() - - // after everything is cleared each request should produce another 2 calls - store.get(barcode1) - store.get(barcode2) - assertThat(networkCalls.toInt()).isEqualTo(4) - } + fun testClearAllBarCodes() = runBlocking { + // each request should produce one call + val result = store.get(barcode1) + store.get(barcode2) + assertThat(networkCalls.toInt()).isEqualTo(2) + store.clear(barcode1) + store.clear(barcode2) + // after everything is cleared each request should produce another 2 calls + store.get(barcode1) + store.get(barcode2) + assertThat(networkCalls.toInt()).isEqualTo(4) } } //everything will be mocked class ClearingPersister : Persister, Clearable { var isClear = false - val barcode1Responses = LinkedList() - val barcode2Responses = LinkedList() - - init { - barcode1Responses.add(null) - barcode1Responses.add(1) - barcode1Responses.add(null) - barcode1Responses.add(1) - - barcode2Responses.add(null) - barcode2Responses.add(1) - barcode2Responses.add(null) - barcode2Responses.add(1) - } + private val barcode1Responses = LinkedList(mutableListOf(null, 1, null, 1)) + private val barcode2Responses = LinkedList(mutableListOf(null, 1, null, 1)) override fun clear(key: BarCode) { isClear = true