两个测试通过,但Elasticsearch仅显示第一个测试的结果

时间:2016-08-10 11:56:23

标签: elasticsearch spring-data-elasticsearch

我接受了以下两项测试:

@Test
    public void shouldCreateSingleBookEntityWithRepository(){

        //given
        Book book = Book.builder().id("1").title("Clean Code").build();
        IndexQuery indexQuery = getIndexQuery(book);

        //when
        String documentId = bookService.saveBook(indexQuery);

        //then
        assertThat(bookService.findOne(book.getId()), is(notNullValue()));
        assertThat(bookService.findOne(book.getId()).getId(), is(documentId));
    }

    @Test
    public void shouldCheckAllDocumentsPresenceAfterInsertSingleThreaded(){

        //given
        List<IndexQuery> indexQueries = new ArrayList<>();

        indexQueries.add(Book.builder().id("1").title("Learn Scala").build().getIndexQuery());
        indexQueries.add(Book.builder().id("2").title("Clean Code").build().getIndexQuery());
        indexQueries.add(Book.builder().id("3").title("Head First: JPA").build().getIndexQuery());
        indexQueries.add(Book.builder().id("4").title("Effective Java").build().getIndexQuery());

        bookService.saveBulkOfBooks(indexQueries);

        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withIndices(INDEX_NAME)
                .withTypes(BOOK_TYPE_NAME)
                .withQuery(matchAllQuery())
                .build();

        //when
        Long bookCount = elasticsearchTemplate.count(searchQuery);
        System.out.print("\n\n##########################################\n"
                +indexQueries.size()+bookCount+ bookService.findOne("2").getTitle()+
                "\n####################################################\n\n");
        //then
        assertThat(bookCount, is(Long.valueOf(indexQueries.size())));
        assertThat(bookService.findOne("1").getTitle(), is("Learn Scala"));
        assertThat(bookService.findOne("2").getTitle(), is("Clean Code"));
        assertThat(bookService.findOne("3").getTitle(), is("Head First: JPA"));
        assertThat(bookService.findOne("4").getTitle(), is("Effective Java"));
    }

每次我运行两个测试时,他们传递。但是,存储在ES中的结果仅代表一个book形式的第一次测试。

GET /bookshop/book/_search
{
   "took": 34,
   "timed_out": false,
   "_shards": {
      "total": 2,
      "successful": 2,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
         {
            "_index": "bookshop",
            "_type": "book",
            "_id": "1",
            "_score": 1,
            "_source": {
               "title": "Clean Code",
               "id": "1"
            }
         }
      ]
   }
}

当我忽略第一次测试时,我从ES的第二次测试中得到了4本书的结果。

我的测试用例中只有@Before方法

@Before
    public void before() {
         elasticsearchTemplate.deleteIndex(Book.class);
         elasticsearchTemplate.createIndex(Book.class);
         elasticsearchTemplate.putMapping(Book.class);
         elasticsearchTemplate.refresh(Book.class);
    }

为什么测试通过但是ES在第一次测试时只显示了一本文档?

1 个答案:

答案 0 :(得分:1)

这是因为带有@Before注释的方法将在使用@Test标记的任何方法之前执行。您可以尝试使用@BeforeClass。但是,我在使用@BeforeClass时可以看到的问题是您无法在那里访问elasticsearchTemplate,该方法必须为static

您可以改用@PostConstruct。 e.g。

@PostConstruct
public void init() {

    elasticsearchTemplate.deleteIndex(Book.class);
    elasticsearchTemplate.createIndex(Book.class);
    elasticsearchTemplate.putMapping(Book.class);
    elasticsearchTemplate.refresh(Book.class);
}