The service method that I'm testing looks like this
void updateOrderStatusToComplete(def orderIdList) {
if (orderIdList) {
try{
def response = Order.executeUpdate("update Order tabrr set tabrr.status = :status where tabrr.id in (:idList)", [status: "Completed", idList: orderIdList])
} catch(Exception e){
throw new RuntimeException("Error updating orders to Complete")
}
}
}
My test first creates 4 orders:
void "updateRowsToMatch" (){
given: "we have 4 existing orders with status open"
def order1 = new Order(description: "item 1", price: 50.00, status: "Open")
order1.save()
def order2 = new Order(description: "item 2", price: 60.00, status: "Open")
order2.save
def order3 = new Order(description: "item 3", price: 70.00, status: "Open")
order3.save()
def order4 = new Order(description: "item 4", price: 80.00, status: "Open")
order4.save()
def orderIdList = [order1.id, order2.id, order3.id, order4.id]
when: "we use orderservice to update the rows status...."
orderservice.updateOrderStatusToComplete(orderIdList)
then: "the orders status should be updated to "Completed"
assert Order.get(order1.id).status == "Completed"
assert Order.get(order2.id).status == "Completed"
assert Order.get(order3.id).status == "Completed"
assert Order.get(order4.id).status == "Completed"
}
When I seed the database with table rows prior to the test and use those ids, then the test passes, the existing rows are updated. The problem seems to only occur when I create table rows in the test. But the interesting thing is that the return value for executeUpdate is 4, which means that 4 table rows have in fact been updated, so I'm a bit stumped here as to why the status isn't actually changing. The test uses the @Integration and @Rollback annotations, so that means that the test transactions are being rolled back rather than fully committed during the test. But I can still retrieve the order's that have been saved using Order.findById(...) or Order.findByDescription(), so I'm not sure why this persists for the duration of the test, but not the executeUpdate transaction.
found the solution. Flushing didn't seem to work, but clearing the session did, so at the start of the test class insert a setter for Hibernate SessionFactory
def sessionFactory
then after saving the objects
sessionFactory.currentSession.clear()
Doing this allowed the objects created in the test to be updated and retrieved