Fixed tests

This commit is contained in:
Andrew Pitt 2020-01-06 12:28:42 -05:00
parent 55dae5ac8d
commit 0d17a00cf5
2 changed files with 32 additions and 40 deletions

View file

@ -28,16 +28,11 @@ public class MainPageIT {
} }
@Test @Test
public void googleTitleIT() { public void petclinicTitleIT() {
WebDriver driver = config.getDriver(); WebDriver driver = config.getDriver();
driver.get(url); driver.get(url);
assertEquals("Title not as expected: ", "PetClinic :: a Spring Framework demonstration", getTitle()); assertEquals("Title not as expected: ", "PetClinic :: a Spring Framework demonstration", getTitle());
// Save the random value from the page.
String welcomeText = driver.findElement(By.id("welcome")).getText(); String welcomeText = driver.findElement(By.id("welcome")).getText();
// Reload the page and get a new random value.
driver.get(url);
String value2 = driver.findElement(By.id("random-value")).getText();
// Values should not be the same.
assertEquals("Wrong welcome text.", "Welcome to the Pet Clinic!", welcomeText); assertEquals("Wrong welcome text.", "Welcome to the Pet Clinic!", welcomeText);
System.out.println("Text extracted: " + welcomeText); System.out.println("Text extracted: " + welcomeText);
} }

View file

@ -43,43 +43,40 @@ import org.springframework.test.web.servlet.ResultActions;
@WebMvcTest(VetController.class) @WebMvcTest(VetController.class)
public class VetControllerTests { public class VetControllerTests {
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
@MockBean @MockBean
private VetRepository vets; private VetRepository vets;
@Before @Before
public void setup() { public void setup() {
Vet james = new Vet(); Vet james = new Vet();
james.setFirstName("James"); james.setFirstName("James");
james.setLastName("Carter"); james.setLastName("Carter");
james.setId(1); james.setId(1);
Vet helen = new Vet(); Vet helen = new Vet();
helen.setFirstName("Helen"); helen.setFirstName("Helen");
helen.setLastName("Leary"); helen.setLastName("Leary");
helen.setId(2); helen.setId(2);
Specialty radiology = new Specialty(); Specialty radiology = new Specialty();
radiology.setId(1); radiology.setId(1);
radiology.setName("radiology"); radiology.setName("radiology");
helen.addSpecialty(radiology); helen.addSpecialty(radiology);
given(this.vets.findAll()).willReturn(Lists.newArrayList(james, helen)); given(this.vets.findAll()).willReturn(Lists.newArrayList(james, helen));
} }
@Test @Test
public void testShowVetListHtml() throws Exception { public void testShowVetListHtml() throws Exception {
mockMvc.perform(get("/vets.html")) mockMvc.perform(get("/vets.html")).andExpect(status().isOk()).andExpect(model().attributeExists("vets"))
.andExpect(status().isOk()) .andExpect(view().name("vets/vetList"));
.andExpect(model().attributeExists("vets")) }
.andExpect(view().name("vets/vetList"));
}
@Test @Test
public void testShowResourcesVetList() throws Exception { public void testShowResourcesVetList() throws Exception {
ResultActions actions = mockMvc.perform(get("/vets") ResultActions actions = mockMvc.perform(get("/vets").accept(MediaType.APPLICATION_JSON))
.accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()); .andExpect(status().isOk());
actions.andExpect(content().contentType("application/json;charset=UTF-8")) actions.andExpect(content().contentType("application/json")).andExpect(jsonPath("$.vetList[0].id").value(1));
.andExpect(jsonPath("$.vetList[0].id").value(1)); }
}
} }