java 使用测试数据集进行DBUnit测试

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 使用测试数据集进行DBUnit测试相关的知识,希望对你有一定的参考价值。

@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace=Replace.NONE)
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
    DirtiesContextTestExecutionListener.class,
   TransactionalTestExecutionListener.class,
    DbUnitTestExecutionListener.class })
@DatabaseSetup("classpath:test-datasets.xml")

public class CustomerContactRepositoryDbUnitTest {

	@Autowired
    private TestEntityManager entityManager;
	
	@Autowired
	private CustomerContactRepository customerContactRepository;
	
	@Test
    public void testFindByEmail() {
		
        // Find an inserted record
        CustomerContact foundContact = customerContactRepository.findByEmail("elaine@myemail.com");
        
        assertThat(foundContact.getEmail(), is(equalTo("elaine@myemail.com")));
    }
	
	@Test
    public void testFindSpecificContactByIdBypassReposClass() {
		
        // Find an inserted record
        CustomerContact foundContact = entityManager.find(CustomerContact.class, new Long("2"));
        
        assertThat(foundContact.getEmail(), is(equalTo("elaine@myemail.com")));
    }
}

in src/test/resources/test-datasets.xml:

<?xml version="1.0" encoding="UTF-8"?>
<dataset>
  <customer_contact id="1" first_name="Jenny" email="jenny@myemail.com" />
  <customer_contact id="2" first_name="Elaine" email="elaine@myemail.com" />
  <customer_contact id="3" first_name="Susan" email="susan@myemail.com" />
  <customer_contact id="4" first_name="Bernard" email="bernard@myemail.com" />
</dataset>

以上是关于java 使用测试数据集进行DBUnit测试的主要内容,如果未能解决你的问题,请参考以下文章

DBUnit数据库测试

DBUnit数据库测试

DBUnit数据库测试

如何在不使用静态 @BeforeClass 的情况下设置/拆卸 dbunit 数据集

dbunit 性能最佳实践

DBUnit 自动数据集生成