Spring boot test @Transactional not saving

后端 未结 5 1387
别那么骄傲
别那么骄傲 2020-12-17 21:24

I\'am trying to do a simple Integration test using Spring Boot Test in order to test the e2e use case. My test does not work because I\'am not able to make the repository sa

5条回答
  •  旧时难觅i
    2020-12-17 22:25

    Do not use @Rollback(false). Unit Test should not generate data.

    JPA FlushMode is AUTO (default - flush INSERT/UPDATE/DELETE SQL when query occurs) / COMMIT.

    Just query the working entity for forcing FLUSH, or using EntityManager to force flush

    @Test
    public void testCreate(){
        InvoiceRange range = service.createInvoiceRange(1, InvoiceRangeCreate.builder()
                .form("01GTKT0/010")
                .serial("NV/18E")
                .effectiveDate(LocalDate.now())
                .rangeFrom(1L)
                .rangeTo(1000L)
                .build(), new byte[] {1,2,3,4,5});
    
        service.findByCriteria(1, "01GTKT0/010", "NV/18E");  // force flush
        // em.flush(); // another way is using entityManager for force flush
    }
    

提交回复
热议问题