Reputation: 343
I have created a custom Elastic Search Client. I need to deploy unit testing on the various functions. How should I do that?
Say for example , in JUnit I use these ->
assertEquals(expectedOutput, actualOutput);
The problem here is that there is actually no return value. The queries execute and manipulate things. I want to unit test the various functions.
For example, I want to see if the index() works properly. I don't really want to actually index data and check if it has been indexed as I am not doing the integration testing right now. Can I do just unit testing?
Following is a method from my client. How should I deploy unit testing here?
@Override
public ActionFuture<IndexResponse> index(IndexRequest request)
{
TimerContext indexTimerContext=indexTimer.time();
super.index(request));
indexTimerContext.stop();
}
How should I really go about doing it?
Upvotes: 2
Views: 8863
Reputation: 688
ElasticSearch has its own Test Framework and ( assertion )
tutorial describes unit and integration testing!
If you want to use other framework you could use mockito or easymock. In your case you have to check that method index(IndexRequest request)
calls indexTimer.time()
and
indexTimerContext.stop()
therefore you must mock indexTimer
and
verify call. look at Java verify void method calls n times with Mockito
EDIT I have never work with ElasticSearch, but your unit test will look like this
@Test
public void testIndex() {
clientTest = Mockito.spy(new MyClient());
//this is a field, which we mock
IndexTimer indexTimerSpy = Mockito.spy(new IndexTimer());
//TimerContext we mock too
TimerContext timerContextSpy = Mockito.spy(new TimerContext());
//IndexTimer.time returns our timer context
Mockito.doReturn(timerContextSpy).when(indexTimerSpy).time();
//set indexTimer
clientTest.setIndexTimer(indexTimerSpy);
//calls method under test
clientTest.index(null);
//verify calls of methods
Mockito.verify(indexTimerSpy).time();
Mockito.verify(timerContextSpy).stop();
// Prevent/stub logic when calling super.index(request)
//Mockito.doNothing().when((YourSuperclass)clientTest).index();
}
Upvotes: 2