I have a few Controller Tests in my Java Spring Boot Application. They basically just check if there is a correct response and if the JSON response is also correct. However, before each test, I want to populate the in memory DB and clear it after each test. I've tried doing it this way, but the clearing of the DB does not seem to work.
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles("test")
@Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:beforeTestRun.sql")
@Sql(executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD, scripts = "classpath:afterTestRun.sql")
class CardControllerTests {
private static final MediaType APPLICATION_JSON_UTF8 = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
private static final String BASE_URL = "/card";
@Autowired
private MockMvc mvc;
@Test
public void getCardsTest() throws Exception {
this.mvc.perform(get(BASE_URL + "/getAll"))
.andExpect(status().isOk())
.andExpect(content().json("[{\"id\":1,\"cardType\":\"WHITE\",\"cardText\":\"Card text\"}," +
"{\"id\":2,\"cardType\":\"BLACK\",\"cardText\":\"Card text\"}]"));
}
@Test
public void getCardTest() throws Exception {
this.mvc.perform(get(BASE_URL + "/get?cardId=1"))
.andExpect(status().isOk())
.andExpect(content().json("{\"id\":1,\"cardType\":\"WHITE\",\"cardText\":\"card-text-for-testing\"}"));
}
@Test
public void addCardTest() throws Exception {
String url = BASE_URL + "/add";
Card card = new Card();
card.setCardType(CardType.WHITE);
card.setCardText("Card text");
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
ObjectWriter objectWriter = objectMapper.writer().withDefaultPrettyPrinter();
String requestJson = objectWriter.writeValueAsString(card);
this.mvc.perform(post(url).contentType(APPLICATION_JSON_UTF8)
.content(requestJson))
.andExpect(status().isOk());
}
}
The getCardTest
method seems to work, but the others get the following error:
Table "CARDS" already exists;
Some more information:
afterTestRun.sql
drop table cards;
beforeTestRun.sql
create table cards (id bigint primary key, card_type integer, card_text varchar(255));
insert into cards (id, card_type, card_text) values (1, 1, 'card-text-for-testing'), (2, 2, 'card-text-for-testing');
application-test.properties
spring.profiles.active=test
spring.jpa.database=h2
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.h2.console.enabled=true
spring.jpa.hibernate.ddl-auto=create-drop
Thanks for any answer :)
Aucun commentaire:
Enregistrer un commentaire