Fix some tests

This commit is contained in:
fabioCollini 2019-05-21 21:32:54 +02:00
parent fc77dd153c
commit 015140fbf0
4 changed files with 16 additions and 33 deletions

View file

@ -1,41 +1,28 @@
package com.nytimes.android.sample 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.BarCode
import com.nytimes.android.external.store3.base.impl.Store import com.nytimes.android.external.store3.base.impl.Store
import com.nytimes.android.external.store3.base.impl.StoreBuilder import com.nytimes.android.external.store3.base.impl.StoreBuilder
import junit.framework.Assert.assertEquals import junit.framework.Assert.assertEquals
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import org.junit.Before
import org.junit.Test import org.junit.Test
import java.util.concurrent.atomic.AtomicInteger import java.util.concurrent.atomic.AtomicInteger
class StoreIntegrationTest { class StoreIntegrationTest {
lateinit var store: Store<String, BarCode> private val atomicInt = AtomicInteger(0)
val atomicInt = AtomicInteger(0)
@Before private val store: Store<String, BarCode> = StoreBuilder.barcode<String>()
@Throws(Exception::class) .fetcher { atomicInt.incrementAndGet().toString() }
fun setUp() { .open()
store = StoreBuilder.barcode<String>()
.fetcher(fetcher = object : Fetcher<String, BarCode> {
override suspend fun fetch(key: BarCode): String {
return atomicInt.incrementAndGet().toString()
}
})
.open()
}
@Test @Test
@Throws(Exception::class)
fun testRepeatedGet() { fun testRepeatedGet() {
val barcode = BarCode.empty() val barcode = BarCode.empty()
runBlocking { runBlocking {
val first = store.get(barcode) val first = store.fresh(barcode)
val same = store.fresh(barcode) val same = store.get(barcode)
assertEquals(first, same) assertEquals(first, same)
} }
} }
} }

View file

@ -33,7 +33,7 @@ class SourceDiskDaoStoreTest {
.open() .open()
val foo = Foo() val foo = Foo()
foo.bar = barCode.getKey() foo.bar = barCode.key
val sourceData = Gson().toJson(foo) val sourceData = Gson().toJson(foo)
val source = source(sourceData) val source = source(sourceData)

View file

@ -31,7 +31,7 @@ class StoreNetworkBeforeStaleTest {
@Test @Test
fun networkBeforeDiskWhenStale() = runBlocking<Unit> { fun networkBeforeDiskWhenStale() = runBlocking<Unit> {
whenever(fetcher.fetch(barCode)) whenever(fetcher.fetch(barCode))
.thenThrow(Exception()) .thenThrow(RuntimeException())
whenever(persister.read(barCode)) whenever(persister.read(barCode))
.thenReturn(disk1) //get should return from disk .thenReturn(disk1) //get should return from disk
whenever(persister.getRecordState(barCode)).thenReturn(RecordState.STALE) whenever(persister.getRecordState(barCode)).thenReturn(RecordState.STALE)

View file

@ -6,18 +6,14 @@ import com.nytimes.android.external.store3.base.impl.StoreBuilder
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.fail import org.assertj.core.api.Assertions.fail
import org.junit.Before
import org.junit.Test import org.junit.Test
class NoNetworkTest { class NoNetworkTest {
private lateinit var store: Store<Any, BarCode> private val store: Store<Any, BarCode> = StoreBuilder.barcode<Any>()
.fetcher {
@Before throw EXCEPTION
fun setUp() { }
store = StoreBuilder.barcode<Any>() .open()
.fetcher { throw EXCEPTION }
.open()
}
@Test @Test
fun testNoNetwork() = runBlocking<Unit> { fun testNoNetwork() = runBlocking<Unit> {
@ -25,7 +21,7 @@ class NoNetworkTest {
store.get(BarCode("test", "test")) store.get(BarCode("test", "test"))
fail("Exception not thrown") fail("Exception not thrown")
} catch (e: Exception) { } catch (e: Exception) {
assertThat(e).isEqualTo(EXCEPTION) assertThat(e.message).isEqualTo(EXCEPTION.message)
} }
} }
@ -38,6 +34,6 @@ class NoNetworkTest {
// } // }
companion object { companion object {
private val EXCEPTION = RuntimeException() private val EXCEPTION = RuntimeException("abc")
} }
} }