PersonControllerTest.java 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package io.zipcoder.crudapp;
  2. import io.zipcoder.crudapp.controllers.PersonController;
  3. import io.zipcoder.crudapp.models.Person;
  4. import io.zipcoder.crudapp.services.PersonService;
  5. import org.junit.Before;
  6. import org.junit.Test;
  7. import org.junit.runner.RunWith;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.boot.test.context.SpringBootTest;
  10. import org.springframework.boot.test.mock.mockito.MockBean;
  11. import org.springframework.http.HttpStatus;
  12. import org.springframework.http.ResponseEntity;
  13. import org.springframework.test.context.junit4.SpringRunner;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. import static org.assertj.core.api.Java6Assertions.assertThat;
  17. import static org.mockito.BDDMockito.given;
  18. @SpringBootTest
  19. @RunWith(SpringRunner.class)
  20. public class PersonControllerTest {
  21. List<Person> list;
  22. Person testPerson = new Person("Test", "Person");
  23. @Before
  24. public void setup(){
  25. list = new ArrayList<>();
  26. list.add(testPerson);
  27. }
  28. @Autowired
  29. PersonController personController;
  30. @MockBean
  31. PersonService personService;
  32. @Test
  33. public void testGetPersonList(){
  34. given(
  35. this.personService.getPersonList()
  36. ).willReturn(
  37. list
  38. );
  39. ResponseEntity<Iterable<Person>> actual = personController.getPersonList();
  40. assertThat(actual)
  41. .isEqualTo(new ResponseEntity<>(list, HttpStatus.OK));
  42. }
  43. @Test
  44. public void testGetPersonById(){
  45. given(this.personService.getPersonById(1L)
  46. ).willReturn(testPerson);
  47. ResponseEntity<?> actual = personController.getPersonById(1L);
  48. assertThat(actual).isEqualTo(new ResponseEntity<>(testPerson, HttpStatus.OK));
  49. }
  50. @Test
  51. public void testGetPersonById_null(){
  52. given(this.personService.getPersonById(2L)
  53. ).willReturn(null);
  54. ResponseEntity<?> actual = personController.getPersonById(2L);
  55. assertThat(actual).isEqualTo(new ResponseEntity<>(null, HttpStatus.NOT_FOUND));
  56. }
  57. @Test
  58. public void testCreatePerson(){
  59. given(this.personService.savePerson(testPerson)
  60. ).willReturn(testPerson);
  61. ResponseEntity<?> actual = personController.createPerson(testPerson);
  62. assertThat(actual).isEqualTo(new ResponseEntity<>(testPerson,HttpStatus.CREATED));
  63. }
  64. @Test
  65. public void testUpdatePerson(){
  66. testPerson.setFirstName("Bill");
  67. given(this.personService.savePerson(testPerson)
  68. ).willReturn(testPerson);
  69. ResponseEntity<?> actual = personController.updatePerson(1L, testPerson);
  70. assertThat(actual).isEqualTo(new ResponseEntity<>(testPerson,HttpStatus.OK));
  71. }
  72. @Test
  73. public void testDelete(){
  74. ResponseEntity<?> actual = personController.deletePerson(1L);
  75. assertThat(actual).isEqualTo(new ResponseEntity(HttpStatus.NO_CONTENT));
  76. }
  77. }