java - @DirtiesContext 不清除数据库
问题描述
我已经尝试了以下步骤,但遗憾的是没有运气:
方法号 1 总是失败,因为大小预期为 1,但它的 2 -> 测试单独运行成功通过,但是当我运行所有应用程序测试时,正是那个失败
1)用标记每个方法 @DirtiesContext(methodMode = DirtiesContext.MethodMode.BEFORE_METHOD)
2)在课堂上标记@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
3)标记方法@Transactional
@SpringBootTest
public class ExportServiceTest {
@Autowired
ExportService exportService;
@Autowired
ExportRepository exportRepository;
@Autowired
UserRepository userRepository;
@Test
public void onlyOneExportUserShouldBeAddedWithMultipleEntries() {
UserEntity userEntity = new UserEntity().setId(2L);
exportService.createIfNotExist(userEntity);
exportService.createIfNotExist(userEntity);
Assertions.assertThat(exportRepository.findAll()).hasSize(1);
}
@Test
public void exportUserShouldBeAdded() {
UserEntity userEntity = new UserEntity().setId(1L);
exportService.createIfNotExist(userEntity);
Optional<UserExportEntity> exportUser = exportRepository.findByUserEntityId(userEntity.getId());
Assertions.assertThat(exportUser).isPresent();
}
@Test
public void shouldHaveTwoRecordsInExport() {
UserEntity userEntity = new UserEntity().setId(1L).setEmail("test@test.com");
UserEntity userEntity2 = new UserEntity().setId(2L).setEmail("test1@test.com");
userRepository.save(userEntity);
userRepository.save(userEntity2);
exportService.createIfNotExist(userEntity2);
exportService.createIfNotExist(userEntity);
Assertions.assertThat(exportRepository.findAll()).hasSize(2);
}
}
解决方案
推荐阅读
- mysql - 编码未正确呈现
- c# - 在 Blazor 中移动子元素时防止 onmouseout 触发
- cuda - 尝试使用超过 48 KB 的(静态)共享内存时出错
- javascript - 在 javascript 中访问 json 数据
- javascript - 烧瓶简单密码保护页面加载
- computer-science - “ -> ab|ac”的扩展 BNF 是什么?
- python - numpy 如何实现将列表作为索引传递给获取和设置项目的功能?
- mysql - 在自定义运算符上使用 dagrun.conf
- php - App\Mail\EmailGenerator 类的对象无法转换为字符串
- javascript - 为什么我的控制器从 Axios 收到的值总是为空?