Hello i am having difficulties in this particular unit test testing the Repository of my app there is a function that returns void.I think i have to do Mockito.when to define the behaviour of a mocked class.
This is my class.
package com.example.moviesdatabase.data.repositories.upcomingrepository
import com.example.moviesdatabase.data.localDatasource.upcomingLocalDatasource.UpcominglocalDatasource
import com.example.moviesdatabase.data.mocks.ModelMocks
import com.example.moviesdatabase.data.remoteDatasource.upcomingRemoteDatasource.UpcomingRemoteDatasource
import kotlinx.coroutines.runBlocking
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.junit.MockitoJUnitRunner
@RunWith(MockitoJUnitRunner::class)
class UpcomingMoviesRepositoryImplTest{
@Mock
private lateinit var upcomingLocalDatasource: UpcominglocalDatasource
@Mock
private lateinit var upcomingRemoteDatasource: UpcomingRemoteDatasource
private val upcomingRepository: UpcomingMovieRepository by lazy {
UpcomingMoviesRepositoryImpl(upcomingLocalDatasource,upcomingRemoteDatasource)
}
@Test
fun `when there is no movies on the database and the table is empty`() {
runBlocking {
//Given
val upcomingListMovieTable = ModelMocks.getListUpcomingTable()
Mockito.`when`(upcomingLocalDatasource.getUpcomingDatabaseMovies()).thenReturn(emptyList())
Mockito.`when`(upcomingLocalDatasource.insertUpcomingDbMovies(upcomingListMovieTable))
val result = upcomingRepository.getUpcomingMoviesRepo()
assert(result == upcomingListMovieTable)
}
}
}
I have been looking al over the internet on how to work around this. since upcomingLocalDatasource is a @Mock i will need to tell what to return if a method is called . But that is a method that receives a list as a parameter and inserts it to the database. The error shows...
java.lang.NullPointerException
at com.example.moviesdatabase.data.repositories.upcomingrepository.UpcomingMoviesRepositoryImpl.getUpcomingMoviesRepo(UpcomingMoviesRepositoryImpl.kt:17)
at com.example.moviesdatabase.data.repositories.upcomingrepository.UpcomingMoviesRepositoryImplTest$when there is no movies on the database and the table is empty$1.invokeSuspend(UpcomingMoviesRepositoryImplTest.kt:45)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:284)
at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:85)
at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:59)
at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:38)
at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
at com.example.moviesdatabase.data.repositories.upcomingrepository.UpcomingMoviesRepositoryImplTest.when there is no movies on the database and the table is empty(UpcomingMoviesRepositoryImplTest.kt:38)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
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.mockito.internal.runners.DefaultInternalRunner$1$1.evaluate(DefaultInternalRunner.java:55)