【问题标题】:@DataJpaTest for failure test cases@DataJpaTest 用于失败测试用例
【发布时间】:2019-12-20 19:25:21
【问题描述】:
任何人都可以在@DataJpaTest 上为失败测试用例提供示例代码sn-p 以及预期的异常。 @DataJpaTest 默认应用事务,那么在这种情况下我们需要使用@Transactional(propgation=Propogation.Not_Supported)。请提供一个包含上述所有内容的代码 sn-p 示例。提前谢谢你...
【问题讨论】:
标签:
hibernate
spring-boot
spring-data-jpa
【解决方案1】:
这是一个示例,其中我有一个实体 LearningContent,其中包含我未设置的不可为空的名称字段。
@Test(expected = DataIntegrityViolationException.class)
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void failLearningContentMissingName() {
LearningProgram learningProgram = new LearningProgram();
learningProgram.setBusinessReference(new BusinessReference("hello"));
final BusinessIdentifier bid = BusinessIdentifier.generate();
learningProgram.setBusinessIdentifier(bid);
learningProgram.setName(new Localized("hello.learningprogram"));
LppIntent intent = new LppIntent();
intent.setBusinessReference(new BusinessReference("intent"));
intent.setName(new Localized("intent"));
learningProgram.addIntent(intent);
LppLearningContent learningContent = new LppLearningContent();
learningContent.setBusinessReference(new BusinessReference("learningContent"));
learningContent.setContent("Hello world".getBytes(StandardCharsets.UTF_8));
learningContent.setDuration(5L);
intent.addLearningContent(learningContent);
learningContent.tag("meow");
learningProgramRepository.save(learningProgram);
}