Spring boot test @Transactional not saving

后端 未结 5 1381
别那么骄傲
别那么骄傲 2020-12-17 21:24

I\'am trying to do a simple Integration test using Spring Boot Test in order to test the e2e use case. My test does not work because I\'am not able to make the repository sa

5条回答
  •  感情败类
    2020-12-17 22:13

    For each @Test function that makes a DB transaction, if you want to permanently persist the changes, then you can use @Rollback(false)

    @Rollback(false)
    @Test
    public void createPerson() throws Exception {
        int databaseSizeBeforeCreate = personRepository.findAll().size();
    
        // Create the Person
        restPersonMockMvc.perform(post("/api/people")
            .contentType(TestUtil.APPLICATION_JSON_UTF8)
            .content(TestUtil.convertObjectToJsonBytes(person)))
            .andExpect(status().isCreated());
    
        // Validate the Person in the database
        List personList = personRepository.findAll();
        assertThat(personList).hasSize(databaseSizeBeforeCreate + 1);
        Person testPerson = personList.get(personList.size() - 1);
        assertThat(testPerson.getFirstName()).isEqualTo(DEFAULT_FIRST_NAME);
        assertThat(testPerson.getLastName()).isEqualTo(DEFAULT_LAST_NAME);
        assertThat(testPerson.getAge()).isEqualTo(DEFAULT_AGE);
        assertThat(testPerson.getCity()).isEqualTo(DEFAULT_CITY);
    }
    

    I tested it with a SpringBoot project generated by jHipster:

    • SpringBoot: 1.5.4
    • jUnit 4.12
    • Spring 4.3.9

提交回复
热议问题