17

I have a Test like this:

@Transactional
@Test
public void addRemoveTest() {
    MyEntitiy entity = new MyEntitiy ();

    entity = textureRepository.saveAndFlush(entity );
    TestTransaction.flagForCommit();
    TestTransaction.end();
    TestTransaction.start();
    final MyEntitiy loadedTexture = myEntityRepository.findOne(entity .getId());
}

This works perfectly fine. But when I remove the Committing code for the transaction, the repository will not call the database when calling findOne(). Can I somehow force it to make the database call for my tests?

I prefer my test to not commit any transaction.

I'm not able to get the Session to clear the cache. And I'm not even sure if clearing the session would help.

Apostolos
  • 10,033
  • 5
  • 24
  • 39
Tarion
  • 16,283
  • 13
  • 71
  • 107
  • One worarround is to use `findAll()` since I just want my UserType to be executed that works for me :) – Tarion Nov 16 '15 at 12:02
  • `.findOne` should change to `.getOne`, they are different. Beside that why you need to force it to make the database call. It's not necessary. – RJ.Hwang Oct 08 '18 at 07:47

1 Answers1

35

The only way I know is to call entityManager.clear() before calling the finder. I think you can autowire the EntityManager into your test:

@Autowired
private EntityManager entityManager;

Your test would then look like this:

@Transactional
@Test
public void addRemoveTest() {
    MyEntitiy entity = new MyEntitiy ();

    entity = textureRepository.saveAndFlush(entity );
    entityManager.clear();
    final MyEntitiy loadedTexture = myEntityRepository.findOne(entity .getId());
}
Mathias Dpunkt
  • 11,594
  • 4
  • 45
  • 70
  • I wrote an entire new question, and at the last second I saw this... Thank you! – Ran Jun 13 '18 at 06:54