Announcement Announcement Module
No announcement yet.
How do you write an Integration Test for Grails/JPA code? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How do you write an Integration Test for Grails/JPA code?

    I've been experiencing some issues in using the JOINED inheritance strategy using JPA within a Grails project and having problems with the dynamically created method count throwing an exception : org.springframework.dao.IncorrectResultSizeDataAcc essException: query did not return a unique result: 3
    (see this thread: )

    In order to properly diagnose this and see if it's a Hibernate or a badly implemented dynamic method, I've been struggling to work out how to test this and submit an appropriate JIRA.

    My progress so far has resulted in attempting to use the resources.groovy like so:
    beans = {
    "javax.persistence.spi.PersistenceUnitTransactionT ype.RESOURCE_LOCAL"(org.springframework.beans.fact ory.config.FieldRetrievingFactoryBean)
    entityManagerFactory(org.hibernate.ejb.EntityManag erFactoryImpl, ref('sessionFactory'), ref('javax.persistence.spi.PersistenceUnitTransact ionType.RESOURCE_LOCAL'),true,null)
    transactionManager(org.springframework.orm.jpa.Jpa TransactionManager) {
    dataSource = ref('dataSource')
    entityManagerFactory = ref('entityManagerFactory')

    along with a copy of hibernate-entitymanager-3-1.4.0.GA.jar in my lib folder, so I can write a test that uses the entity manager.

    It's an integration test of sorts...
    I'd like to make use of all the infrastructure Grails uses to setup the classpath, application context etc via the BeanBuilder DSLs, and hopefully inject the EntityManagerFactory.
    My thinking is this isn't going to be a conventional Groovy class, rather a Java one as I'm looking to use something like.

    resultCount = (Integer) em.createQuery(query1).getSingleResult()

    where em is an EntityManager instance created from the factory.
    What's the best way to do this?
    Can I for example use something like this in a java class in the integration folder:

    import org.springframework.context.ApplicationContext;
    import plicationContext;
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext .xml");

    and expect all the grails commands to setup my environment for me?

    I'm assuming applicationContext - because that's the one Grails uses to bootstrap the web app, but is it the same for the test/integration test environment?

    I've also posted a thread here on the same topic

    Is this approach possible? Does anyone have a recommendation?