2

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?

Exalt
  • 343
  • 5
  • 15

1 Answers1

2

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();

}
Community
  • 1
  • 1
rpc1
  • 688
  • 10
  • 23
  • Should I mock indexTimer or indexTmerContext? the time() is returning a new instance of timerContext ["new TimerContext(this, clock)" ] – Exalt Jun 11 '15 at 09:13
  • You have to mock all objects indexTimer indexTimerContext and your class. I have edit my answer. I think my example is clear – rpc1 Jun 13 '15 at 11:45
  • The elasticsearch Test Frameworks is equal to nothing. Not a very useful thing. Where can I find proper examples? – Bob Aug 03 '16 at 10:39