PR example

This commit is contained in:
dobozic 2023-10-18 11:52:07 +02:00
parent 923e2b7aa3
commit 5b1464a7b4
3 changed files with 21 additions and 0 deletions

View file

@ -23,6 +23,7 @@ import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@ -75,4 +76,13 @@ class VetController {
return vets;
}
@GetMapping({"/vets/{id}"})
public @ResponseBody Vet getById(@PathVariable Integer id) {
if (id == null) {
throw new IllegalArgumentException("id is required");
}
System.out.println("id: " + id);
return vetRepository.findById(id);
}
}

View file

@ -55,4 +55,7 @@ public interface VetRepository extends Repository<Vet, Integer> {
@Cacheable("vets")
Page<Vet> findAll(Pageable pageable) throws DataAccessException;
@Transactional(readOnly = false)
Vet findById(Integer id) throws RuntimeException;
}

View file

@ -72,6 +72,7 @@ class VetControllerTests {
@BeforeEach
void setup() {
given(this.vets.findAll()).willReturn(Lists.newArrayList(james(), helen()));
given(this.vets.findById(1)).willReturn(james());
given(this.vets.findAll(any(Pageable.class)))
.willReturn(new PageImpl<Vet>(Lists.newArrayList(james(), helen())));
@ -95,4 +96,11 @@ class VetControllerTests {
.andExpect(jsonPath("$.vetList[0].id").value(1));
}
@Test
void testShowResourcesVet() throws Exception {
ResultActions actions = mockMvc.perform(get("/vets/1").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
actions.andExpect(jsonPath("$.id").value(1));
}
}