Spring Boot-Spring Data-Jersey-JUnit集成测试期间没有存储库结果

时间:2018-09-05 19:21:12

标签: java spring spring-boot jpa spring-data

在该问题上苦苦挣扎了很长时间,我感到奇怪的是,它与我如何设置@Transactional注释有关。

那我想怎么办?

  1. 我正在准备一些数据,并将它们与数据库中的可用存储库一起保存。

这可以在我的FormTest类的 prepareExampleApplication 方法中找到

LetMeIn<A>
  1. 我接下来要获取的数据是使用restTemplate并向REST服务调度发布请求。您可以在测试方法中找到POST调用。出于调试的原因-并查看仓库正在运行-我实际上是在使用存储库来获取表单的ID,而不是使用准备方法填充的类字段。并且表单将被退回!

  2. 在我的REST服务中,我再次使用formRepository,以查找之前在测试类中找到的实体。但是这次仓库没有返回任何东西。仅空。我在不同的位置设置@Transactional尝试了很多不同的事情,但是我在REST服务中执行formRepository所做的任何事情只会给我0个实体和null。这是REST服务

    @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);
    
    
        }
    }
    

    如果您知道答案,那么我对解释理解我的错误非常感兴趣。我正在使用内存中的H2数据库进行测试。

也添加了Form实体和FormRepository

@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();

    }

-

    @Entity
    @Data
    public class Form {

        @Id
        @GeneratedValue
        private Long id;

        private String name;

    }

提前感谢您的帮助!

0 个答案:

没有答案