The problem I got
After adding a additional Test, which should use a @ActiveProfiles
, i got exceptions which are rise when the database-schema.sql script is running twice on the same database.
What framework i use
spring:3.2.13.Release
&spring-data-jpa:1.6.5.RELEASE
&hsqldb:2.3.2
&hibernate-entitymanager:4.3.1.FINAL
junit:4.12
&spring-test:3.2.13.RELEASE
What do i use in my code/tests
- to create the datasource i use
<jdbc:embedded-database id="dataSource">
in my.../test/resources/spring/testConfig.xml
@RunWith(SpringJUnit4ClassRunner.class)
and@ContextConfiguration(locations = {"classpath:spring/testConfig.xml"})
for both of my test- the test with
@ActiveProfiles
extended the other test <- i checked, if this is the problem here, but it isnt.
My Guess
after adding a @ActiveProfiles
the spring contest has to be recreated because there are other beans or other settings which depends on the active profile. But it seems that the database isnt dropped.
What i dont want
I dont want to change the schema.sql file to something like
create table exampleModel if not exists
because this script is generated automaticly in intervals, and in my production situation there are many custom types and tables, which i dont want to edit manualy.
What i want
reuse of the database (so its not initialized for every test)
Way to reproduce
Checkout my github-repo
run
mvn test
in./test/
Exception
java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [spring/testConfig.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Invocation of init method failed; nested exception is org.springframework.dao.DataAccessResourceFailureException: Failed to execute database script; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement at line 1 of resource class path resource [testDB/schema.sql]: CREATE TYPE TEXT AS VARCHAR(1000000)
...
Caused by: java.sql.SQLSyntaxErrorException: object name already exists: TEXT in statement [CREATE TYPE TEXT AS VARCHAR(1000000)]
...
Caused by: org.hsqldb.HsqlException: object name already exists: TEXT