Java 如何在SpringBootTest案例中手动将数据刷新到数据库中?

Java 如何在SpringBootTest案例中手动将数据刷新到数据库中?,java,spring-boot,Java,Spring Boot,我正在从事一个spring启动项目,在我的测试中,我希望确保licenseUpdate能够正常工作。以下是步骤 创建一支新钢笔 检索testLicense 使用新笔更新testLicense 检索新笔的许可证 断言它与更新的笔是同一许可证 在步骤5 assertThat(lic).isPresent(); 它失败,因为findByPenSerial未找到许可证。我已经对它进行了调试,并看到对象已正确更新,所以我猜它没有写入测试数据库 这就是findByPenSerial不返回任何内容的原因。在

我正在从事一个spring启动项目,在我的测试中,我希望确保licenseUpdate能够正常工作。以下是步骤

  • 创建一支新钢笔
  • 检索testLicense
  • 使用新笔更新testLicense
  • 检索新笔的许可证
  • 断言它与更新的笔是同一许可证
  • 在步骤5

    assertThat(lic).isPresent();
    
    它失败,因为findByPenSerial未找到许可证。我已经对它进行了调试,并看到对象已正确更新,所以我猜它没有写入测试数据库 这就是findByPenSerial不返回任何内容的原因。在步骤4之后,如何手动将更改存储到数据库

        @ActiveProfiles("test")
        @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.AUTO_CONFIGURED)
        @Transactional
        @SpringBootTest
        public class FooTest extends BaseTests {
    
            //...
    
            @Test
            public void whenUpdateLicenseToNewPen(){
                //create a new pen
    
                String penSerial = "foo";
                Optional<PenEntity> newPen = penService.saveBySerial(penSerial);
                assertThat(newPen).isPresent();
    
                Optional<LicenseEntity> lic = licenseService.findByPenSerial(testPen.getSerial());
                LicenseEntity currentLic = lic.get();
    
                //setting license to another pen
                currentLic.setPen(newPen.get());
                //save the license
                licenseService.update(lic.get().getId(), lic.get());
    
    
                //find the license now under new serial
                lic = licenseService.findByPenSerial(penSerial);
                assertThat(lic).isPresent();
                assertThat(lic.get().getId()).isEqualTo(currentLic.getId());
    
            }
    
        }
    
    
    
        @Service
        @AllArgsConstructor
        public class LicenseServiceImpl
                extends Base<LicenseEntity>
                implements LicenseService {
    
    
            //....
    
            @Override
            public Optional<LicenseEntity> save(LicenseEntity entity) {
                LOG.debug("creating license for pen {0}", entity.getPen().getSerial());
                LicenseEntity lic = licenseRepository.save(entity);
                return Optional.ofNullable(lic);
            }
    
            @Override
            public Optional<LicenseEntity> update(Long id, LicenseEntity entity) {
                Optional<LicenseEntity> licToUpdate = licenseRepository.findById(id);
                if (licToUpdate.isPresent()){
                    LicenseEntity currentLic = licToUpdate.get();
                    BaseBeanUtils.copyPropertiesSkipNulls(entity, currentLic);
                    LicenseEntity updatedLic = licenseRepository.save(currentLic);
                    return Optional.ofNullable(updatedLic);
                }
    
                return Optional.empty();
            }
    
     @Override
       public Optional<LicenseEntity> findByPenSerial(String penSerial) {
          LicenseEntity license = licenseRepository.findByPenSerial(penSerial);
          if (license == null){
             return Optional.empty();
          }
           return Optional.of(license);
    
        }
    
    
    
    
     @Repository("LicenseRepository")
        public interface LicenseRepository
                extends JpaRepository<LicenseEntity, Long>, 
        JpaSpecificationExecutor<LicenseEntity> {
    
        LicenseEntity findByPenSerial(String serial);
        LicenseEntity findByPenPenId(Long penId);
    }
    
    @ActiveProfiles(“测试”)
    @AutoConfigureTestDatabase(replace=AutoConfigureTestDatabase.replace.AUTO\u配置)
    @交易的
    @春靴测试
    公共类FooTest扩展了BaseTests{
    //...
    @试验
    当更新数据许可证更新时公共无效(){
    //创建一支新钢笔
    字符串penSerial=“foo”;
    可选的newPen=penService.saveBySerial(penSerial);
    assertThat(newPen.isPresent();
    可选lic=licenseService.findByPenSerial(testPen.getSerial());
    LicenseEntity currentLic=lic.get();
    //将许可证设置为其他笔
    currentLic.setPen(newPen.get());
    //保存许可证
    licenseService.update(lic.get().getId(),lic.get());
    //现在在“新序列号”下查找许可证
    lic=licenseService.findByPenSerial(penSerial);
    断言(lic).isPresent();
    断言(lic.get().getId()).isEqualTo(currentLic.getId());
    }
    }
    @服务
    @AllArgsConstructor
    公共类LicenseServiceImpl
    扩底
    实施许可证服务{
    //....
    @凌驾
    公共可选保存(LicenseEntity){
    debug(“为钢笔{0}创建许可证”,entity.getPen().getSerial());
    LicenseEntity lic=licenseRepository.save(实体);
    返回可选。不可用(lic);
    }
    @凌驾
    公共可选更新(长id,LicenseEntity){
    可选licToUpdate=licenseRepository.findById(id);
    if(licToUpdate.isPresent()){
    LicenseEntity currentLic=licToUpdate.get();
    BaseBeanUtils.copyPropertiesSkipNulls(实体,currentLic);
    LicenseEntity updatedLic=licenseRepository.save(currentLic);
    返回可选的.ofNullable(updatedLic);
    }
    返回可选的.empty();
    }
    @凌驾
    公共可选findByPenSerial(字符串penSerial){
    LicenseEntity license=licenseRepository.findBypenseria(penSerial);
    如果(许可证==null){
    返回可选的.empty();
    }
    返回可选。of(许可证);
    }
    @存储库(“许可证存储库”)
    公共接口许可证存储库
    这是一个假设,
    JpaSpecificationExecutor{
    LicenseEntity findByPenSerial(字符串序列);
    被许可方实体findByPenPenId(长penId);
    }
    
    如果您正在测试持久性,您的测试应该直接与存储库层交互。然后您可以调用saveAndFlush()在存储库上,注入。如果您正在测试服务,则存储库层通常会被模拟。请参阅此处有关如何刷新和清除持久性上下文以确保进行有意义的测试的详细信息:是否可以尝试使用LicenseServiceImpl.save(currentLic)而不是licenseService.update(lic.get().getId(),lic.get());@dassum是的,我已经尝试过了,但结果相同。请共享licenseService的代码。findByPenSerial@dassum完成后,还添加了licenseRepo