Created
September 1, 2020 12:05
-
-
Save harmittaa/a7a06c2ef55788228ee031dcac2a19b0 to your computer and use it in GitHub Desktop.
With mockito
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.dropbox.android.external.store4.Fetcher | |
import com.dropbox.android.external.store4.Store | |
import com.dropbox.android.external.store4.StoreBuilder | |
import com.dropbox.android.external.store4.fresh | |
import com.dropbox.android.sample.Graph | |
import com.dropbox.android.sample.data.model.Post | |
import com.nhaarman.mockitokotlin2.any | |
import com.nhaarman.mockitokotlin2.mock | |
import com.nhaarman.mockitokotlin2.whenever | |
import kotlinx.coroutines.ExperimentalCoroutinesApi | |
import kotlinx.coroutines.FlowPreview | |
import kotlinx.coroutines.test.TestCoroutineScope | |
import kotlinx.coroutines.test.runBlockingTest | |
import org.junit.Before | |
import org.junit.Test | |
import org.junit.runner.RunWith | |
import org.junit.runners.JUnit4 | |
import kotlin.time.ExperimentalTime | |
@OptIn( | |
FlowPreview::class, | |
ExperimentalCoroutinesApi::class, | |
ExperimentalTime::class, | |
ExperimentalStdlibApi::class | |
) | |
open class StoreProvider() { | |
open fun getStore() = | |
StoreBuilder.from(Fetcher.of { key: String -> | |
Graph.provideRetrofit().fetchSubreddit(key, 10).data.children.map(Graph::toPosts) | |
}).build() | |
} | |
class MyTestClass(val provider: StoreProvider) { | |
suspend fun testFetching(): List<Post> { | |
val store = provider.getStore() | |
return store.fresh("asd") | |
} | |
} | |
@ExperimentalCoroutinesApi | |
@FlowPreview | |
@RunWith(JUnit4::class) | |
class MyTestClassTest { | |
private val testScope = TestCoroutineScope() | |
private lateinit var storeProvider: StoreProvider | |
private lateinit var store: Store<String, List<Post>> | |
private lateinit var testClass: MyTestClass | |
@Before | |
fun setUp() { | |
storeProvider = mock() | |
store = mock() | |
whenever(storeProvider.getStore()).thenReturn(store) | |
testClass = MyTestClass(storeProvider) | |
} | |
@Test | |
fun failingTest() = testScope.runBlockingTest { | |
whenever(store.fresh(any())).thenReturn(mock()) | |
val result = testClass.testFetching() | |
} | |
} | |
/* | |
Crashes: | |
java.lang.NullPointerException | |
at com.dropbox.android.external.store4.StoreKt$fresh$$inlined$filterNot$1.collect(SafeCollector.common.kt:114) | |
at kotlinx.coroutines.flow.FlowKt__ReduceKt.first(Reduce.kt:163) | |
at kotlinx.coroutines.flow.FlowKt.first(Unknown Source) | |
at com.dropbox.android.external.store4.StoreKt.fresh(Store.kt:84) | |
at com.dropbox.android.sample.ui.reddit.MyTestClassTest$failingTest$1.invokeSuspend(ExampleTest.kt:70) | |
at com.dropbox.android.sample.ui.reddit.MyTestClassTest$failingTest$1.invoke(ExampleTest.kt) | |
at kotlinx.coroutines.test.TestBuildersKt$runBlockingTest$deferred$1.invokeSuspend(TestBuilders.kt:50) | |
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) | |
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56) | |
at kotlinx.coroutines.test.TestCoroutineDispatcher.dispatch(TestCoroutineDispatcher.kt:50) | |
at kotlinx.coroutines.DispatchedContinuationKt.resumeCancellableWith(DispatchedContinuation.kt:288) | |
at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:26) | |
at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:109) | |
at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:158) | |
at kotlinx.coroutines.BuildersKt__Builders_commonKt.async(Builders.common.kt:91) | |
at kotlinx.coroutines.BuildersKt.async(Unknown Source) | |
at kotlinx.coroutines.BuildersKt__Builders_commonKt.async$default(Builders.common.kt:84) | |
at kotlinx.coroutines.BuildersKt.async$default(Unknown Source) | |
at kotlinx.coroutines.test.TestBuildersKt.runBlockingTest(TestBuilders.kt:49) | |
at kotlinx.coroutines.test.TestBuildersKt.runBlockingTest(TestBuilders.kt:73) | |
at com.dropbox.android.sample.ui.reddit.MyTestClassTest.failingTest(ExampleTest.kt:69) | |
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) | |
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) | |
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) | |
at java.lang.reflect.Method.invoke(Method.java:498) | |
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59) | |
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) | |
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56) | |
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) | |
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) | |
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) | |
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100) | |
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366) | |
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103) | |
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63) | |
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331) | |
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79) | |
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329) | |
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66) | |
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293) | |
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) | |
at org.junit.runners.ParentRunner.run(ParentRunner.java:413) | |
at org.junit.runner.JUnitCore.run(JUnitCore.java:137) | |
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) | |
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33) | |
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230) | |
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am facing the same issue, did you find the fix for the same?