hibernate 使用spring-test-dbunit将数据加载到内存数据库中

9ceoxa92  于 2023-10-23  发布在  Spring
关注(0)|答案(1)|浏览(144)

我正在尝试为java spring应用程序设置一些集成测试。我按照指示(https://github.com/ppodgorsek/spring-test-dbunit),但我似乎不能得到的东西工作。我可以验证表是否正在创建,模式验证是否通过。然而,当我在测试中实际查询查询时,我得到了如下错误:
java.lang.IllegalArgumentException:无法使用类com.github.springtestdbunit.dataset.FlatXmlDataSetLoader从“data.xml”加载数据集
似乎我的data.xml文件在错误的位置,但我不清楚它应该在哪里。我试着把它放在与测试类相同的包中(src/test/java/integration),但得到了相同的结果。我不太清楚我做错了什么,谁能给我一些建议吗?
我的测试类看起来像这样:

@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);
        }
    }
}

它在CrudRepository上执行查询,看起来像:

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

一个实体类:

@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;
...
}

在src/test/resources目录中,我有一个application.properties文件:

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

一个data.xml文件:

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

3gtaxfhh1#

经过反复试验,我终于明白了。如果其他任何人遇到这种情况,xml数据集需要位于测试资源中与测试类的包相匹配的目录中。因此,如果测试类位于src/test/java/A/B/C中,则data.xml需要位于src/test/resources/A/B/C中。否则,您需要提供一个像这样的显式路径:
@DatabaseSetup(“/META-INF/dbtest/sampleData.xml”)

相关问题