java - Spring Boot - Spring Data - Jersey - 在 JUnit 集成测试期间没有存储库的结果
问题描述
在这个问题上挣扎了很长时间,我有一种奇怪的感觉,它与我如何设置 @Transactional 注释有关。
那我想做什么?
- 我正在准备一些数据并将它们与数据库中的可用存储库一起保存。
这可以在我的 FormTest 类中的prepareExampleApplication方法中找到
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class FormTest {
@Autowired
private ApplicationRepository applicationRepository;
@Autowired
private AppActionRepository appActionRepository;
@Autowired
private RoleRepository roleRepository;
@Autowired
private TestRestTemplate restTemplate;
@Autowired
private FormRepository formRepository;
@Autowired
private FormElementRepository formElementRepository;
@Autowired
private SectionRepository sectionRepository;
private Application application;
private InputField refInputField;
private Select refSelectBox;
@Before
public void prepareExampleApplication() {
Form form = formRepository.save(ModelFactory.getForm("Project"));
Application application = ModelFactory.getApplication("Example", form);
this.application = applicationRepository.save(application);
Role role = new Role();
role.setRoleName("ADMIN");
role.setApp(application);
role = roleRepository.save(role);
Section section = ModelFactory.getSection(form, null, null);
section = formElementRepository.save(section);
InputField inputField = ModelFactory.getInputField(form, section, section);
refInputField = formElementRepository.save(inputField);
//once again. Just for my own eyes to see if it is there
Iterable<Form> all = formRepository.findAll();
// lot more stuff
}
@Test
@Transactional
public void testUserInput() {
// first create a new container to give inouts
Long id = this.application.getEntity().getId();
// for the sake of debugging I am using the formRepo to really SEARCH for the persisted form AND IT IS THERE!!!
Form byId = formRepository.findById(id).orElseThrow(NotFoundException::new);
URI uri = this.restTemplate.postForLocation("/api/form/" + id + "/forminstance", null);
long containerId = TestHelper.extractId(uri);
}
}
拥有这些数据的下一件事是使用 restTemplate 并将发布请求发送到 REST 服务。您可以在测试方法中找到 POST 调用。出于调试原因 - 并看到 repo 正在工作 - 我真的使用存储库来获取表单的 id,而不是使用已填充准备方法的类字段。并且表格将被退回!
在我的休息服务中,我再次使用 formRepository,寻找我之前在测试类中找到的实体。但是这次存储库没有返回任何东西。只有空。我已经尝试了很多不同的事情,在不同的位置设置@Transactional,但是无论我做什么,REST服务中的formRepository都只会给我0个实体和一个null。这是 REST 服务
@Service @Api("FormService") @Path("/form") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public class FormService { @Autowired private FormInstanceRepository formInstanceRepository; @Autowired private FormRepository formRepository; @Autowired private FormElementRepository formElementRepository; @Autowired private UserInputRepository userInputRepository; @POST @Path("{id}/forminstance") @Transactional public Response createFormInstance(@Context UriInfo info, @PathParam("id") long formId) { // returns ALWAYS 0 elements Iterable<Form> all = formRepository.findAll(); // returns always null Form form = formRepository.findById(formId).orElse(null); FormInstance formInstance = new FormInstance(); formInstance.setForm(form); FormInstance save = formInstanceRepository.save(formInstance); UriBuilder builder = info.getAbsolutePathBuilder(); builder.path(Long.toString(save.getId())); return Response.created(builder.build()).build(); }
如果您知道答案,我对解释我的错误非常感兴趣。我正在使用内存中的 H2 db 进行测试。
也添加 Form 实体和 FormRepository
@Entity
@Data
public class Form {
@Id
@GeneratedValue
private Long id;
private String name;
}
-
public interface FormRepository extends CrudRepository<Form, Long> {
}
在此先感谢您的帮助!!
解决方案
推荐阅读
- html - 可滚动的弹性项目 - 如何让宽度等于内容
- typescript - Typescript 使用派生类作为方法的返回类型
- ruby-on-rails - Ruby on rails:如何从机架中间件身份验证中排除某些路径?
- laravel - 如何在 Laravel 中为用户显示相应的登录页面?
- postgresql - “QueryFailedError:整数的输入语法无效:”查询浮点类型时
- azure-devops - 解决 UserStory 时自动化,将视频从附件上传到 Yammer 作为新帖子作为附件。美国的标题将是内容
- html - 找不到未禁用的字段“player_1_name”
- entity-framework - 将 SQL 命令转换为 lambda 表达式
- javascript - 二进制数据文件下载 JavaScript 中的任何扩展
- azure - 提醒我如何从函数应用程序向销售人员对象发送数据?