English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Elasticsearch Testing

Elasticsearch provides a jar file that can be added to any Java IDE and used to test code related to Elasticsearch. A series of tests can be executed using the framework provided by Elasticsearch. In this chapter, we will discuss these tests in detail.-

  • Unit test

  • Integration test

  • Random test

Prerequisites

To start the test, you need to add the Elasticsearch test dependency to your program. You can use Maven for this purpose and can add the following content to pom.xml.

<dependency>
   <groupId>org.elasticsearch</groupId>
   <artifactId>elasticsearch</artifactId>
   <version>2.1.0</version>
</dependency>

EsSetup has been initialized to start and stop Elasticsearch nodes and create indexes.

EsSetup esSetup = new EsSetup();

The function with createIndex in esSetup.execute() will create an index, you need to specify the settings, type, and data.

Unit test

Unit tests are performed using JUnit and the Elasticsearch test framework. Nodes and indexes can be created using the Elasticsearch class, and tests can be executed using the test method. The ESTestCase and ESTokenStreamTestCase classes are used for this test.

Integration test

Integration tests use multiple nodes in the cluster. The ESIntegTestCase class is used for this test. There are several methods to simplify the preparation of test cases.

Serial numberMethods and descriptions
1

refresh()

Refresh all indexes in the cluster

2

ensureGreen()

Ensure the green healthy cluster status

3

ensureYellow()

Ensure the yellow cluster status

4

createIndex(name)

Create an index using the name passed to this method

5

flush()

Refresh all indexes in the cluster
6

flushAndRefresh()

flush() and refresh()

7

indexExists(name)

Verify if the specified index exists

8

clusterService()

Return the cluster service Java class

9

cluster()

Return the test cluster class

Test cluster method

Serial numberMethods and descriptions
1

ensureAtLeastNumNodes(n)

Ensure the minimum number of nodes in the cluster is greater than or equal to the specified number

2

ensureAtMostNumNodes(n)

Ensure the maximum number of nodes in the cluster is less than or equal to the specified number

3

stopRandomNode()

Stop a random node in the cluster

4

stopCurrentMasterNode()

Stop the master node

5

stopRandomNonMaster()

Stop a random non-master node in the cluster

6

buildNode()

Create a new node

7

startNode(settings)

Start a new node

8

nodeSettings()

Override this method to change node settings

Access client

Clients are used to access different nodes in the cluster and perform certain operations. The ESIntegTestCase.client() method is used to get a random client. Elasticsearch also provides other methods to access clients, which can be used with the ESIntegTestCase.internalCluster() method.

Serial numberMethods and descriptions
1

iterator()

This helps you access all available clients

2

masterClient()

This will return a client that communicates with the master node

3

nonMasterClient()

This will return a client that does not communicate with the master node

4

clientNodeClient()

This will return the client currently on the client node

Random test

This test is used to test the user code with all possible data, so that no type of data failure will occur in the future. Random data is the best choice for executing this test.

Generate random data

In this test, the Random class is instantiated by the instance provided by RandomizedTest, and provides many methods for obtaining different types of data.

MethodReturn value
getRandom()Instance of random class
randomBoolean()Random boolean
randomByte()Random byte
randomShort()Random short
randomInt()Random integer
randomLong()Random long
randomFloat()Random float
randomDouble()Random double
randomLocale()Random locale
randomTimeZone()Random time zone
randomFrom()Random element from array

Assertion

The ElasticsearchAssertions and ElasticsearchGeoAssertions classes contain assertions that are used to perform some routine checks during testing. For example, observe the code provided here-

SearchResponse seearchResponse = client().prepareSearch();
assertHitCount(searchResponse, 6);
assertFirstHit(searchResponse, hasId("6);
assertSearchHits(searchResponse,123456