Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我们应该在SpringBoot中为存储库层编写单元测试和集成测试吗?_Java_Spring_Unit Testing_Spring Boot_Spring Boot Test - Fatal编程技术网

Java 我们应该在SpringBoot中为存储库层编写单元测试和集成测试吗?

Java 我们应该在SpringBoot中为存储库层编写单元测试和集成测试吗?,java,spring,unit-testing,spring-boot,spring-boot-test,Java,Spring,Unit Testing,Spring Boot,Spring Boot Test,我有一大堆关于springboot存储库层的问题 我的问题是: 我们是否应该在springboot中为没有任何自定义代码或查询的存储库层编写单元测试和集成测试 在springboot中为存储库层编写集成测试的最佳方法是什么?我在下面列出了这两种方法。在这两种方法中,哪一种更好。有什么最佳实践是我应该遵循的吗 如果我上面问题1的答案是肯定的,那么如何在Spring Boot中为存储库层编写单元测试 CurrencyRepository.java @Repository public interfa

我有一大堆关于
springboot
存储库层的问题

我的问题是:

  • 我们是否应该在
    springboot
    中为没有任何自定义代码或查询的存储库层编写单元测试和集成测试
  • springboot
    中为存储库层编写集成测试的最佳方法是什么?我在下面列出了这两种方法。在这两种方法中,哪一种更好。有什么最佳实践是我应该遵循的吗
  • 如果我上面问题1的答案是肯定的,那么如何在
    Spring Boot
    中为存储库层编写单元测试
  • CurrencyRepository.java

    @Repository
    public interface CurrencyRepository extends CrudRepository<Currency, String> {
    
    }
    
    @存储库
    公共接口CurrencyRepository扩展了Crudepository{
    }
    
    因为它使用嵌入式H2 DB,所以它是一个集成测试,而不是单元测试。我的理解正确吗

    CurrencyRepositoryIntegrationTest.java(方法1)

    @RunWith(SpringRunner.class)
    @DataJpaTest
    公共类CurrencyRepositoryIntegrationTest{
    @自动连线
    私人测试管理者实体管理者;
    @自动连线
    专用存储库;
    @试验
    public void testFindByName(){
    entityManager.persist(新货币(“美元”,“美元”,2L));
    可选c=repository.findById(“USD”);
    资产质量(“美元”,c.get().getCcyNm());
    }
    }
    
    CurrencyRepositoryIntegrationTest2.java(方法2)

    @RunWith(SpringRunner.class)
    @SpringBootTest(类=DemoApplication.class)
    公共类CurrencyRepositoryIntegrationTest2{
    @自动连线
    专用存储库;
    @试验
    public void testFindByName(){
    存储库.保存(新货币(“美元”,“美元”,2L));
    可选c=repository.findById(“USD”);
    资产质量(“美元”,c.get().getCcyNm());
    }
    }
    
    对于集成测试,有句老话“不要嘲笑你不拥有的东西”。 参见例如和

    您将编写的JUnit测试将模拟底层EntityManger,以测试spring是否正确实现。这是一个我们希望spring开发人员已经完成的测试,所以我不会重复


    对于集成测试,我想您不关心存储库如何或是否在下面使用EntityManager。你只想看看它的行为是否正确。因此,第二种方法更适合。

    我个人认为没有理由对我没有编写的具体类进行单元测试,但我确实为我的存储库层实施了集成测试,以验证查询方法是否产生了预期的结果。
    @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());
        }
    
    }
    
    @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());
        }
    
    }