Commit 2da27a76 authored by Martin Parák's avatar Martin Parák

Added Spring MVC Tests for Owner Controller. Closes #52.

parent 427a63eb
package sk.jusska.sfgpetclinic.controller;
import static org.hamcrest.Matchers.hasSize;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
import java.util.HashSet;
import java.util.Set;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import sk.jusska.sfgpetclinic.model.Owner;
import sk.jusska.sfgpetclinic.service.OwnerService;
@ExtendWith(MockitoExtension.class)
class OwnerControllerTest {
@Mock
OwnerService ownerService;
@InjectMocks
OwnerController controller;
Set<Owner> owners;
MockMvc mockMvc;
@BeforeEach
void setUp() {
owners = new HashSet<>();
owners.add(Owner.builder().id(1L).build());
owners.add(Owner.builder().id(2L).build());
mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
}
@Test
void listOwners() throws Exception {
when(ownerService.findAll()).thenReturn(owners);
mockMvc.perform(MockMvcRequestBuilders.get("/owners"))
.andExpect(status().isOk())
.andExpect(view().name("owners/index"))
.andExpect(model().attribute("owners", hasSize(2)));
}
@Test
void listOwnersByIndex() throws Exception {
when(ownerService.findAll()).thenReturn(owners);
mockMvc.perform(MockMvcRequestBuilders.get("/owners/index"))
.andExpect(status().isOk())
.andExpect(view().name("owners/index"))
.andExpect(model().attribute("owners", hasSize(2)));
}
@Test
void findOwners() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/owners/find"))
.andExpect(status().isOk())
.andExpect(view().name("notimplemented"));
verifyNoInteractions(ownerService);
}
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment