1

I'm trying to set up some integration tests for a java spring application. I followed the instructions (https://github.com/ppodgorsek/spring-test-dbunit) but I can't seem to get things working. I can verify that the tables are getting created and the schema validation is passing. When I actually query the datasource in my tests however, I'm getting errors like:

java.lang.IllegalArgumentException: Unable to load dataset from "data.xml" using class com.github.springtestdbunit.dataset.FlatXmlDataSetLoader

It seems like my data.xml file is in the wrong location, but I'm not clear on where it should be. I tried putting in in the same package as the test class (src/test/java/integration) but got the same results. I'm not quite sure what I am doing wrong, can anyone offer some suggestions?

My test class looks like this:

@SpringJUnitConfig
@ContextConfiguration
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
        DbUnitTestExecutionListener.class })
@SpringBootTest
@DatabaseSetup("data.xml")
@DatabaseTearDown("data.xml")
public class SubscriptionQueryTest{

    @Autowired
    private LearnerSubscriptionRepository learnerSubscriptionRepository;

    @Test
    public void testSubscriptionQuery()
    {
        assertNotNull(learnerSubscriptionRepository);
    }

    @Test
    public void testFindDistinctEmails()
    {
        long lCount = learnerSubscriptionRepository.count();
        assertTrue(lCount > 0);
        ArrayList<String> learnerEmails = learnerSubscriptionRepository.findDistinctMails();
        assertNotNull(learnerEmails);
        assertEquals(6, learnerEmails.size());
        List<String> found = new ArrayList<>();
        //check that all the results are unique values
        for (String email : learnerEmails ) {
            assertFalse(found.contains(email));
            found.add(email);
        }
    }
}

It is executing queries on a CrudRepository that looks like:

public interface LearnerSubscriptionRepository extends CrudRepository<LearnerSubscription, Integer> {
    @Query("SELECT DISTINCT mail FROM LearnerSubscription")
    ArrayList<String> findDistinctMails();
}

with an entity class:

@Entity
public class LearnerSubscription {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", length = 100, nullable = false, unique = true)
    private int id;
    
    @Column(name = "sub", length = 100, nullable = false, unique = false)
    private String sub;

    @Column(name = "mail", length = 100, nullable = false, unique = false)
    private String mail;
...
}

In the src/test/resources directory, I have an application.properties files:

jdbc.driverClassName=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:myDb;mode=mysql;DB_CLOSE_DELAY=-1;NON_KEYWORDS=KEY,VALUE
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.show-sql=true
spring.jpa.hibernate.use-new-id-generator-mappings=false

and a data.xml file:

<?xml version='1.0' encoding='UTF-8'?>
<dataset>
  <learner_subscription id="330" mail="foo@bar.edu" opted_in="true" sub="foo039" subscription="6e8c478e-aabe-4789-ab22-8092e19c2dde" individual_emails="false"/>
  <learner_subscription id="331" mail="foo@bar.edu" opted_in="true" sub="foo039" subscription="890740d5-2edc-4843-9500-c5841da835d3" individual_emails="false"/>
  <learner_subscription id="332" mail="foo@bar.edu" opted_in="true" sub="foo039" subscription="23dc6e5e-26cf-447e-9f16-79cea544dc77" individual_emails="false"/>
  <learner_subscription id="333" mail="foo@bar.edu" opted_in="true" sub="foo039" subscription="57823ff6-e262-4201-8766-84cd17dfe116" individual_emails="false"/>
  <learner_subscription id="334" mail="foo@bar.edu" opted_in="true" sub="foo039" subscription="dfaeb775-dd7c-4f8e-a632-624df4bae041" individual_emails="false"/>
  <learner_subscription id="335" mail="foo@bar.edu" opted_in="true" sub="foo039" subscription="6b7ea99a-a9d2-4610-abb7-aef37eec4c26" individual_emails="false"/>
</dataset>
pbuchheit
  • 1,371
  • 1
  • 20
  • 47

1 Answers1

0

After some trial and error I finally figured it out. If anyone else runs into this, the xml dataset needs to be in test resources in a directory that matches the package of the test class. So if the test class is located in src/test/java/A/B/C then the data.xml needs to be in src/test/resources/A/B/C. Otherwise, you need to provide an explicit path like this:

@DatabaseSetup("/META-INF/dbtest/sampleData.xml")

pbuchheit
  • 1,371
  • 1
  • 20
  • 47