Results 1 to 3 of 3

Thread: Spring test context and database setup

Hybrid View

  1. #1
    Join Date
    Jan 2013
    Posts
    2

    Default 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

  2. #2
    Join Date
    Jan 2006
    Location
    Zürich, Switzerland
    Posts
    423

    Default

    Having you considered using the jdbc namespace for populating the database as the ApplicationContext is loaded?

    If you need more control than you get from the XML namespace, you can simply use the DataSourceInitializer directly, and define it as a component in your application.

    Or you can just declare and configure a DatabasePopulator bean.

    Regards,

    Sam

  3. #3
    Join Date
    Jan 2013
    Posts
    2

    Default

    Hi Sam,

    thanks for the tips.

    Regards,
    Miljenko

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •