hibernate - @DataJpaTest 用于失败测试用例
问题描述
任何人都可以在@DataJpaTest 上为失败测试用例提供示例代码片段以及预期的异常。@DataJpaTest 默认应用事务,在这种情况下我们需要使用@Transactional(propgation=Propogation.Not_Supported)。请提供一个包含以上所有内容的代码片段示例。提前谢谢你...
解决方案
这是一个示例,其中我有一个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);
}
推荐阅读
- javascript - 如何通过代码更改贝宝按钮上的重定向页面
- c++ - GTK+ 中的小方形按钮
- python - 在 Pycharm 中不显示 Arcpy 模数
- php - Laravel 5.8:__construct() 必须是整数类型,给定数组
- elasticsearch - Elasticsearch 查询具有多个值的字段
- javascript - 为什么我不能输入输入而不是按下按钮?
- bash - 如何使用 BASH 返回目录中多个文件的 MD5 和 SHA1 值
- java - 如果所有周期对象都是连续周期,如何使用 java 8 (LAMBDA) 检查集合
- azure - 如何在 Azure 服务总线上转发过期消息
- linux - 如何使我的 bash 脚本在下载时自动变成终端命令?