-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
260 additions
and
50 deletions.
There are no files selected for viewing
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
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
1 change: 0 additions & 1 deletion
1
KotlinMVVM/app/src/main/java/com/emedinaa/kotlinmvvm/data/OperationCallback.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
package com.emedinaa.kotlinmvvm.data | ||
|
||
interface OperationCallback { | ||
|
||
fun onSuccess(obj:Any?) | ||
fun onError(obj:Any?) | ||
} |
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
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
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
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
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
17 changes: 0 additions & 17 deletions
17
KotlinMVVM/app/src/test/java/com/emedinaa/kotlinmvvm/ExampleUnitTest.kt
This file was deleted.
Oops, something went wrong.
124 changes: 124 additions & 0 deletions
124
KotlinMVVM/app/src/test/java/com/emedinaa/kotlinmvvm/MVVMUnitTest.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package com.emedinaa.kotlinmvvm | ||
|
||
import android.app.Application | ||
import android.content.Context | ||
import androidx.arch.core.executor.testing.InstantTaskExecutorRule | ||
import androidx.lifecycle.Observer | ||
import com.emedinaa.kotlinmvvm.data.OperationCallback | ||
import com.emedinaa.kotlinmvvm.model.Museum | ||
import com.emedinaa.kotlinmvvm.model.MuseumDataSource | ||
import com.emedinaa.kotlinmvvm.viewmodel.MuseumViewModel | ||
import org.junit.Assert | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.mockito.ArgumentCaptor | ||
import org.mockito.Captor | ||
import org.mockito.Mock | ||
import org.mockito.* | ||
import org.mockito.Mockito.* | ||
|
||
class MVVMUnitTest { | ||
|
||
@Mock | ||
private lateinit var repository: MuseumDataSource | ||
@Mock private lateinit var context: Application | ||
|
||
@Captor | ||
private lateinit var operationCallbackCaptor: ArgumentCaptor<OperationCallback> | ||
|
||
private lateinit var viewModel:MuseumViewModel | ||
|
||
private lateinit var isViewLoadingObserver:Observer<Boolean> | ||
private lateinit var onMessageErrorObserver:Observer<Any> | ||
private lateinit var emptyListObserver:Observer<Boolean> | ||
private lateinit var onRenderMuseumsObserver:Observer<List<Museum>> | ||
|
||
private lateinit var museumEmptyList:List<Museum> | ||
private lateinit var museumList:List<Museum> | ||
|
||
/** | ||
//https://jeroenmols.com/blog/2019/01/17/livedatajunit5/ | ||
//Method getMainLooper in android.os.Looper not mocked | ||
java.lang.IllegalStateException: operationCallbackCaptor.capture() must not be null | ||
*/ | ||
@get:Rule | ||
val rule = InstantTaskExecutorRule() | ||
|
||
@Before | ||
fun setup() { | ||
MockitoAnnotations.initMocks(this) | ||
`when`<Context>(context.applicationContext).thenReturn(context) | ||
|
||
viewModel= MuseumViewModel(repository) | ||
|
||
mockData() | ||
setupObservers() | ||
} | ||
|
||
@Test | ||
fun museumEmptyListRepositoryAndViewModel(){ | ||
with(viewModel){ | ||
loadMuseums() | ||
isViewLoading.observeForever(isViewLoadingObserver) | ||
//onMessageError.observeForever(onMessageErrorObserver) | ||
isEmptyList.observeForever(emptyListObserver) | ||
museums.observeForever(onRenderMuseumsObserver) | ||
} | ||
|
||
verify(repository, times(1)).retrieveMuseums(capture(operationCallbackCaptor)) | ||
operationCallbackCaptor.value.onSuccess(museumEmptyList) | ||
|
||
Assert.assertNotNull(viewModel.isViewLoading.value) | ||
//Assert.assertNotNull(viewModel.onMessageError.value) //java.lang.AssertionError | ||
//Assert.assertNotNull(viewModel.isEmptyList.value) | ||
Assert.assertTrue(viewModel.isEmptyList.value==true) | ||
Assert.assertTrue(viewModel.museums.value?.size==0) | ||
} | ||
|
||
@Test | ||
fun museumListRepositoryAndViewModel(){ | ||
with(viewModel){ | ||
loadMuseums() | ||
isViewLoading.observeForever(isViewLoadingObserver) | ||
museums.observeForever(onRenderMuseumsObserver) | ||
} | ||
|
||
verify(repository, times(1)).retrieveMuseums(capture(operationCallbackCaptor)) | ||
operationCallbackCaptor.value.onSuccess(museumList) | ||
|
||
Assert.assertNotNull(viewModel.isViewLoading.value) | ||
Assert.assertTrue(viewModel.museums.value?.size==3) | ||
} | ||
|
||
@Test | ||
fun museumFailRepositoryAndViewModel(){ | ||
with(viewModel){ | ||
loadMuseums() | ||
isViewLoading.observeForever(isViewLoadingObserver) | ||
onMessageError.observeForever(onMessageErrorObserver) | ||
} | ||
verify(repository, times(1)).retrieveMuseums(capture(operationCallbackCaptor)) | ||
operationCallbackCaptor.value.onError("Ocurrió un error") | ||
Assert.assertNotNull(viewModel.isViewLoading.value) | ||
Assert.assertNotNull(viewModel.onMessageError.value) | ||
} | ||
|
||
private fun setupObservers(){ | ||
isViewLoadingObserver= mock(Observer::class.java) as Observer<Boolean> | ||
onMessageErrorObserver= mock(Observer::class.java) as Observer<Any> | ||
emptyListObserver= mock(Observer::class.java) as Observer<Boolean> | ||
onRenderMuseumsObserver= mock(Observer::class.java)as Observer<List<Museum>> | ||
} | ||
|
||
private fun mockData(){ | ||
museumEmptyList= emptyList() | ||
val mockList:MutableList<Museum> = mutableListOf() | ||
mockList.add(Museum(0,"Museo Nacional de Arqueología, Antropología e Historia del Perú","")) | ||
mockList.add(Museum(1,"Museo de Sitio Pachacamac","")) | ||
mockList.add(Museum(2,"Casa Museo José Carlos Mariátegui","")) | ||
|
||
museumList= mockList.toList() | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
KotlinMVVM/app/src/test/java/com/emedinaa/kotlinmvvm/MockitoKotlinHelper.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.emedinaa.kotlinmvvm | ||
|
||
import org.mockito.ArgumentCaptor | ||
|
||
/** | ||
* Returns ArgumentCaptor.capture() as nullable type to avoid java.lang.IllegalStateException | ||
* when null is returned. | ||
*/ | ||
fun <T> capture(argumentCaptor: ArgumentCaptor<T>): T = argumentCaptor.capture() |
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
Oops, something went wrong.