Spring test context and database setup
Hi all,
I don't know did I knock on the right place with a question, but here's my problem. I have an application based on Spring and OpenJPA. To enable unit testing I'm using embedded H2 database that is filled with data for each unit test in a @Before-annotated method, like this:
Code:
public class FooServiceTest extends AbstractTransactionalJUnit4SpringContextTests {
@Autowired
private FooService fooService;
@Before
public void setUp() {
// fill database for FooService
Resource resource = new FileSystemResource(new File(this.getClass().getResource("somePath").getPath()));
this.executeSqlScript(resource.getURL().toString(), false);
}
}
As said, Spring configuration uses embedded H2 database, and my persistence.xml looks like this:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="MyPU" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>com.test.Foo</class>
<!-- other classes -->
<properties>
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(SchemaAction='add,deleteTableContents')" />
</properties>
</persistence>
My FooService looks like this:
Code:
public class FooService {
public void init() {
// get some data from database
}}
And my Spring configuration XML is like this:
Code:
<bean id="fooService" class="com.test.service.FooService" init-method="init" />
The problem is that FooService init() method is called before unit test @Before method, and my unit test fails because it expects some data to exist in database.
Is there any way to seed the database before unit test and actual Spring configuration? I know about DBUnit, but this particular OpenJPA configuration is reading my entities and creating database and I do not need a separate SQL to create database.
Thanks,
Miljenko