8 Replies Latest reply on Jun 2, 2009 4:38 AM by Chris Lowe

    Overriding an EJB datasource for testing

    Chris Lowe Apprentice


      I have a Seam project (EAR) generated using JBoss tools.

      I'm currently trying to write some integration tests by extending SeamTest class and implementing the ComponentTest hooks. So far so good - I can get the tests firing.

      However, for my tests I'd like to use the embedded HSQLDB, create the DDL from my entities and populate it with my test data. This is where I'm stuck. I can't get the bootstrap to use any other version of persistence.xml other than that specified in the EJB project.

      Can anyone tell me what I need to do?



        • 1. Re: Overriding an EJB datasource for testing
          Chris Lowe Apprentice

          I've really hit a brick wall on this issue. Can anyone confirm either way if it is possible to make a test use a different version persistence.xml and import.sql?



          • 2. Re: Overriding an EJB datasource for testing
            Max Rydahl Andersen Master

            Look in the test project - here you can redefine the datasource used for testing, no need for a different persistence.xml

            similarly if you place import.sql in the test project it should be picked up before the normal import.sql

            • 3. Re: Overriding an EJB datasource for testing
              Chris Lowe Apprentice

              Thanks for the reply Max.

              I now specify the local datasource against DefaultDS.

              I also needed to override some of my properties in that were specified in persistence.xml, specifically hibernate.dialect and hibernate.hbm2ddl.auto - for my test I want to use "create-drop", but in dev and production I just want to use "validate".

              I tried editing bootstrap/META-INF/persistence.properties in order to override the values but this seemingly had no effect. In the end I moved the properties that I wanted to override into their own perstence.properties in the EJB for the dev/production properties and set my test properties in bootstrap/META-INF/persistence.properties. Now my tests and my dev/production environments work appear to work.

              Is the factoring of the properties in this way legit or have I fluked it and Hibernate is just making some sensible defaults?



              • 5. Re: Overriding an EJB datasource for testing
                Chris Lowe Apprentice

                Good stuff! Thanks for your time Max.

                • 6. Re: Overriding an EJB datasource for testing
                  Nicklas Karlsson Master

                  I'm trying a similar setup with a Seam WAR.

                  I have a prod persistence.xml which defines a java:/ProdDatasource. In the same dir I have a persistence.xml with


                  and then in my test project I have in my bootstrap/persistence.properties


                  and by DB testng.xml does

                   <parameter name="datasourceJndiName" value="java:/DefaultDS" />
                   <parameter name="database" value="HSQL" />
                   <parameter name="binaryDir" value="" />

                  and the import in the test class

                   protected void prepareDBUnitOperations()
                   beforeTestOperations.add(new DataSetOperation("path/to/my/BaseData.xml"));

                  but the strange result is that I get Oracle create-errors and then I get another error for table not found when the import is attempted.

                  Any pointers are appreciated,

                  • 7. Re: Overriding an EJB datasource for testing
                    Nicklas Karlsson Master

                    I'm still interested in pointers.

                    Another thing that emerged is the fact that I have some native queries that (of course) doesn't have their tables autogenerated. Is there a place to stick creation scripts so that the Seam DB tests would pick them up?

                    thanks in advance,

                    PS. Tried looking for a users mailing list, is there one since this forum seem quite low-traffic?

                    • 8. Re: Overriding an EJB datasource for testing
                      Chris Lowe Apprentice


                      For the native queries problem one solution might be to create entity objects for the extra tables you require then they would just get generated as part of the create-drop in your tests.

                      As for your original issue, I'm afraid I have no experience with Oracle and JPA. It does seem odd however that you're getting creation errors when your persistence properties are supposed to "validate" the schema. Sounds like maybe the correct persistence.xml is being picked up (hence a connection to Oracle) but the wrong persistence.properties (hence creation errors). Where are your properties files located? My project was using an EAR structure and I had to put my production properties in my EJB project rather than the bootstrap/META-INF. Also make sure Eclipse is aware of the files and is actually copying them the relevant .ear file.