I have a problem with session and integration test. Here is what I tried. I have a service called ShoppingCartWebshopService and it has the methode "createNewShoppableItem". This method calls two methods from PriceService and there is the problem.
Part of PriceService:
def sessionFactory_lookup
def getActionResult(){
final session = sessionFactory_lookup.currentSession.connection()
def conn = new Sql(session)
{query and rest of the code}
}
Part of ShoppingCartWebshopService:
def customerPriceDetails = priceService.getActionResult()
My integration test:
class ShoppingCartWebshopServiceIntegrationSpec extends IntegrationSpec {
def shoppingCartWebshopService
def priceService
def sessionFactory
void "test something"() {
when:
//First version of session injection:
// ApplicationContext context = (ApplicationContext) ServletContextHolder.getServletContext().getAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT);
// SessionFactory sf = context.getBean('sessionFactory')
// Session session = sf.getCurrentSession()
shoppingCartWebshopService.priceService = priceService
shoppingCartWebshopService.priceService.getActionResult("003-65607758", "19")
//Second version of session injection:
def session = sessionFactory.getCurrentSession()
//
shoppingCartWebshopService.priceService.sessionFactory_lookup = session
def artikl = shoppingCartWebshopService.createNewShoppableItem("003-65607758", "19")
then:
artikl.size() > 0
}
}
The first version I found here but it doesn't work. Grails integration test and sessionFactory.currentSession
With first version I get his error: "groovy.lang.MissingPropertyException: No such property: currentSession for class: org.hibernate.impl.SessionImpl"
With second version I get this: java.lang.NullPointerException: "Cannot get property 'currentSession' on null object" at the part in PriceService where I call session for sql query execution.
Please, could someone give me an advice because I don't know what to try anymore. Thank you.