Javatpoint Logo
Javatpoint Logo

Elasticsearch Testing

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 -

  1. Unit testing
  2. Integration testing
  3. Randomized Testing

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.

Pre-requirements

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

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

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 -

Sr.No Method Description
1 cluster() The cluster() method returns the test cluster class.
2 clusterService() This function returns the cluster service java class.
3 createIndex(name) This method is used to create an index by the name passed in it.
4 ensureGreen() This function helps to ensure the green health cluster state.
5 ensureYellow() This function helps to confirm the yellow health cluster state.
6 flush() This function is used to flush all the indices in a cluster.
7 flushAndRefresh() It performs both flush() and refresh() operation in a single term.
8 indexExists(name) This function verifies whether the specified index exists or not. This means it ensures the existence of the index.
9 refresh() This function is used to refresh all the indices in a cluster.

Test Cluster Methods

Elasticsearch offers several test cluster methods, which are given below -

Sr.No Method Description
1 ensureAtLeastNumNodes(n) As the name describes, this method is used to ensure the minimum number of nodes up in a cluster is more than or equal to the specified number.
2 ensureAtMostNumNodes(n) As the name describes, it is used to ensure that the maximum number of nodes up in a cluster is equal or less than to the specified number.
3 stopRandomNode() This function is used to stop a random node in a cluster.
4 stopCurrentMasterNode() To stop the master node, this function is useful.
5 stopRandomNonMaster() This function helps to stop a random node in a cluster. However, this random node is not a master node.
6 buildNode() The buildNode() function is used to create a new node.
7 startNode(settings) This function is used to start a new node.
8 nodeSettings() To change the node setting, we need to override this method.

Accessing Clients

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 -

Sr.No Method Description
1 iterator() The iterator methods used to access all available clients.
2 masterClient() This method returns a client that communicates with master node.
3 nonMasterClient() Unlike masterClient() method, it returns a client that does not communicate with master node.
4 clientNodeClient() The method returns a client, which currently up on the client node.

Randomized Testing

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 -

Method Return Value
getRandom() It returns an instance of random class.
getBoolean() Return random boolean value.
randomByte() Return random byte
randomShort() Return random short
randomInt() Return random int
randomLong() Return random long
randomFloat() Return random float
randomDouble() Return random double
randomLocale() Return random locale
randomTimeZone() Randomly return the time zone
randomFrom() Returns random element from array

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.


Next TopicIngest Node




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA