我们应该在 Spring Boot 中为存储层编写单元测试和集成测试吗?
Should we write unit test and integration test for repository layer in Spring Boot?
我有一堆关于 Spring Boot
存储库层的问题。
我的问题是:
- 我们是否应该为
Spring Boot
中没有任何自定义代码或查询的存储库层编写单元测试和集成测试?
- 在
Spring Boot
中为存储库层编写集成测试的最佳方法是什么?我在下面列出了这两种方法。在这两种方法中,哪一种更好。有没有我应该遵循其中一个的最佳实践?
- 如果我对上述问题 #1 的回答是肯定的,那么我该如何在
Spring Boot
中为存储库层编写单元测试?
CurrencyRepository.java
@Repository
public interface CurrencyRepository extends CrudRepository<Currency, String> {
}
由于这使用嵌入式 H2 DB,因此它是集成测试而不是单元测试。我的理解正确吗?
CurrencyRepositoryIntegrationTest.java(方法一)
@RunWith(SpringRunner.class)
@DataJpaTest
public class CurrencyRepositoryIntegrationTest {
@Autowired
private TestEntityManager entityManager;
@Autowired
private CurrencyRepository repository;
@Test
public void testFindByName() {
entityManager.persist(new Currency("USD", "United States Dollar", 2L));
Optional<Currency> c = repository.findById("USD");
assertEquals("United States Dollar", c.get().getCcyNm());
}
}
CurrencyRepositoryIntegrationTest2.java(方法二)
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class)
public class CurrencyRepositoryIntegrationTest2 {
@Autowired
private CurrencyRepository repository;
@Test
public void testFindByName() {
repository.save(new Currency("USD", "United States Dollar", 2L));
Optional<Currency> c = repository.findById("USD");
assertEquals("United States Dollar", c.get().getCcyNm());
}
}
对于集成测试,有句老话“不要嘲笑你不拥有的东西”。
参见例如https://markhneedham.com/blog/2009/12/13/tdd-only-mock-types-you-own/ and https://8thlight.com/blog/eric-smith/2011/10/27/thats-not-yours.html
您将编写的 JUnit 测试将模拟底层 EntityManger 以测试 spring 是否正确实施。这是我们希望 spring 开发人员已经完成的测试,所以我不会重复它。
对于集成测试,我想您不关心存储库如何或是否在下面使用 EntityManager。你只想看看它的行为是否正确。所以第二种方法更合适。
我有一堆关于 Spring Boot
存储库层的问题。
我的问题是:
- 我们是否应该为
Spring Boot
中没有任何自定义代码或查询的存储库层编写单元测试和集成测试? - 在
Spring Boot
中为存储库层编写集成测试的最佳方法是什么?我在下面列出了这两种方法。在这两种方法中,哪一种更好。有没有我应该遵循其中一个的最佳实践? - 如果我对上述问题 #1 的回答是肯定的,那么我该如何在
Spring Boot
中为存储库层编写单元测试?
CurrencyRepository.java
@Repository
public interface CurrencyRepository extends CrudRepository<Currency, String> {
}
由于这使用嵌入式 H2 DB,因此它是集成测试而不是单元测试。我的理解正确吗?
CurrencyRepositoryIntegrationTest.java(方法一)
@RunWith(SpringRunner.class)
@DataJpaTest
public class CurrencyRepositoryIntegrationTest {
@Autowired
private TestEntityManager entityManager;
@Autowired
private CurrencyRepository repository;
@Test
public void testFindByName() {
entityManager.persist(new Currency("USD", "United States Dollar", 2L));
Optional<Currency> c = repository.findById("USD");
assertEquals("United States Dollar", c.get().getCcyNm());
}
}
CurrencyRepositoryIntegrationTest2.java(方法二)
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class)
public class CurrencyRepositoryIntegrationTest2 {
@Autowired
private CurrencyRepository repository;
@Test
public void testFindByName() {
repository.save(new Currency("USD", "United States Dollar", 2L));
Optional<Currency> c = repository.findById("USD");
assertEquals("United States Dollar", c.get().getCcyNm());
}
}
对于集成测试,有句老话“不要嘲笑你不拥有的东西”。 参见例如https://markhneedham.com/blog/2009/12/13/tdd-only-mock-types-you-own/ and https://8thlight.com/blog/eric-smith/2011/10/27/thats-not-yours.html
您将编写的 JUnit 测试将模拟底层 EntityManger 以测试 spring 是否正确实施。这是我们希望 spring 开发人员已经完成的测试,所以我不会重复它。
对于集成测试,我想您不关心存储库如何或是否在下面使用 EntityManager。你只想看看它的行为是否正确。所以第二种方法更合适。