You can use Mockito
to mock your jdbc
behavior. Let's say you want to test how you verticle behave when res
returns one row.
Mock your jdbc
:
jdbc = Mockito.mock(JDBCClient.class);
Then you have to mock a ResultSet
object:
ResultSet res = Mockito.mock(ResultSet.class);
Mockito.when(res.getNumRows()).thenReturn(1);
Then you have to mock the AsyncResult
object who is in charge to return res
:
AsyncResult<ResultSet> asyncResultResultSet = Mockito.mock(AsyncResult.class);
Mockito.when(asyncResultResultSet.succeeded()).thenReturn(true);
Mockito.when(asyncResultResultSet.result()).thenReturn(res);
Then you have to mock the SQLConnection
object who is in charge to return asyncResultResultSet
. Use Answer
to grab the handler and force it to return your mock:
SQLConnection sqlConnection = Mockito.mock(SQLConnection.class);
Mockito.doAnswer(new Answer<AsyncResult<ResultSet>>() {
@Override
public AsyncResult<ResultSet> answer(InvocationOnMock arg0) throws Throwable {
((Handler<AsyncResult<ResultSet>>) arg0.getArgument(2)).handle(asyncResultResultSet);
return null;
}
}).when(sqlConnection).queryWithParams(Mockito.any(), Mockito.any(), Mockito.any());
Then you have to mock the AsyncResult
object who is in charge to return the sqlConnection
. Again, Answer
helps:
AsyncResult<SQLConnection> sqlConnectionResult = Mockito.mock(AsyncResult.class);
Mockito.when(sqlConnectionResult.succeeded()).thenReturn(true);
Mockito.when(sqlConnectionResult.result()).thenReturn(sqlConnection);
Mockito.doAnswer(new Answer<AsyncResult<SQLConnection>>() {
@Override
public AsyncResult<SQLConnection> answer(InvocationOnMock arg0) throws Throwable {
((Handler<AsyncResult<SQLConnection>>) arg0.getArgument(0)).handle(sqlConnectionResult);
return null;
}
}).when(jdbc).getConnection(Mockito.any());
Here you are. It is a bunch of code but you can mock multiple ResultSet
object and use them with the asyncResultResultSet
by chaining multiple thenReturn
:
Mockito.when(asyncResultResultSet.result()).thenReturn(res1).thenReturn(res2) ...;
Try not to test vertx.io
.
Here is my complete solution if you want to find dependencies. I also use Powermock
with VertxUnit
to run my verticle.