Bladeren bron

testing improvements

mpierse 5 jaren geleden
bovenliggende
commit
ce43edca22

+ 15
- 0
src/main/java/io/zipcoder/crudapp/Person.java Bestand weergeven

@@ -6,6 +6,7 @@ import javax.persistence.Entity;
6 6
 import javax.persistence.GeneratedValue;
7 7
 import javax.persistence.GenerationType;
8 8
 import javax.persistence.Id;
9
+import java.util.Objects;
9 10
 
10 11
 @Entity
11 12
 public class Person {
@@ -48,4 +49,18 @@ public class Person {
48 49
         this.lastName = lastName;
49 50
     }
50 51
 
52
+    @Override
53
+    public boolean equals(Object o) {
54
+        if (this == o) return true;
55
+        if (!(o instanceof Person)) return false;
56
+        Person person = (Person) o;
57
+        return Objects.equals(getId(), person.getId()) &&
58
+                Objects.equals(getFirstName(), person.getFirstName()) &&
59
+                Objects.equals(getLastName(), person.getLastName());
60
+    }
61
+
62
+    @Override
63
+    public int hashCode() {
64
+        return Objects.hash(getId(), getFirstName(), getLastName());
65
+    }
51 66
 }

+ 4
- 5
src/main/java/io/zipcoder/crudapp/PersonController.java Bestand weergeven

