diff --git a/pom.xml b/pom.xml index 138695756..0e77ff4d6 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.samples spring-petclinic-rest - 3.2.1 + 3.4.0 REST version of the Spring Petclinic sample application https://spring-petclinic.github.io/ @@ -13,14 +13,14 @@ org.springframework.boot spring-boot-starter-parent - 3.3.5 + 3.4.0 1.2.1.RELEASE - 2.6.0 + 2.7.0 0.2.6 1.6.3 2.3.1 diff --git a/src/main/java/org/springframework/samples/petclinic/model/Pet.java b/src/main/java/org/springframework/samples/petclinic/model/Pet.java index f5d3402fd..e198b988a 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/Pet.java +++ b/src/main/java/org/springframework/samples/petclinic/model/Pet.java @@ -38,7 +38,7 @@ public class Pet extends NamedEntity { @Column(name = "birth_date", columnDefinition = "DATE") private LocalDate birthDate; - @ManyToOne + @ManyToOne(cascade = CascadeType.ALL) @JoinColumn(name = "type_id") private PetType type; diff --git a/src/main/java/org/springframework/samples/petclinic/rest/controller/OwnerRestController.java b/src/main/java/org/springframework/samples/petclinic/rest/controller/OwnerRestController.java index 906f8f546..631fb4034 100644 --- a/src/main/java/org/springframework/samples/petclinic/rest/controller/OwnerRestController.java +++ b/src/main/java/org/springframework/samples/petclinic/rest/controller/OwnerRestController.java @@ -139,6 +139,7 @@ public ResponseEntity addPetToOwner(Integer ownerId, PetFieldsDto petFie Owner owner = new Owner(); owner.setId(ownerId); pet.setOwner(owner); + pet.getType().setName(null); this.clinicService.savePet(pet); PetDto petDto = petMapper.toPetDto(pet); headers.setLocation(UriComponentsBuilder.newInstance().path("/api/pets/{id}") diff --git a/src/main/java/org/springframework/samples/petclinic/rest/controller/VetRestController.java b/src/main/java/org/springframework/samples/petclinic/rest/controller/VetRestController.java index 531dede5e..27219be93 100644 --- a/src/main/java/org/springframework/samples/petclinic/rest/controller/VetRestController.java +++ b/src/main/java/org/springframework/samples/petclinic/rest/controller/VetRestController.java @@ -56,8 +56,7 @@ public VetRestController(ClinicService clinicService, VetMapper vetMapper, Speci @PreAuthorize("hasRole(@roles.VET_ADMIN)") @Override public ResponseEntity> listVets() { - List vets = new ArrayList<>(); - vets.addAll(vetMapper.toVetDtos(this.clinicService.findAllVets())); + List vets = new ArrayList<>(vetMapper.toVetDtos(this.clinicService.findAllVets())); if (vets.isEmpty()) { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } diff --git a/src/main/java/org/springframework/samples/petclinic/service/ClinicServiceImpl.java b/src/main/java/org/springframework/samples/petclinic/service/ClinicServiceImpl.java index baf81184e..2e82cb15e 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/ClinicServiceImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/service/ClinicServiceImpl.java @@ -197,6 +197,7 @@ public Pet findPetById(int id) throws DataAccessException { @Override @Transactional public void savePet(Pet pet) throws DataAccessException { + pet.setType(findPetTypeById(pet.getType().getId())); petRepository.save(pet); } diff --git a/src/test/java/org/springframework/samples/petclinic/rest/controller/OwnerRestControllerTests.java b/src/test/java/org/springframework/samples/petclinic/rest/controller/OwnerRestControllerTests.java index c45612893..1ac443ce1 100644 --- a/src/test/java/org/springframework/samples/petclinic/rest/controller/OwnerRestControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/rest/controller/OwnerRestControllerTests.java @@ -23,7 +23,6 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; import org.springframework.samples.petclinic.mapper.OwnerMapper; import org.springframework.samples.petclinic.mapper.PetMapper; @@ -38,6 +37,7 @@ import org.springframework.samples.petclinic.service.clinicService.ApplicationTestConfig; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.bean.override.mockito.MockitoBean; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.result.MockMvcResultHandlers; @@ -75,7 +75,7 @@ class OwnerRestControllerTests { @Autowired private VisitMapper visitMapper; - @MockBean + @MockitoBean private ClinicService clinicService; private MockMvc mockMvc; @@ -189,7 +189,7 @@ void testGetOwnersListSuccess() throws Exception { void testGetOwnersListNotFound() throws Exception { owners.clear(); given(this.clinicService.findOwnerByLastName("0")).willReturn(ownerMapper.toOwners(owners)); - this.mockMvc.perform(get("/api/owners/?lastName=0") + this.mockMvc.perform(get("/api/owners?lastName=0") .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isNotFound()); } @@ -200,7 +200,7 @@ void testGetAllOwnersSuccess() throws Exception { owners.remove(0); owners.remove(1); given(this.clinicService.findAllOwners()).willReturn(ownerMapper.toOwners(owners)); - this.mockMvc.perform(get("/api/owners/") + this.mockMvc.perform(get("/api/owners") .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().contentType("application/json")) @@ -215,7 +215,7 @@ void testGetAllOwnersSuccess() throws Exception { void testGetAllOwnersNotFound() throws Exception { owners.clear(); given(this.clinicService.findAllOwners()).willReturn(ownerMapper.toOwners(owners)); - this.mockMvc.perform(get("/api/owners/") + this.mockMvc.perform(get("/api/owners") .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isNotFound()); } @@ -229,7 +229,7 @@ void testCreateOwnerSuccess() throws Exception { mapper.registerModule(new JavaTimeModule()); mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); String newOwnerAsJSON = mapper.writeValueAsString(newOwnerDto); - this.mockMvc.perform(post("/api/owners/") + this.mockMvc.perform(post("/api/owners") .content(newOwnerAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isCreated()); } @@ -244,7 +244,7 @@ void testCreateOwnerError() throws Exception { mapper.registerModule(new JavaTimeModule()); mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); String newOwnerAsJSON = mapper.writeValueAsString(newOwnerDto); - this.mockMvc.perform(post("/api/owners/") + this.mockMvc.perform(post("/api/owners") .content(newOwnerAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isBadRequest()); } @@ -359,7 +359,7 @@ void testCreatePetSuccess() throws Exception { mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); String newPetAsJSON = mapper.writeValueAsString(newPet); System.err.println("--> newPetAsJSON=" + newPetAsJSON); - this.mockMvc.perform(post("/api/owners/1/pets/") + this.mockMvc.perform(post("/api/owners/1/pets") .content(newPetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isCreated()); } @@ -375,7 +375,7 @@ void testCreatePetError() throws Exception { mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); mapper.registerModule(new JavaTimeModule()); String newPetAsJSON = mapper.writeValueAsString(newPet); - this.mockMvc.perform(post("/api/owners/1/pets/") + this.mockMvc.perform(post("/api/owners/1/pets") .content(newPetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isBadRequest()).andDo(MockMvcResultHandlers.print()); } diff --git a/src/test/java/org/springframework/samples/petclinic/rest/controller/PetRestControllerTests.java b/src/test/java/org/springframework/samples/petclinic/rest/controller/PetRestControllerTests.java index c6029feb0..12711e211 100644 --- a/src/test/java/org/springframework/samples/petclinic/rest/controller/PetRestControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/rest/controller/PetRestControllerTests.java @@ -23,7 +23,6 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.samples.petclinic.mapper.PetMapper; @@ -36,6 +35,7 @@ import org.springframework.samples.petclinic.service.clinicService.ApplicationTestConfig; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.bean.override.mockito.MockitoBean; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; @@ -63,7 +63,7 @@ @WebAppConfiguration class PetRestControllerTests { - @MockBean + @MockitoBean protected ClinicService clinicService; @Autowired private PetRestController petRestController; @@ -132,7 +132,7 @@ void testGetAllPetsSuccess() throws Exception { System.err.println(pets); when(this.clinicService.findAllPets()).thenReturn(pets); //given(this.clinicService.findAllPets()).willReturn(petMapper.toPets(pets)); - this.mockMvc.perform(get("/api/pets/") + this.mockMvc.perform(get("/api/pets") .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().contentType("application/json")) @@ -147,7 +147,7 @@ void testGetAllPetsSuccess() throws Exception { void testGetAllPetsNotFound() throws Exception { pets.clear(); given(this.clinicService.findAllPets()).willReturn(petMapper.toPets(pets)); - this.mockMvc.perform(get("/api/pets/") + this.mockMvc.perform(get("/api/pets") .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isNotFound()); } diff --git a/src/test/java/org/springframework/samples/petclinic/rest/controller/PetTypeRestControllerTests.java b/src/test/java/org/springframework/samples/petclinic/rest/controller/PetTypeRestControllerTests.java index 2e21d73ae..758f840f0 100644 --- a/src/test/java/org/springframework/samples/petclinic/rest/controller/PetTypeRestControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/rest/controller/PetTypeRestControllerTests.java @@ -21,7 +21,6 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; import org.springframework.samples.petclinic.mapper.PetTypeMapper; import org.springframework.samples.petclinic.model.PetType; @@ -30,6 +29,7 @@ import org.springframework.samples.petclinic.service.clinicService.ApplicationTestConfig; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.bean.override.mockito.MockitoBean; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; @@ -58,7 +58,7 @@ class PetTypeRestControllerTests { @Autowired private PetTypeMapper petTypeMapper; - @MockBean + @MockitoBean private ClinicService clinicService; private MockMvc mockMvc; @@ -132,7 +132,7 @@ void testGetAllPetTypesSuccessAsOwnerAdmin() throws Exception { petTypes.remove(0); petTypes.remove(1); given(this.clinicService.findAllPetTypes()).willReturn(petTypes); - this.mockMvc.perform(get("/api/pettypes/") + this.mockMvc.perform(get("/api/pettypes") .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().contentType("application/json")) @@ -148,7 +148,7 @@ void testGetAllPetTypesSuccessAsVetAdmin() throws Exception { petTypes.remove(0); petTypes.remove(1); given(this.clinicService.findAllPetTypes()).willReturn(petTypes); - this.mockMvc.perform(get("/api/pettypes/") + this.mockMvc.perform(get("/api/pettypes") .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().contentType("application/json")) @@ -163,7 +163,7 @@ void testGetAllPetTypesSuccessAsVetAdmin() throws Exception { void testGetAllPetTypesNotFound() throws Exception { petTypes.clear(); given(this.clinicService.findAllPetTypes()).willReturn(petTypes); - this.mockMvc.perform(get("/api/pettypes/") + this.mockMvc.perform(get("/api/pettypes") .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isNotFound()); } @@ -175,7 +175,7 @@ void testCreatePetTypeSuccess() throws Exception { newPetType.setId(null); ObjectMapper mapper = new ObjectMapper(); String newPetTypeAsJSON = mapper.writeValueAsString(petTypeMapper.toPetTypeFieldsDto(newPetType)); - this.mockMvc.perform(post("/api/pettypes/") + this.mockMvc.perform(post("/api/pettypes") .content(newPetTypeAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isCreated()); } @@ -188,7 +188,7 @@ void testCreatePetTypeError() throws Exception { newPetType.setName(null); ObjectMapper mapper = new ObjectMapper(); String newPetTypeAsJSON = mapper.writeValueAsString(petTypeMapper.toPetTypeDto(newPetType)); - this.mockMvc.perform(post("/api/pettypes/") + this.mockMvc.perform(post("/api/pettypes") .content(newPetTypeAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isBadRequest()); } diff --git a/src/test/java/org/springframework/samples/petclinic/rest/controller/SpecialtyRestControllerTests.java b/src/test/java/org/springframework/samples/petclinic/rest/controller/SpecialtyRestControllerTests.java index 7f1ea1141..f836e16d7 100644 --- a/src/test/java/org/springframework/samples/petclinic/rest/controller/SpecialtyRestControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/rest/controller/SpecialtyRestControllerTests.java @@ -21,16 +21,15 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; import org.springframework.samples.petclinic.mapper.SpecialtyMapper; import org.springframework.samples.petclinic.model.Specialty; import org.springframework.samples.petclinic.rest.advice.ExceptionControllerAdvice; -import org.springframework.samples.petclinic.rest.controller.SpecialtyRestController; import org.springframework.samples.petclinic.service.ClinicService; import org.springframework.samples.petclinic.service.clinicService.ApplicationTestConfig; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.bean.override.mockito.MockitoBean; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; @@ -58,7 +57,7 @@ class SpecialtyRestControllerTests { @Autowired private SpecialtyMapper specialtyMapper; - @MockBean + @MockitoBean private ClinicService clinicService; private MockMvc mockMvc; @@ -115,7 +114,7 @@ void testGetSpecialtyNotFound() throws Exception { void testGetAllSpecialtysSuccess() throws Exception { specialties.remove(0); given(this.clinicService.findAllSpecialties()).willReturn(specialties); - this.mockMvc.perform(get("/api/specialties/") + this.mockMvc.perform(get("/api/specialties") .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().contentType("application/json")) @@ -130,7 +129,7 @@ void testGetAllSpecialtysSuccess() throws Exception { void testGetAllSpecialtysNotFound() throws Exception { specialties.clear(); given(this.clinicService.findAllSpecialties()).willReturn(specialties); - this.mockMvc.perform(get("/api/specialties/") + this.mockMvc.perform(get("/api/specialties") .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isNotFound()); } @@ -142,7 +141,7 @@ void testCreateSpecialtySuccess() throws Exception { newSpecialty.setId(999); ObjectMapper mapper = new ObjectMapper(); String newSpecialtyAsJSON = mapper.writeValueAsString(specialtyMapper.toSpecialtyDto(newSpecialty)); - this.mockMvc.perform(post("/api/specialties/") + this.mockMvc.perform(post("/api/specialties") .content(newSpecialtyAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isCreated()); } @@ -155,7 +154,7 @@ void testCreateSpecialtyError() throws Exception { newSpecialty.setName(null); ObjectMapper mapper = new ObjectMapper(); String newSpecialtyAsJSON = mapper.writeValueAsString(specialtyMapper.toSpecialtyDto(newSpecialty)); - this.mockMvc.perform(post("/api/specialties/") + this.mockMvc.perform(post("/api/specialties") .content(newSpecialtyAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isBadRequest()); } diff --git a/src/test/java/org/springframework/samples/petclinic/rest/controller/UserRestControllerTests.java b/src/test/java/org/springframework/samples/petclinic/rest/controller/UserRestControllerTests.java index ed65ee184..3033aa3c2 100644 --- a/src/test/java/org/springframework/samples/petclinic/rest/controller/UserRestControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/rest/controller/UserRestControllerTests.java @@ -54,7 +54,7 @@ void testCreateUserSuccess() throws Exception { user.addRole("OWNER_ADMIN"); ObjectMapper mapper = new ObjectMapper(); String newVetAsJSON = mapper.writeValueAsString(userMapper.toUserDto(user)); - this.mockMvc.perform(post("/api/users/") + this.mockMvc.perform(post("/api/users") .content(newVetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isCreated()); } @@ -68,7 +68,7 @@ void testCreateUserError() throws Exception { user.setEnabled(true); ObjectMapper mapper = new ObjectMapper(); String newVetAsJSON = mapper.writeValueAsString(userMapper.toUserDto(user)); - this.mockMvc.perform(post("/api/users/") + this.mockMvc.perform(post("/api/users") .content(newVetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isBadRequest()); } diff --git a/src/test/java/org/springframework/samples/petclinic/rest/controller/VetRestControllerTests.java b/src/test/java/org/springframework/samples/petclinic/rest/controller/VetRestControllerTests.java index 5b5709783..35db59b67 100644 --- a/src/test/java/org/springframework/samples/petclinic/rest/controller/VetRestControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/rest/controller/VetRestControllerTests.java @@ -21,16 +21,15 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; import org.springframework.samples.petclinic.mapper.VetMapper; import org.springframework.samples.petclinic.model.Vet; import org.springframework.samples.petclinic.rest.advice.ExceptionControllerAdvice; -import org.springframework.samples.petclinic.rest.controller.VetRestController; import org.springframework.samples.petclinic.service.ClinicService; import org.springframework.samples.petclinic.service.clinicService.ApplicationTestConfig; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.bean.override.mockito.MockitoBean; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; @@ -58,7 +57,7 @@ class VetRestControllerTests { @Autowired private VetMapper vetMapper; - @MockBean + @MockitoBean private ClinicService clinicService; private MockMvc mockMvc; @@ -117,7 +116,7 @@ void testGetVetNotFound() throws Exception { @WithMockUser(roles="VET_ADMIN") void testGetAllVetsSuccess() throws Exception { given(this.clinicService.findAllVets()).willReturn(vets); - this.mockMvc.perform(get("/api/vets/") + this.mockMvc.perform(get("/api/vets") .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().contentType("application/json")) @@ -132,7 +131,7 @@ void testGetAllVetsSuccess() throws Exception { void testGetAllVetsNotFound() throws Exception { vets.clear(); given(this.clinicService.findAllVets()).willReturn(vets); - this.mockMvc.perform(get("/api/vets/") + this.mockMvc.perform(get("/api/vets") .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isNotFound()); } @@ -144,7 +143,7 @@ void testCreateVetSuccess() throws Exception { newVet.setId(999); ObjectMapper mapper = new ObjectMapper(); String newVetAsJSON = mapper.writeValueAsString(vetMapper.toVetDto(newVet)); - this.mockMvc.perform(post("/api/vets/") + this.mockMvc.perform(post("/api/vets") .content(newVetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isCreated()); } @@ -157,7 +156,7 @@ void testCreateVetError() throws Exception { newVet.setFirstName(null); ObjectMapper mapper = new ObjectMapper(); String newVetAsJSON = mapper.writeValueAsString(vetMapper.toVetDto(newVet)); - this.mockMvc.perform(post("/api/vets/") + this.mockMvc.perform(post("/api/vets") .content(newVetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isBadRequest()); } diff --git a/src/test/java/org/springframework/samples/petclinic/rest/controller/VisitRestControllerTests.java b/src/test/java/org/springframework/samples/petclinic/rest/controller/VisitRestControllerTests.java index 74217cc5f..7226e5ccb 100644 --- a/src/test/java/org/springframework/samples/petclinic/rest/controller/VisitRestControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/rest/controller/VisitRestControllerTests.java @@ -23,7 +23,6 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; import org.springframework.samples.petclinic.mapper.VisitMapper; import org.springframework.samples.petclinic.model.Owner; @@ -35,6 +34,7 @@ import org.springframework.samples.petclinic.service.clinicService.ApplicationTestConfig; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.bean.override.mockito.MockitoBean; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; @@ -60,7 +60,7 @@ class VisitRestControllerTests { @Autowired private VisitRestController visitRestController; - @MockBean + @MockitoBean private ClinicService clinicService; @Autowired @@ -140,7 +140,7 @@ void testGetVisitNotFound() throws Exception { @WithMockUser(roles="OWNER_ADMIN") void testGetAllVisitsSuccess() throws Exception { given(this.clinicService.findAllVisits()).willReturn(visits); - this.mockMvc.perform(get("/api/visits/") + this.mockMvc.perform(get("/api/visits") .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().contentType("application/json")) @@ -155,7 +155,7 @@ void testGetAllVisitsSuccess() throws Exception { void testGetAllVisitsNotFound() throws Exception { visits.clear(); given(this.clinicService.findAllVisits()).willReturn(visits); - this.mockMvc.perform(get("/api/visits/") + this.mockMvc.perform(get("/api/visits") .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isNotFound()); } @@ -170,7 +170,7 @@ void testCreateVisitSuccess() throws Exception { mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); String newVisitAsJSON = mapper.writeValueAsString(visitMapper.toVisitDto(newVisit)); System.out.println("newVisitAsJSON " + newVisitAsJSON); - this.mockMvc.perform(post("/api/visits/") + this.mockMvc.perform(post("/api/visits") .content(newVisitAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isCreated()); } @@ -184,7 +184,7 @@ void testCreateVisitError() throws Exception { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new JavaTimeModule()); String newVisitAsJSON = mapper.writeValueAsString(visitMapper.toVisitDto(newVisit)); - this.mockMvc.perform(post("/api/visits/") + this.mockMvc.perform(post("/api/visits") .content(newVisitAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isBadRequest()); } diff --git a/src/test/java/org/springframework/samples/petclinic/service/clinicService/AbstractClinicServiceTests.java b/src/test/java/org/springframework/samples/petclinic/service/clinicService/AbstractClinicServiceTests.java index aff2ec156..501270c98 100644 --- a/src/test/java/org/springframework/samples/petclinic/service/clinicService/AbstractClinicServiceTests.java +++ b/src/test/java/org/springframework/samples/petclinic/service/clinicService/AbstractClinicServiceTests.java @@ -403,6 +403,7 @@ void shouldUpdatePetType(){ void shouldDeletePetType(){ PetType petType = this.clinicService.findPetTypeById(1); this.clinicService.deletePetType(petType); + clearCache(); try { petType = this.clinicService.findPetTypeById(1); } catch (Exception e) { @@ -499,4 +500,6 @@ void shouldFindSpecialtiesByNameIn() { && actual.getId().equals(expected.getId()))).isTrue(); } } + + void clearCache() {} } diff --git a/src/test/java/org/springframework/samples/petclinic/service/clinicService/ClinicServiceJpaTests.java b/src/test/java/org/springframework/samples/petclinic/service/clinicService/ClinicServiceJpaTests.java index 1a7305079..a33c5956e 100644 --- a/src/test/java/org/springframework/samples/petclinic/service/clinicService/ClinicServiceJpaTests.java +++ b/src/test/java/org/springframework/samples/petclinic/service/clinicService/ClinicServiceJpaTests.java @@ -1,5 +1,7 @@ package org.springframework.samples.petclinic.service.clinicService; +import jakarta.persistence.EntityManager; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; @@ -16,4 +18,11 @@ @ActiveProfiles({"jpa", "hsqldb"}) class ClinicServiceJpaTests extends AbstractClinicServiceTests { + @Autowired + EntityManager entityManager; + + @Override + void clearCache() { + entityManager.clear(); + } } diff --git a/src/test/java/org/springframework/samples/petclinic/service/clinicService/ClinicServiceSpringDataJpaTests.java b/src/test/java/org/springframework/samples/petclinic/service/clinicService/ClinicServiceSpringDataJpaTests.java index b5c57c0ce..8ab858a79 100644 --- a/src/test/java/org/springframework/samples/petclinic/service/clinicService/ClinicServiceSpringDataJpaTests.java +++ b/src/test/java/org/springframework/samples/petclinic/service/clinicService/ClinicServiceSpringDataJpaTests.java @@ -1,5 +1,7 @@ package org.springframework.samples.petclinic.service.clinicService; +import jakarta.persistence.EntityManager; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; @@ -14,4 +16,11 @@ @ActiveProfiles({"spring-data-jpa", "hsqldb"}) class ClinicServiceSpringDataJpaTests extends AbstractClinicServiceTests { + @Autowired + EntityManager entityManager; + + @Override + void clearCache() { + entityManager.clear(); + } }