0

I am working on a spring boot project, in my test I want to ensure that the licenseUpdate works. Here are the steps

  1. create a new pen
  2. retrieve the testLicense
  3. update the testLicense with the new pen
  4. retrieve the license for the new pen
  5. assert that it is the same license with the updated pen

At step 5

assertThat(lic).isPresent();

it fails because the findByPenSerial did not find the license. I have debugged it and see that the objects are updated correctly so I guess that it is not written into the test database thats why the findByPenSerial returns nothing. How can I manually store the changes to the database after step 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);
}
Al Phaba
  • 6,545
  • 12
  • 51
  • 83
  • If you are testing persistence your test should interact directly with the repository tier. You can then call saveAndFlush() on the repository, inject. If you were testing services then the repository tier would normally be mocked. See further here on how to flush and clear the persistence context to ensure a meaningful test: https://stackoverflow.com/questions/53621129/hibernate-isolated-integration-tests/53628792#53628792 – Alan Hay Dec 12 '19 at 09:00
  • can you try using LicenseServiceImpl.save(currentLic) instead of licenseService.update(lic.get().getId(), lic.get()); – dassum Dec 12 '19 at 09:00
  • @dassum yes, I already tried this with the same result. – Al Phaba Dec 12 '19 at 09:02
  • share the code for licenseService.findByPenSerial – dassum Dec 12 '19 at 09:04
  • @dassum done, also added the licenseRepo – Al Phaba Dec 12 '19 at 09:09

0 Answers0