Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's an example that I was able to get working.</p> <p>I utilize the <code>@ContextConfiguration</code> annotation to start the context using 3 different files. "MyJobConfiguration.xml" contains the production batch job definition which I won't include here (irrelevant to this question). "BatchIntegrationTests.xml" is a common context configuration used for all of my batch integration tests. It handles overriding the datasource to use an in-memory database that automatically generates the schema. Again, not included. </p> <p>"BatchJobIT.xml" is where I set up the actual repository populator I will be using. Unfortunately, the <code>repository:unmarshaller-populator</code> tag requires the <code>locations</code> attribute to be specified; so I needed to provide a 'dummy' xml file (com/example/anyData.xml) that the repository populator will load when the application contexts starts. I also specify to use an XStream Unmarshaller and supply any aliases it might encounter during my tests.</p> <p>BatchJobIT.xml</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!-- * * Additional Configuration for BatchJobIT integration tests. * --&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:repository="http://www.springframework.org/schema/data/repository" xmlns:oxm="http://www.springframework.org/schema/oxm" xsi:schemaLocation="http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.5.xsd http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"&gt; &lt;repository:unmarshaller-populator id="populator" locations="classpath:com/example/anyData.xml" unmarshaller-ref="xstreamMarshaller" /&gt; &lt;bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller"&gt; &lt;property name="aliases"&gt; &lt;props&gt; &lt;prop key="domainObjects"&gt;java.util.List&lt;/prop&gt; &lt;prop key="domainObject"&gt;com.example.domain.MyObject&lt;/prop&gt; &lt;/props&gt; &lt;/property&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre> <p>The actual test class is then pretty straightforward... with a little hackiness going on in the <code>@Setup</code>. First, I <code>@Autowire</code> the populator to get a reference to it. I also need a reference to the application context, so I <code>@Autowire</code> that as well.</p> <p>For this example, I search the classpath for a test-data.xml file qualified by the test class name and test method name. Then, I set that test-data file as the resource location on the populator and fire off a <code>ContextRefreshedEvent</code> which causes the populator to re-populate it's repositories.</p> <p>BatchJobIT.java</p> <pre><code>package com.example; import static org.junit.Assert.assertNotNull; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TestName; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.data.repository.init.UnmarshallerRepositoryPopulatorFactoryBean; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * This integration test will test a Spring Batch job. */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "/modules/MyJobConfiguration.xml", "BatchIntegrationTests.xml", "BatchJobIT.xml" }) public class BatchJobIT { @Autowired private EntityManagerFactory entityManagerFactory; @Autowired private UnmarshallerRepositoryPopulatorFactoryBean populator; //Keep track of the test name for use within the test methods @Rule public TestName name = new TestName(); @Autowired private ApplicationContext applicationContext; @Before public void setUp() throws Exception { //Dynamically build the path to the test-data file. In this example, classpath will be searched for //'com/example/BatchJobIT.testBatchJob.test-data.xml' StringBuilder testDataFileName = new StringBuilder(); testDataFileName.append(this.getClass().getCanonicalName()); testDataFileName.replace(0, testDataFileName.length(), testDataFileName.toString().replaceAll("\\.", "/")); testDataFileName.append("."); testDataFileName.append(name.getMethodName()); testDataFileName.append(".test-data.xml"); //Set new resource location populator.getObject().setResourceLocation(testDataFileName.toString()); //Send a context refreshed event to the populator which causes it to re-populate repositories populator.onApplicationEvent(new ContextRefreshedEvent(applicationContext)); } @After public void tearDown() throws Exception { //Delete test data from database EntityManager em = entityManagerFactory.createEntityManager(); try { em.getTransaction().begin(); em.createNativeQuery("TRUNCATE SCHEMA public AND COMMIT").executeUpdate(); if (em.getTransaction().isActive()) { em.getTransaction().commit(); } } catch (Exception e) { if (em.getTransaction().isActive()) { em.getTransaction().rollback(); } throw e; } finally { em.close(); } } @Test public void testBatchJob() throws Exception { //Uncomment to launch hsqldb manager to inspect that data was loaded //org.hsqldb.util.DatabaseManagerSwing.main(new String[] { "--url", "jdbc:hsqldb:mem:testdb" }); //Run test assertNotNull(populator); } } </code></pre> <p>This does feel a little hacky but I think it's robust enough until I can look closer at spring-test-dbunit like @NealeU suggested.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload