Elasticsearch provides a jar document that is used to test the code related to elasticsearch. We can add this jar document to any Java IDE. Elasticsearch offers a framework that helps to perform a range of tests by utilizing the system. Basically, three types of testing are performed in elasticsearch to test the code, which is as follows -
One thing that needs to be kept in mind is that the newer versions of elasticsearch do not support the testing. It has now been deprecated by the elasticsearch community. But in this chapter, we will provide you the general guide of testing for the users of earlier version of elasticsearch.
To start performing the testing in Elasticsearch, some pre-requirements need to be setup. For this, we have to add the elasticsearch dependency to our program. For this purpose, we can use maven and add the following code to pom.xml file.
Initialize the EsSetup by creating its object to start and stop the Elasticsearch node. See the following code -
You can also create indices using esSetup.execute() function with createindex where setting, type, and data need to be specified.
Unit testing is basic testing, which is carried out by using elasticsearch test framework and JUnit. We can create node and indices by using Elasticsearch classes, which can be used in test method to perform the testing.
To perform unit testing, Elasticsearch Test Case and Elasticsearch Token Stream Case classes are used.
Integration testing takes place after unit testing. In elasticsearch, it is performed by using multiple nodes in a cluster. You need to use ESIntegTestCase class for this testing. Elasticsearch provides various methods, which help the users to prepare the test cases more easily. Following is a list of those methods -
Test Cluster Methods
Elasticsearch offers several test cluster methods, which are given below -
In elasticsearch, a client is used to access different nodes in a cluster. It performs some actions on them as well. To get the random client, use ESIntegTestCase.client() method. There are some other methods provided by elasticsearch, which are used to access the client. The ESIntegCase.internalCluster() method is used to access these methods. Following is a list of those methods -
As the name specifies, randomized testing tests the user's code for every possible data. Therefore, the chances of failure are very less with any type of data. It is the best way to perform this testing using random data.
Generating Random data
In this testing, RandomizedTest provides the instance that accelerates the Random class. Apart from this, it also offers various methods to obtain the different types of data. Following are the methods and their return value -
Remember that the testing feature is now deprecated by the elasticsearch community. But the guide provided in this chapter is for the users of the older version of elasticsearch.