So I am busy with small project for my school and I am writing the test class for my movie controller class (using Spring Data JPA). All endpoints from my controller class are working fine, but when I write a test for my PATCH request with MockMvc I find no result matching my adjusted record. I get status 200 but when trying to assert the record it finds none.
Patch request code from controller class:
@PatchMapping("{id}/ranking")
void changeRanking(@PathVariable long id, @RequestBody @NotNull @Min(0) @Max(10) BigDecimal ranking) {
movieService.changeRanking(id, ranking);
}
Service class code from changeRanking method:
@Transactional
void changeRanking(long id, BigDecimal ranking) {
movieRepository.findAndLockById(id)
.orElseThrow(MovieNietGevondenException::new)
.setRanking(ranking);
}
Code for my test:
@Test
void changeRankingAdjustsAMovieRanking() throws Exception {
long id = idTestMovie();
mockMvc.perform(patch("/movies/{id}/ranking", id)
.contentType(MediaType.APPLICATION_JSON)
.content("7"))
.andExpect(status().isOk());
assertThat(JdbcTestUtils.countRowsInTableWhere(jdbcClient, MOVIES_TABLE, "ranking = 7 and name like 'testMovie' and id = " + id)).isOne();
}
I tried to assert the data from existing record and it passes the test so assertion works fine. When I send HTTP request with intelliJ, it does the changes in database. When i test my endpoint with wrong data i also get the desired status. I tried to change the way of passing Big Decimal as a JSON but it doesnt work either. I have written already multiple test like that with basically the same data and types and never had such a problem. your text
AdamH is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.