# HS6, Testing and @Transactional

**URL:** <https://discourse.hibernate.org/t/hs6-testing-and-transactional/6032>\
**Category:** Hibernate Search\
**Created:** [February 9, 2022, 10:41am UTC](https://discourse.hibernate.org/t/hs6-testing-and-transactional/6032 "2022-02-09T10:41:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dunyr](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/dunyr/32/3126_2.png) [@Dunyr](https://discourse.hibernate.org/u/Dunyr)\
**Post date:** [February 9, 2022, 10:41am UTC](https://discourse.hibernate.org/t/hs6-testing-and-transactional/6032/1 "2022-02-09T10:41:32Z")

</div>

I wanted to test my research in my springboot + hibernate application.

In order to do that I used the in memory index for the test configuration.

```auto
hibernate.search.backend.directory.type = local-heap
hibernate.search.automatic_indexing.synchronization.strategy = sync

```

And then made a dummy test, who create and saveAndFlush an entity that I then search:

```auto
@Test
public void testSearchBook() {
    String ean = "000000000001";
    Book book = createBook(ean);
    bookDao.saveAndFlush(book);

    SearchFilterDto filter = new SearchFilterDto();
    filter.setEan(ean);

    List<Book> results = bookService.searchBook(filter);
    assertThat(results.size()).isEqualTo(1);
}

```

With all my test classes extending:

```auto
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public abstract class BaseServiceTest

```

And I couldn’t get the test working, I tried to delay the search expecting that the indexing wasn’t fast enought but it didn’t changed the outcome.

I finally found out that the problem came from the `@Transactional` annotation (javax.transaction.Transactional).  
If I remove it globally or just for this test with `@Transactional(TxType.NEVER)` then the test pass sucessfully.

Does someone know what side effects of `@Transactional` cause the test to fail ?

---

<div class="post-metadata">

**Author:** ![yrodiere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yrodiere/32/1760_2.png) [@yrodiere](https://discourse.hibernate.org/u/yrodiere)\
**Post date:** [February 9, 2022, 10:49am UTC](https://discourse.hibernate.org/t/hs6-testing-and-transactional/6032/2 "2022-02-09T10:49:04Z")

</div>

When you make changes to your database in a transaction, [indexing happens on commit](https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#coordination-none-indexing-guarantee). That’s actually a good thing, and generally what you want in production.

However, if you annotate your test with `@Transactional`, the commit happens at the end of the test method. Thus indexing won’t happen until after your test is finished, and you will never see the result of indexing.

Either:

- Move the logic you want to test to a separate component (database changes in one method, search query in another), and annotate _that_ component with `@Transactional`
- Or use [Spring’s TransactionTemplates](https://www.baeldung.com/spring-programmatic-transaction-management) to explicitly wrap your data changes in one transaction, and your search query in another transaction (after the changes have been committed).

---

<div class="post-metadata">

**Author:** ![Dunyr](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/dunyr/32/3126_2.png) [@Dunyr](https://discourse.hibernate.org/u/Dunyr)\
**Post date:** [February 9, 2022, 4:18pm UTC](https://discourse.hibernate.org/t/hs6-testing-and-transactional/6032/3 "2022-02-09T16:18:48Z")

</div>

Thank you for responding so quickly.  
I knew indexing was on commit but I wrongly assumed that a flush would provoke said commit.

I will keep the global `@Transactional` annotation since I like the default rollback by Spring for all the tests who don’t rely on indexes.

Since the data’s creation and the search are already executed in their own separate transactions I just need to remove the one on the test method (added by the global annotation) with `@Transactional(TxType.NEVER)`.

And as mentionned [here](https://discourse.hibernate.org/t/hibernate-search-6-and-spring-boot-and-testing/5340/7) adding on the test method `@DirtiesContext(methodMode = MethodMode.AFTER_METHOD)` also renew the test context emptying datas and indexes for the next tests.
