Back to Repositories

Testing Elasticsearch High-Level Client Operations in spring-boot-demo

This test suite demonstrates comprehensive testing of Elasticsearch operations in a Spring Boot application. It covers essential CRUD operations and index management using the Elasticsearch High Level REST Client, validating the PersonService implementation.

Test Coverage Overview

The test suite provides complete coverage of Elasticsearch operations including index management and CRUD functionality.

  • Index operations: creation and deletion
  • Document operations: insert, update, delete, and search
  • Data handling with Person entity objects
  • Integration with Spring Boot context

Implementation Analysis

The testing approach utilizes Spring Boot Test framework with JUnit4 integration. Tests are organized around the PersonService class, using @SpringBootTest for full application context loading and dependency injection. Each test method focuses on a specific Elasticsearch operation with clear separation of concerns.

Technical Details

  • Spring Test Context framework with @RunWith(SpringRunner.class)
  • JUnit4 test annotations
  • Elasticsearch High Level REST Client
  • Spring Boot Test configuration
  • PersonService autowiring for dependency injection

Best Practices Demonstrated

The test suite exemplifies several testing best practices for Elasticsearch integration.

  • Isolated test methods for each operation
  • Consistent use of test data structures
  • Clear test method naming conventions
  • Proper Spring Boot test configuration
  • Structured index and document management

xkcoding/spring-boot-demo

demo-elasticsearch-rest-high-level-client/src/test/java/com/xkcoding/elasticsearch/ElasticsearchApplicationTests.java

            
package com.xkcoding.elasticsearch;

import com.xkcoding.elasticsearch.contants.ElasticsearchConstant;
import com.xkcoding.elasticsearch.model.Person;
import com.xkcoding.elasticsearch.service.PersonService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ElasticsearchApplicationTests {

    @Autowired
    private PersonService personService;

    /**
     * 测试删除索引
     */
    @Test
    public void deleteIndexTest() {
        personService.deleteIndex(ElasticsearchConstant.INDEX_NAME);
    }

    /**
     * 测试创建索引
     */
    @Test
    public void createIndexTest() {
        personService.createIndex(ElasticsearchConstant.INDEX_NAME);
    }

    /**
     * 测试新增
     */
    @Test
    public void insertTest() {
        List<Person> list = new ArrayList<>();
        list.add(Person.builder().age(11).birthday(new Date()).country("CN").id(1L).name("哈哈").remark("test1").build());
        list.add(Person.builder().age(22).birthday(new Date()).country("US").id(2L).name("hiahia").remark("test2").build());
        list.add(Person.builder().age(33).birthday(new Date()).country("ID").id(3L).name("呵呵").remark("test3").build());

        personService.insert(ElasticsearchConstant.INDEX_NAME, list);
    }

    /**
     * 测试更新
     */
    @Test
    public void updateTest() {
        Person person = Person.builder().age(33).birthday(new Date()).country("ID_update").id(3L).name("呵呵update").remark("test3_update").build();
        List<Person> list = new ArrayList<>();
        list.add(person);
        personService.update(ElasticsearchConstant.INDEX_NAME, list);
    }

    /**
     * 测试删除
     */
    @Test
    public void deleteTest() {
        personService.delete(ElasticsearchConstant.INDEX_NAME, Person.builder().id(1L).build());
    }

    /**
     * 测试查询
     */
    @Test
    public void searchListTest() {
        List<Person> personList = personService.searchList(ElasticsearchConstant.INDEX_NAME);
        System.out.println(personList);
    }

}