Answer the question
In order to leave comments, you need to log in
How to mock a dynamic table DAO using Mockito?
Hello. I write unit tests for a service that actively works with other services. Other services mostly delegate calls to the DAO. Part of the DAO works with directories, part - with dynamic tables (in which data is constantly changing during the program's operation).
Question: how to mock such DAO/services working with dynamic tables? For mocks I use Mockito
.
I'm just writing a unit test for a method that calls the service twice with the same data (data request). But in the first case, an empty list should come in response, and in the second (after writing to the table) - a list with one element.
C when()
and thenReturn()
can't do it. So far, the idea is only to write your own mock class with a collection inside.
PS So far I have found that you can call twice thenReturn()
, but have not tried it yet. Yes, and I don’t really like this solution, since it rather depends not on how many times a data request is made, but on when the next time a request is sent to receive data, or, rather, after what (after what actions).
Answer the question
In order to leave comments, you need to log in
You can write your own Answer, it seems that this should solve the problem.
when(mockFoo.someMethod()).thenReturn(0, 1, -1);
The first call will return 0, the second 1, all others -1
UPD:
since it rather depends not on how many times a data request is made, but on when the next time a request is sent to receive data, or, rather, after that (after what actions)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question