0

I'm trying to do some testing in my object kotlin class, but I can getting an error in my thenReturn method when I try to pass the object. I get a Require: Unit! Found checkout. Someone can point me how is possible to test it??

If I remove thenReturn method I get this error:

Hints:
 1. missing thenReturn()
 2. you are trying to stub a final method, which is not supported
 3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed

I refer this link but I can't get it.

object CheckoutRepository: CheckoutContract.Model {

    var checkout: MutableList<Checkout> = mutableListOf<Checkout>()

    override fun addProductToShoppingCart(checkoutProduct: Checkout){
        checkout.add(checkoutProduct)
    }

    override fun getProductsInShoppinCart() : List<Checkout>?{
        return checkout
    }

    override fun cleanCheckout(){
        checkout.clear()
    }
}



@Test
    fun test_with_mock() {
        val mock = mock<CheckoutContract.Model>()

        var checkout = Checkout("VOUCHER", "voucher", 35.0, 5)
        mock.addProductToShoppingCart(checkout)

        val answer = mock.getProductsInShoppinCart()
        `when`(mock.addProductToShoppingCart(checkout)).thenReturn(checkout)

        assertNotNull(checkout)
        assertEquals(checkout, answer)
    }
halfer
  • 19,824
  • 17
  • 99
  • 186
S.P.
  • 2,274
  • 4
  • 26
  • 57

1 Answers1

0

In your function addProductToShoppingCart don't have any return type.

if you want check Checkout class make the change in the functions then test case will pass

override fun addProductToShoppingCart(checkoutProduct: Checkout):Checkout{
    checkout.add(checkoutProduct)
return checkout
}
sasikumar
  • 12,540
  • 3
  • 28
  • 48
  • Yes, I don't need the return type. If I remove the thenReturn I get the error: missing thenReturn() Check my question. That I want to do is test the addProductToShoppingCart is working – S.P. Sep 03 '19 at 12:54