@@ -17,11 +17,10 @@ public class PersonController {
17 17
 
18 18
     @RequestMapping(value = "/people", method = RequestMethod.POST)
19 19
     public ResponseEntity<Person> createPerson(@RequestBody Person p){
20
-        personRepository.save(p);
21
-        return new ResponseEntity<>(p,HttpStatus.CREATED);
20
+        Person savedPerson = personRepository.save(p);
21
+        return new ResponseEntity<>(savedPerson, HttpStatus.CREATED);
22 22
     }
23 23
 
24
-
25 24
     @RequestMapping(value = "/people/{id}", method = RequestMethod.GET)
26 25
     public ResponseEntity<Person> getPerson(@PathVariable("id")int id){
27 26
        Person p = personRepository.findOne((long)id);
@@ -36,8 +35,8 @@ public class PersonController {
36 35
 
37 36
     @RequestMapping(value = "/people/{id}", method = RequestMethod.PUT)
38 37
     public ResponseEntity<Person> updatePerson(@RequestBody Person p){
39
-        personRepository.save(p);
40
-        return new ResponseEntity<>(p, HttpStatus.OK);
38
+        Person person = personRepository.save(p);
39
+        return new ResponseEntity<>(person, HttpStatus.OK);
41 40
     }
42 41
 
43 42
     @RequestMapping(value = "/people/{id}", method = RequestMethod.DELETE)

+ 56
- 24
src/test/java/io/zipcoder/crudapp/PersonControllerTest.java Bestand weergeven

@@ -3,17 +3,25 @@ package io.zipcoder.crudapp;
3 3
 import org.junit.Assert;
4 4
 import org.junit.Before;
5 5
 import org.junit.Test;
6
+import org.mockito.InjectMocks;
6 7
 import org.mockito.Mock;
7 8
 import org.mockito.MockitoAnnotations;
8 9
 import org.springframework.http.HttpStatus;
9 10
 import org.springframework.http.ResponseEntity;
11
+
12
+import java.util.ArrayList;
13
+
10 14
 import static org.mockito.Matchers.any;
11 15
 import static org.mockito.Mockito.when;
12 16
 
13 17
 public class PersonControllerTest {
14 18
 
15 19
     @Mock
16
-    private PersonController controller;
20
+    private PersonRepository repository;
21
+
22
+    @InjectMocks
23
+    private PersonController controller = new PersonController();
24
+
17 25
 
18 26
     @Before
19 27
     public void setUp() throws Exception {
@@ -23,46 +31,70 @@ public class PersonControllerTest {
23 31
 
24 32
     @Test
25 33
     public void createPersonTest(){
26
-        when(controller.createPerson(any(Person.class))).thenReturn(new ResponseEntity<>( HttpStatus.CREATED));
34
+        Person person = new Person("a", "b");
35
+        Person savedPerson = new Person();
36
+        when(repository.save(person)).thenReturn(savedPerson);
27 37
 
28
-        Person p = new Person();
29
-        ResponseEntity<Person> actual = controller.createPerson(p);
30
-        ResponseEntity<Person> expected = new ResponseEntity<>(HttpStatus.CREATED);
38
+        ResponseEntity<Person> actual = controller.createPerson(person);
31 39
 
32
-        Assert.assertEquals(expected, actual);
40
+        Assert.assertEquals(actual.getStatusCode(), HttpStatus.CREATED);
41
+        Assert.assertEquals(actual.getBody(), savedPerson);
33 42
     }
34 43
 
35 44
     @Test
36 45
     public void getPersonTest(){
37
-        when(controller.getPerson(1)).thenReturn(new ResponseEntity<>( HttpStatus.OK));
46
+        Person person = new Person();
47
+        person.setId(88L);
48
+        when(repository.findOne(88L)).thenReturn(person);
38 49
 
39
-        Person p = new Person();
40
-        p.setId(1L);
41
-        ResponseEntity<Person> actual = controller.getPerson(1);
42
-        ResponseEntity<Person> expected = new ResponseEntity<>(HttpStatus.OK);
50
+        ResponseEntity<Person> actual = controller.getPerson(88);
43 51
 
44
-        Assert.assertEquals(expected, actual);
52
+        Assert.assertEquals(actual.getStatusCode(), HttpStatus.OK);
53
+        Assert.assertEquals(actual.getBody(), person);
45 54
     }
46 55
 
47 56
     @Test
48
-    public void updatePersonTest(){
49
-        when(controller.updatePerson(any(Person.class))).thenReturn(new ResponseEntity<>( HttpStatus.OK));
57
+    public void getPersonList(){
58
+        Iterable<Person> personList = new ArrayList<>();
59
+        when(repository.findAll()).thenReturn(personList);
50 60
 
51
-        Person p = new Person();
52
-        p.setId(1L);
53
-        ResponseEntity<Person> actual = controller.updatePerson(any(Person.class));
54
-        ResponseEntity<Person> expected = new ResponseEntity<>(HttpStatus.OK);
61
+        ResponseEntity<Iterable<Person>> actual = controller.getPersonList();
55 62
 
56
-        Assert.assertEquals(expected, actual);
63
+        Assert.assertEquals(actual.getStatusCode(), HttpStatus.OK);
64
+        Assert.assertEquals(actual.getBody(), personList);
57 65
     }
58 66
 
59 67
     @Test
60
-    public void deletePerson(){
61
-        when(controller.deletePerson(1)).thenReturn(new ResponseEntity<>( HttpStatus.NO_CONTENT));
68
+    public void updatePerson(){
69
+        Person p = new Person("a", "b");
70
+        Person update = new Person("a","b");
71
+        when(repository.save(p)).thenReturn(update);
62 72
 
63
-        ResponseEntity<Person> actual = controller.deletePerson(1);
64
-        ResponseEntity<Person> expected = new ResponseEntity<>( HttpStatus.NO_CONTENT);
73
+        ResponseEntity<Person> actual = controller.updatePerson(p);
65 74
 
66
-        Assert.assertEquals(expected, actual);
75
+        Assert.assertEquals(actual.getStatusCode(), HttpStatus.OK);
76
+        Assert.assertEquals(actual.getBody(), update);
67 77
     }
78
+    
79
+//    @Test
80
+//    public void updatePersonTest(){
81
+//        when(controller.updatePerson(any(Person.class))).thenReturn(new ResponseEntity<>( HttpStatus.OK));
82
+//
83
+//        Person p = new Person();
84
+//        p.setId(1L);
85
+//        ResponseEntity<Person> actual = controller.updatePerson(any(Person.class));
86
+//        ResponseEntity<Person> expected = new ResponseEntity<>(HttpStatus.OK);
87
+//
88
+//        Assert.assertEquals(expected, actual);
89
+//    }
90
+//
91
+//    @Test
92
+//    public void deletePerson(){
93
+//        when(controller.deletePerson(1)).thenReturn(new ResponseEntity<>( HttpStatus.NO_CONTENT));
94
+//
95
+//        ResponseEntity<Person> actual = controller.deletePerson(1);
96
+//        ResponseEntity<Person> expected = new ResponseEntity<>( HttpStatus.NO_CONTENT);
97
+//
98
+//        Assert.assertEquals(expected, actual);
99
+//    }
68 100
 }