I've set up an Integration Test for a Spring Data JpaRepository which contains an @Query method that updates the associated entity, but I cannot correctly assert the result of the update in my test unless I set the clearAutomatically value of the query @Modifying annotation to true (which I don't want to do...)
I'm guessing that this is connected with when flushing occurs in the underlying EntityManager (or when the update statement is executed in the underlying db, or maybe caching?), but I'm a bit lost as to an explanation and (more importantly) how to alter the flushing configuration to avoid the need for the clearAutomatically?
Here's my Repo Interface...
and the associated test...Code:public interface ProductRepo extends JpaRepository<Product, String> { @Modifying //(clearAutomatically=true) required here in order for test to assert correctly... @Transactional @Query("update Product p set p.deleted = true where p.shortName = :shortName") int softDeleteByShortName(@Param("shortName") String shortName); }
Am I missing anything obvious? Any help would be very much appreciated as it's taken me quite a while to even get this far in determining where/what the problem is...Code:@ContextConfiguration({ //assume correct config here }) @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true) public class NormalisedProductRepoIntTests extends AbstractTransactionalJUnit4SpringContextTests { @Autowired private ProductRepo productRepo; @Test public void softDeleteByShortName_validShortName_entitySoftDeleted() { Product product = ProductObjectMother.createSimple(); productRepo.save(product); String id = product.getId(); assertNotNull(id); //assert save successful int count = productRepo.softDeleteByShortName(product.getShortName()); assertEquals(1, count); //this asserts ok, and so 1 row must have been updated product = productRepo.findOne(id); //reload the entity //this does not assert unless @Modifying(clearAutomatically=true) is set on repo method under test assertEquals(true, product.isDeleted()); } }
Best wishes,
Daniel


Reply With Quote
