2 次代码提交

作者 SHA1 备注 提交日期
  mpierse 7e0d7f8884 mokito testing 5 年前
  mpierse 0886b84c45 overloaded person constructor 5 年前

+ 5
- 1
src/main/java/io/zipcoder/crudapp/Person.java 查看文件

@@ -6,7 +6,6 @@ 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.io.Serializable;
10 9
 
11 10
 @Entity
12 11
 public class Person {
@@ -20,6 +19,11 @@ public class Person {
20 19
     public Person() {
21 20
     }
22 21
 
22
+    public Person(String firstName, String lastName) {
23
+        this.firstName = firstName;
24
+        this.lastName = lastName;
25
+    }
26
+
23 27
     public Long getId() {
24 28
         return id;
25 29
     }

+ 0
- 2
src/main/java/io/zipcoder/crudapp/PersonRepository.java 查看文件

@@ -4,6 +4,4 @@ import org.springframework.data.repository.CrudRepository;
4 4
 
5 5
 public interface PersonRepository extends CrudRepository<Person, Long> {
6 6
 
7
-
8
-
9 7
 }

+ 0
- 5
src/main/java/io/zipcoder/crudapp/PersonService.java 查看文件

@@ -1,5 +0,0 @@
1
-package io.zipcoder.crudapp;
2
-
3
-public class PersonService {
4
-
5
-}

+ 68
- 0
src/test/java/io/zipcoder/crudapp/PersonControllerTest.java 查看文件

@@ -0,0 +1,68 @@
1
+package io.zipcoder.crudapp;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+import org.mockito.Mock;
7
+import org.mockito.MockitoAnnotations;
8
+import org.springframework.http.HttpStatus;
9
+import org.springframework.http.ResponseEntity;
10
+import static org.mockito.Matchers.any;
11
+import static org.mockito.Mockito.when;
12
+
13
+public class PersonControllerTest {
14
+
15
+    @Mock
16
+    private PersonController controller;
17
+
18
+    @Before
19
+    public void setUp() throws Exception {
20
+
21
+        MockitoAnnotations.initMocks(this);
22
+    }
23
+
24
+    @Test
25
+    public void createPersonTest(){
26
+        when(controller.createPerson(any(Person.class))).thenReturn(new ResponseEntity<>( HttpStatus.CREATED));
27
+
28
+        Person p = new Person();
29
+        ResponseEntity<Person> actual = controller.createPerson(p);
30
+        ResponseEntity<Person> expected = new ResponseEntity<>(HttpStatus.CREATED);
31
+
32
+        Assert.assertEquals(expected, actual);
33
+    }
34
+
35
+    @Test
36
+    public void getPersonTest(){
37
+        when(controller.getPerson(1)).thenReturn(new ResponseEntity<>( HttpStatus.OK));
38
+
39
+        Person p = new Person();
40
+        p.setId(1L);
41
+        ResponseEntity<Person> actual = controller.getPerson(1);
42
+        ResponseEntity<Person> expected = new ResponseEntity<>(HttpStatus.OK);
43
+
44
+        Assert.assertEquals(expected, actual);
45
+    }
46
+
47
+    @Test
48
+    public void updatePersonTest(){
49
+        when(controller.updatePerson(any(Person.class))).thenReturn(new ResponseEntity<>( HttpStatus.OK));
50
+
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);
55
+
56
+        Assert.assertEquals(expected, actual);
57
+    }
58
+
59
+    @Test
60
+    public void deletePerson(){
61
+        when(controller.deletePerson(1)).thenReturn(new ResponseEntity<>( HttpStatus.NO_CONTENT));
62
+
63
+        ResponseEntity<Person> actual = controller.deletePerson(1);
64
+        ResponseEntity<Person> expected = new ResponseEntity<>( HttpStatus.NO_CONTENT);
65
+
66
+        Assert.assertEquals(expected, actual);
67
+    }
68
+}