Hi,
I have a test class using testng and extends SeamTest, with two test methods. Both tests are calling a method to setup data for the test in the doBefore method.
I am testing repository methods that doing search using lucene as follow:
Code:
public int countResults(...){
FullTextQuery query = buildSearchQuery(...);
return query.getResultSize();
}
public List<T> search(...){
FullTextQuery query = buildSearchQuery(...);
return query.getResultList();
}
The first test method pass, but the second test fail when it asserts count equals some value (say n), b/c the returned count is 2n, the wired is the search method returning results that of size n. And if I changed the count method to return query.getResultList().size(), my test pass.
So it is a problem w/ lucene indexes seems it is not cleaned after the first test method, and since getResultSize() not accessing db to fetch data, I got count = 2n.
I am using HSQL db for unit tests, and it is configured to create-drop data, and I am using RAMDirectory to have lucene indexes in-memory.
The last thing, I made a workaround to make my tests pass. In the doBefore I am reindexing my entity before calling setupData because if I called it after setup data, I also got wrong counts but now at the first test too.
Here's my doBefore method:
Code:
@BeforeMethod
public void begin() {
super.begin();
componentTest = new AbstractTransactionEnabledComponentTest() {
@Override
public void doBefore() throws Exception {
super.doBefore();
. . .
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
try {
fullTextEntityManager.createIndexer(MyEntity.class).startAndWait();
} catch (InterruptedException e) {
log.warn("Interrupted while indexing entire search index: {}", e.toString());
}
setUpData();
}
};
}
What I think is when reindexing a class and it has zero entities in the db, the index is cleaned, but if it has entities, index is updated for the new entities.
So what should be the correct fix to this problem? how to clean lucene index after each test method, as hibernate does for the database?