2 Commitit

Tekijä SHA1 Viesti Päivämäärä
  mpierse ff128d9a0e improved testing, all tests pass 5 vuotta sitten
  mpierse ce43edca22 testing improvements 5 vuotta sitten

+ 15
- 0
src/main/java/io/zipcoder/crudapp/Person.java Näytä tiedosto

@@ -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 Näytä tiedosto

@@ -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)

+ 60
- 23
src/test/java/io/zipcoder/crudapp/PersonControllerTest.java Näytä tiedosto

@@ -3,17 +3,28 @@ 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;
9
+import org.mockito.invocation.InvocationOnMock;
10
+import org.mockito.stubbing.Answer;
8 11
 import org.springframework.http.HttpStatus;
9 12
 import org.springframework.http.ResponseEntity;
13
+
14
+import java.util.ArrayList;
15
+
10 16
 import static org.mockito.Matchers.any;
17
+import static org.mockito.Mockito.doAnswer;
11 18
 import static org.mockito.Mockito.when;
12 19
 
13 20
 public class PersonControllerTest {
14 21
 
15 22
     @Mock
16
-    private PersonController controller;
23
+    private PersonRepository repository;
24
+
25
+    @InjectMocks
26
+    private PersonController controller = new PersonController();
27
+
17 28
 
18 29
     @Before
19 30
     public void setUp() throws Exception {
@@ -23,46 +34,72 @@ public class PersonControllerTest {
23 34
 
24 35
     @Test
25 36
     public void createPersonTest(){
26
-        when(controller.createPerson(any(Person.class))).thenReturn(new ResponseEntity<>( HttpStatus.CREATED));
37
+        Person person = new Person("a", "b");
38
+        Person savedPerson = new Person();
39
+        when(repository.save(person)).thenReturn(savedPerson);
27 40
 
28
-        Person p = new Person();
29
-        ResponseEntity<Person> actual = controller.createPerson(p);
30
-        ResponseEntity<Person> expected = new ResponseEntity<>(HttpStatus.CREATED);
41
+        ResponseEntity<Person> actual = controller.createPerson(person);
31 42
 
32
-        Assert.assertEquals(expected, actual);
43
+        Assert.assertEquals(actual.getStatusCode(), HttpStatus.CREATED);
44
+        Assert.assertEquals(actual.getBody(), savedPerson);
33 45
     }
34 46
 
35 47
     @Test
36 48
     public void getPersonTest(){
37
-        when(controller.getPerson(1)).thenReturn(new ResponseEntity<>( HttpStatus.OK));
49
+        Person person = new Person();
50
+        person.setId(88L);
51
+        when(repository.findOne(88L)).thenReturn(person);
52
+
53
+        ResponseEntity<Person> actual = controller.getPerson(88);
54
+
55
+        Assert.assertEquals(actual.getStatusCode(), HttpStatus.OK);
56
+        Assert.assertEquals(actual.getBody(), person);
57
+    }
58
+
59
+    @Test
60
+    public void getPersonListTest(){
61
+        Iterable<Person> personList = new ArrayList<>();
62
+        when(repository.findAll()).thenReturn(personList);
38 63
 
39
-        Person p = new Person();
40
-        p.setId(1L);
41
-        ResponseEntity<Person> actual = controller.getPerson(1);
42
-        ResponseEntity<Person> expected = new ResponseEntity<>(HttpStatus.OK);
64
+        ResponseEntity<Iterable<Person>> actual = controller.getPersonList();
43 65
 
44
-        Assert.assertEquals(expected, actual);
66
+        Assert.assertEquals(actual.getStatusCode(), HttpStatus.OK);
67
+        Assert.assertEquals(actual.getBody(), personList);
45 68
     }
46 69
 
47 70
     @Test
48 71
     public void updatePersonTest(){
49
-        when(controller.updatePerson(any(Person.class))).thenReturn(new ResponseEntity<>( HttpStatus.OK));
72
+        Person p = new Person("a", "b");
73
+        Person update = new Person("a","b");
74
+        when(repository.save(p)).thenReturn(update);
50 75
 
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);
76
+        ResponseEntity<Person> actual = controller.updatePerson(p);
55 77
 
56
-        Assert.assertEquals(expected, actual);
78
+        Assert.assertEquals(actual.getStatusCode(), HttpStatus.OK);
79
+        Assert.assertEquals(actual.getBody(), update);
57 80
     }
58 81
 
59 82
     @Test
60
-    public void deletePerson(){
61
-        when(controller.deletePerson(1)).thenReturn(new ResponseEntity<>( HttpStatus.NO_CONTENT));
83
+    public void deletePersonTest(){
84
+        Person p = new Person("a", "b");
85
+        p.setId(8783L);
86
+        doAnswer(new Answer<Void>() {
87
+
88
+            @Override
89
+            public Void answer(InvocationOnMock invocation) throws Throwable {
90
+                Object[] arguments = invocation.getArguments();
91
+                if (arguments != null && arguments.length > 1 && arguments[0] != null && arguments[1] != null) {
92
+
93
+                    controller.deletePerson(8783);
94
+
95
+                }
96
+                return null;
97
+            }
98
+        }).when(repository).delete(8783L);
62 99
 
63
-        ResponseEntity<Person> actual = controller.deletePerson(1);
64
-        ResponseEntity<Person> expected = new ResponseEntity<>( HttpStatus.NO_CONTENT);
100
+        ResponseEntity<Person> actual = controller.deletePerson(8783);
65 101
 
66
-        Assert.assertEquals(expected, actual);
102
+        Assert.assertEquals(actual.getStatusCode(), HttpStatus.NO_CONTENT);
103
+        Assert.assertEquals(actual.getBody(), null);
67 104
     }
68 105
 }