Unit Test: wanted but not invoked (Mockito)

149 views Asked by At

I'm development a unit test method using mockito, I already search about this error but anithing can solve this problem.

Here is my implementation:

@RunWith(MockitoJUnitRunner::class)
@ExperimentalCoroutinesApi
class ProductsViewModelTest : PresentationBaseTest() {

    @Mock
    private lateinit var getTransactions: GetTransactions

    @Mock
    private lateinit var getProductsByTransactions: GetProductsByTransactions

    private lateinit var productsViewModel: ProductsViewModel

    @Mock
    private lateinit var observer: Observer<List<Product>>

    @Mock
    private lateinit var observerLoading: EventObserver<Boolean>

    @Mock
    private lateinit var context: Context

    @Mock
    private lateinit var products: List<Product>

    @Before
    fun setup() {
        MockitoAnnotations.openMocks(this)
        productsViewModel =
            ProductsViewModel(dispatcher, context, getTransactions, getProductsByTransactions)
        productsViewModel.products.observeForever(observer)
        productsViewModel.onProductLoading.observeForever(observerLoading)
    }

    @Test
    fun get transactions should return transaction list rom use-case() =
        dispatcher.test.scheduler.run {
            // Arrange (Given)
            val transactions = FakePresentationData.getTransaction(10)
            products = FakePresentationData.getProducts(10)
            
            whenever(getTransactions.invoke(anyBoolean())).thenReturn(flowOf(transactions))
            whenever(getProductsByTransactions.groupProductsSkuByTransactions(transactions))
                .thenReturn(products)

            // Act (When)
            productsViewModel.fetchProducts()

            // Assert (Then)
            verify(observer).onChanged(products)
        }
}

All time I receive the message of 'Wanted but not invoked'. Anyone can help me with this?

0

There are 0 answers