@DataJpaTest无法与@GenericGenerator

时间:2018-10-08 07:06:34

标签: hibernate spring-boot jpa spring-data-jpa spring-boot-test

我尝试使用@DataJpaTest批注测试我的存储库,但出现了一些奇怪的情况。

当我使用经典的@GeneratedValue时,一切正常,我的测试成功了。但是当我使用下面的发电机时,我的测试失败了。

测试createCountry_should_succeed成功了,但其他测试却没有成功,因为没有引发约束条件评估上的异常。

@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "com.example.demojpa.CustomIdentifierGenerator") 

例如,这里是失败的断言之一:

  

java.lang.AssertionError:预期测试会引发org.springframework.dao.DataIntegrityViolationException实例

at org.junit.Assert.fail(Assert.java:88)
at org.junit.rules.ExpectedException.failDueToMissingException(ExpectedException.java:263)
at org.junit.rules.ExpectedException.access$200(ExpectedException.java:106)
at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:245)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

我的实体类

@NoArgsConstructor
@Data
@Entity()
@Table(name = "mw_ecom_country", uniqueConstraints = {@UniqueConstraint(name = "abbreviation", columnNames = "abbreviation")})
public class Country {

    @Id
    @GeneratedValue
    //@GeneratedValue(generator = "UUID")
    //@GenericGenerator(name = "UUID", strategy = "com.example.demojpa.CustomIdentifierGenerator")
    protected Long id;

    @NotNull
    @NotEmpty
    private String abbreviation;

    @NotNull
    private String name;

    public Country(Long id, String abbreviation, String name) {
        this.id = id;
        this.abbreviation = abbreviation;
        this.name = name;
    }
}

这是测试

@DataJpaTest
@RunWith(SpringRunner.class)
public class CountryRepoTest {

    @Autowired
    private CountryRepository countryRepository;

    @Rule
    public ExpectedException exception = ExpectedException.none();

    @Test
    public void createCountry_should_succeed() {
        Country country = countryRepository.save(new Country(null, "FR", "France"));
        assertThat(country.getId(), notNullValue());
    }

    @Test
    public void createCountry_should_failed_duplicate_abbreviation() {
        exception.expect(DataIntegrityViolationException.class);
        countryRepository.save(new Country(null, "FR", "France"));
        countryRepository.save(new Country(null, "FR", "France"));
    }

    @Test
    public void createCountry_should_failed_null_abbreviation() {
        exception.expect(ConstraintViolationException.class);
        countryRepository.save(new Country(null, null, "France"));
    }

    @Test
    public void createCountry_should_failed_empty_abbreviation() {
        exception.expect(ConstraintViolationException.class);
        countryRepository.save(new Country(null, "", "France"));

    }
}

和自定义标识符生成器

public class CustomIdentifierGenerator implements IdentifierGenerator {

    @Override
    public Serializable generate(SessionImplementor session, Object object) throws HibernateException {
        return new Random().nextLong();
    }
}

也许是个错误? https://github.com/spring-projects/spring-boot/issues/14711

1 个答案:

答案 0 :(得分:0)

这很可能是因为您的实体从未写入数据库。

JPA充当后写缓存。 如果不必将更改写入数据库,则会尽可能延迟它。 当ID由数据库生成时,它必须实际执行插入操作才能获得ID。 这将触发异常。 在JVM中生成ID时,插入只会在刷新期间发生。

但是在测试中,事务永远不会提交,而是回滚,因此您永远不会看到异常。

使用JpaRepository.saveAndFlush或将EntityManager注入测试中,并在测试结束时调用flush

另请参阅:JPA cache behaviour when invoke count() method on Spring Data JPA Repository

相关问题