2

In my arquillian test a number of application methods are called, which either read data from the database or create data and persist to database. a) At some point a method annotated with @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) is called. The test fails as the annotated method cannot find(on read it returns 0 records) some data persisted by some previous called methods(annotated with TransactionAttributeType.REQUIRED). Probably this is a normal behavior as a new Transaction opens. Reading the data in a previous called method and passing them as parameters to the annotated(TransactionAttributeType.REQUIRE) method works fine, but data should be read inside the REQUIRES_NEW method. Same problem appears in other tests when the TransactionAttributeType.REQUIRES_NEW is used. Is there a workaround to handle this case?

b) I've also mentioned that arquillian has issues with methods annotated with @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED). It is like the code in these methods is not executed at all.

My current solution for both issues is to create a Mock class overriding the problematic methods. In case of b, the overridden method is annotated with @TransactionAttribute(TransactionAttributeType.SUPPORTS).

anna
  • 745
  • 2
  • 9
  • 30
  • My temporary solution was to create a mock class which extends the standard implementation(class). In the mocked class I override the problematic method so that, when the annotated with TransactionAttributeType.REQUIRES_NEW method is called, will not create a new transaction. Finally, in the arquillian test the mocked version of the class is injected. In my case the REQUIRES_NEW was used to reduce the possibilities for transaction timeout exceptions, so this trick keeps testing the code without side effects. – anna Sep 02 '15 at 13:29
  • Has the first transaction already committed? Sounds like you're trying to read data from a different transaction which isn't allowed by your DB. – LightGuard Sep 03 '15 at 20:33
  • Hi @anna, have you found a proper solution to this? We are facing the same issue now, I am surprised that it isn't a more common occurrence with others! – KayDK Sep 17 '15 at 12:28
  • Hi @KDK, Unfortunately, I have kept my initial solution(limited time). It works fine but a more elegant solution would be better. Waiting for your post if you find something better. – anna Sep 17 '15 at 14:15

1 Answers1

0

Try using flush() method and then refresh() of EntityManager before calling the method annoted with TransactionAttributeType.REQUIRED_NEW.

Michu93
  • 5,058
  • 7
  • 47
  • 80
Chirag Soni
  • 437
  • 2
  • 5
  • 18
  • Thank you @Chirag soni. I have tried both of these. I have also tried EntityManager.clear(). Still the same issue. – anna Sep 02 '15 at 10:16