test: remove duplicate assertion

This commit is contained in:
Anyul Rivas 2024-03-19 09:32:15 +01:00 committed by Dave Syer
parent 19f5268ac1
commit 8f799263e0

View file

@ -17,8 +17,6 @@
package org.springframework.samples.petclinic.owner;
import org.assertj.core.util.Lists;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledInNativeImage;
@ -33,7 +31,6 @@ import org.springframework.test.context.aot.DisabledInAotMode;
import org.springframework.test.web.servlet.MockMvc;
import java.time.LocalDate;
import java.util.List;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.greaterThan;
@ -94,9 +91,9 @@ class OwnerControllerTests {
Owner george = george();
given(this.owners.findByLastName(eq("Franklin"), any(Pageable.class)))
.willReturn(new PageImpl<Owner>(Lists.newArrayList(george)));
.willReturn(new PageImpl<>(Lists.newArrayList(george)));
given(this.owners.findAll(any(Pageable.class))).willReturn(new PageImpl<Owner>(Lists.newArrayList(george)));
given(this.owners.findAll(any(Pageable.class))).willReturn(new PageImpl<>(Lists.newArrayList(george)));
given(this.owners.findById(TEST_OWNER_ID)).willReturn(george);
Visit visit = new Visit();
@ -145,14 +142,14 @@ class OwnerControllerTests {
@Test
void testProcessFindFormSuccess() throws Exception {
Page<Owner> tasks = new PageImpl<Owner>(Lists.newArrayList(george(), new Owner()));
Page<Owner> tasks = new PageImpl<>(Lists.newArrayList(george(), new Owner()));
Mockito.when(this.owners.findByLastName(anyString(), any(Pageable.class))).thenReturn(tasks);
mockMvc.perform(get("/owners?page=1")).andExpect(status().isOk()).andExpect(view().name("owners/ownersList"));
}
@Test
void testProcessFindFormByLastName() throws Exception {
Page<Owner> tasks = new PageImpl<Owner>(Lists.newArrayList(george()));
Page<Owner> tasks = new PageImpl<>(Lists.newArrayList(george()));
Mockito.when(this.owners.findByLastName(eq("Franklin"), any(Pageable.class))).thenReturn(tasks);
mockMvc.perform(get("/owners?page=1").param("lastName", "Franklin"))
.andExpect(status().is3xxRedirection())
@ -161,7 +158,7 @@ class OwnerControllerTests {
@Test
void testProcessFindFormNoOwnersFound() throws Exception {
Page<Owner> tasks = new PageImpl<Owner>(Lists.newArrayList());
Page<Owner> tasks = new PageImpl<>(Lists.newArrayList());
Mockito.when(this.owners.findByLastName(eq("Unknown Surname"), any(Pageable.class))).thenReturn(tasks);
mockMvc.perform(get("/owners?page=1").param("lastName", "Unknown Surname"))
.andExpect(status().isOk())
@ -228,21 +225,6 @@ class OwnerControllerTests {
.andExpect(model().attribute("owner", hasProperty("telephone", is("6085551023"))))
.andExpect(model().attribute("owner", hasProperty("pets", not(empty()))))
.andExpect(model().attribute("owner", hasProperty("pets", hasItem(hasProperty("visits", hasSize(greaterThan(0)))))))
.andExpect(model().attribute("owner", hasProperty("pets", new BaseMatcher<List<Pet>>() {
@Override
public boolean matches(Object item) {
@SuppressWarnings("unchecked")
List<Pet> pets = (List<Pet>) item;
Pet pet = pets.get(0);
return !pet.getVisits().isEmpty();
}
@Override
public void describeTo(Description description) {
description.appendText("Max did not have any visits");
}
})))
.andExpect(view().name("owners/ownerDetails"));
}