| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- package io.zipcoder.crudapp;
-
- import io.zipcoder.crudapp.controllers.PersonController;
- import io.zipcoder.crudapp.models.Person;
- import io.zipcoder.crudapp.services.PersonService;
- import org.junit.Before;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- 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.HttpStatus;
- import org.springframework.http.ResponseEntity;
- import org.springframework.test.context.junit4.SpringRunner;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import static org.assertj.core.api.Java6Assertions.assertThat;
- import static org.mockito.BDDMockito.given;
-
- @SpringBootTest
- @RunWith(SpringRunner.class)
- public class PersonControllerTest {
- List<Person> list;
- Person testPerson = new Person("Test", "Person");
-
- @Before
- public void setup(){
- list = new ArrayList<>();
- list.add(testPerson);
- }
-
- @Autowired
- PersonController personController;
-
- @MockBean
- PersonService personService;
-
- @Test
- public void testGetPersonList(){
- given(
- this.personService.getPersonList()
- ).willReturn(
- list
- );
- ResponseEntity<Iterable<Person>> actual = personController.getPersonList();
- assertThat(actual)
- .isEqualTo(new ResponseEntity<>(list, HttpStatus.OK));
- }
-
- @Test
- public void testGetPersonById(){
- given(this.personService.getPersonById(1L)
- ).willReturn(testPerson);
-
- ResponseEntity<?> actual = personController.getPersonById(1L);
- assertThat(actual).isEqualTo(new ResponseEntity<>(testPerson, HttpStatus.OK));
- }
-
- @Test
- public void testGetPersonById_null(){
- given(this.personService.getPersonById(2L)
- ).willReturn(null);
-
- ResponseEntity<?> actual = personController.getPersonById(2L);
- assertThat(actual).isEqualTo(new ResponseEntity<>(null, HttpStatus.NOT_FOUND));
- }
-
- @Test
- public void testCreatePerson(){
- given(this.personService.savePerson(testPerson)
- ).willReturn(testPerson);
-
- ResponseEntity<?> actual = personController.createPerson(testPerson);
- assertThat(actual).isEqualTo(new ResponseEntity<>(testPerson,HttpStatus.CREATED));
- }
-
- @Test
- public void testUpdatePerson(){
- testPerson.setFirstName("Bill");
- given(this.personService.savePerson(testPerson)
- ).willReturn(testPerson);
-
- ResponseEntity<?> actual = personController.updatePerson(1L, testPerson);
- assertThat(actual).isEqualTo(new ResponseEntity<>(testPerson,HttpStatus.OK));
- }
-
- @Test
- public void testDelete(){
- ResponseEntity<?> actual = personController.deletePerson(1L);
- assertThat(actual).isEqualTo(new ResponseEntity(HttpStatus.NO_CONTENT));
- }
- }
|