collegevur.blogg.se

Mockito with kotlin
Mockito with kotlin







mockito with kotlin

If the method returns simple fixed value we may not see any performance difference. In case of thenAnswer(), every time we call mocked method getTime(), now() method is also called and returns the new value. This value is used in thenReturn() method every time getTime() function is called. In case of thenRetun() the value is calculated from now() method i.e. In the above example, we can see that getTime() method returns different values in case of thenAnswer() but same value in case of thenRetun(). Let’s assume we need to return string value “Mockito” on getName() method call.

mockito with kotlin

If you need to perform some operation or the value need to be computed at run time then we should use thenAnswer(…) Let’s look at the examples The simplest answer is – if you need fixed return value on method call then we should use thenReturn(…). When we should use thenReturn and when thenAnswer? Most of the times thenReturn() is used(which is fine) but some times we need to use thenAnswer(). The most common ways are using thenReturn() and thenAnswer() methods. There are different ways to specify mocked behavior. In Mockito insead of calling real method you can specify what to return or do when the method is called. If not you can visit official mockito website before we proceed. Let us assume you already know about mockito. Mockito is one of the most famous mocking framework for testing. Then we face the question, how to use thenReturn() and thenAnswer() methods of Mockito? If you are facing this also, don’t worry every one faces or has faced this question and we are here to solve it!! So, get set go… When it comes to writing test cases for code, Mockito is one of the most common choices of developers.









Mockito with kotlin