浏览代码

everything works except for mockito

NedRedmond 5 年前
父节点
当前提交
c5a1e6b969

+ 16
- 1
pom.xml 查看文件

@@ -25,6 +25,13 @@
25 25
 	</properties>
26 26
 
27 27
 	<dependencies>
28
+		<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
29
+		<dependency>
30
+			<groupId>com.fasterxml.jackson.core</groupId>
31
+			<artifactId>jackson-core</artifactId>
32
+			<version>2.9.7</version>
33
+		</dependency>
34
+
28 35
 		<dependency>
29 36
 			<groupId>org.springframework.boot</groupId>
30 37
 			<artifactId>spring-boot-starter</artifactId>
@@ -50,7 +57,15 @@
50 57
 			<artifactId>spring-boot-starter-data-jpa</artifactId>
51 58
 			<!--<version>1.3.5.RELEASE</version>-->
52 59
 		</dependency>
53
-    </dependencies>
60
+		<!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
61
+		<dependency>
62
+			<groupId>org.mockito</groupId>
63
+			<artifactId>mockito-all</artifactId>
64
+			<version>1.9.5</version>
65
+			<scope>test</scope>
66
+		</dependency>
67
+
68
+	</dependencies>
54 69
 
55 70
 	<build>
56 71
 		<plugins>

+ 49
- 0
src/main/java/io/zipcoder/crudapp/Person.java 查看文件

@@ -0,0 +1,49 @@
1
+package io.zipcoder.crudapp;
2
+
3
+import javax.persistence.Entity;
4
+import javax.persistence.GeneratedValue;
5
+import javax.persistence.GenerationType;
6
+import javax.persistence.Id;
7
+
8
+@Entity
9
+public class Person {
10
+
11
+    private String firstName;
12
+    private String lastName;
13
+
14
+    @Id
15
+    @GeneratedValue(strategy = GenerationType.AUTO)
16
+    private Integer id;
17
+
18
+    public Person(String firstName, String lastName) {
19
+        this.firstName = firstName;
20
+        this.lastName = lastName;
21
+    }
22
+
23
+    public Person() {
24
+    }
25
+
26
+    public String getFirstName() {
27
+        return firstName;
28
+    }
29
+
30
+    public void setFirstName(String firstName) {
31
+        this.firstName = firstName;
32
+    }
33
+
34
+    public String getLastName() {
35
+        return lastName;
36
+    }
37
+
38
+    public void setLastName(String lastName) {
39
+        this.lastName = lastName;
40
+    }
41
+
42
+    public Integer getId() {
43
+        return id;
44
+    }
45
+
46
+    public void setId(Integer id) {
47
+        this.id = id;
48
+    }
49
+}

+ 42
- 0
src/main/java/io/zipcoder/crudapp/PersonController.java 查看文件

@@ -0,0 +1,42 @@
1
+package io.zipcoder.crudapp;
2
+
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.web.bind.annotation.*;
5
+
6
+import java.util.ArrayList;
7
+import java.util.List;
8
+
9
+@RestController
10
+public class PersonController {
11
+
12
+    @Autowired
13
+    PersonService personService;
14
+
15
+    @PostMapping("/people/{firstName}/{lastName}")
16
+    Person createPerson(@PathVariable String firstName, @PathVariable String lastName) {
17
+        Person person = new Person(firstName, lastName);
18
+        personService.save(person);
19
+        return personService.getOne(person.getId());
20
+    }
21
+
22
+    @GetMapping("/people/{id}")
23
+    Person getPerson(@PathVariable int id) {
24
+        return personService.getOne(id);
25
+    }
26
+
27
+    @GetMapping("/people")
28
+    List<Person> getPersonList() {
29
+        return personService.getAll();
30
+    }
31
+
32
+    @PutMapping("/people/{id}/{firstName}/{lastName}")
33
+    Person updatePerson(@ModelAttribute Person p) {
34
+        return personService.save(p);
35
+    }
36
+
37
+    @DeleteMapping("/people/{id}")
38
+    void deletePerson(@PathVariable int id) {
39
+        personService.delete(id);
40
+    }
41
+
42
+}

+ 9
- 0
src/main/java/io/zipcoder/crudapp/PersonRepo.java 查看文件

@@ -0,0 +1,9 @@
1
+package io.zipcoder.crudapp;
2
+
3
+import org.springframework.data.repository.CrudRepository;
4
+import org.springframework.stereotype.Repository;
5
+
6
+@Repository
7
+public interface PersonRepo extends CrudRepository<Person, Integer> {
8
+
9
+}

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

@@ -0,0 +1,34 @@
1
+package io.zipcoder.crudapp;
2
+
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.stereotype.Service;
5
+import org.springframework.web.bind.annotation.ModelAttribute;
6
+
7
+import java.util.ArrayList;
8
+import java.util.List;
9
+
10
+@Service
11
+public class PersonService {
12
+    @Autowired
13
+    PersonRepo personRepo;
14
+
15
+    public List<Person> getAll() {
16
+        List<Person> personList = new ArrayList<>();
17
+        personRepo.findAll().forEach(personList::add);
18
+        return personList;
19
+    }
20
+
21
+    public Person getOne(int id) {
22
+        return personRepo.findOne(id);
23
+    }
24
+
25
+    public Person save(Person p) {
26
+        personRepo.save(p);
27
+        return getOne(p.getId());
28
+    }
29
+
30
+    public void delete(int id){
31
+        personRepo.delete(id);
32
+    }
33
+
34
+}

+ 60
- 0
src/test/java/io/zipcoder/crudapp/CRUDApplicationTests.java 查看文件

@@ -1,16 +1,76 @@
1 1
 package io.zipcoder.crudapp;
2 2
 
3
+import org.junit.Before;
3 4
 import org.junit.Test;
4 5
 import org.junit.runner.RunWith;
6
+import org.mockito.InjectMocks;
7
+import org.mockito.Mock;
8
+import org.mockito.MockitoAnnotations;
5 9
 import org.springframework.boot.test.context.SpringBootTest;
10
+import org.springframework.http.MediaType;
6 11
 import org.springframework.test.context.junit4.SpringRunner;
12
+import org.springframework.test.web.servlet.MockMvc;
13
+import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
14
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
15
+
16
+import java.util.Arrays;
17
+import java.util.List;
18
+
19
+import static org.hamcrest.Matchers.*;
20
+import static org.mockito.Mockito.*;
21
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
22
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
23
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
24
+
7 25
 
8 26
 @RunWith(SpringRunner.class)
9 27
 @SpringBootTest
10 28
 public class CRUDApplicationTests {
11 29
 
30
+	private MockMvc mockMvc;
31
+
32
+	@Mock
33
+	private PersonService personService;
34
+
35
+	@InjectMocks
36
+	private PersonController personController;
37
+
38
+	@Before
39
+	public void init(){
40
+		MockitoAnnotations.initMocks(this);
41
+		mockMvc = MockMvcBuilders
42
+				.standaloneSetup(personController)
43
+				.build();
44
+	}
45
+
12 46
 	@Test
13 47
 	public void contextLoads() {
14 48
 	}
15 49
 
50
+	@Test
51
+	public void test_get_all_success() throws Exception {
52
+		List<Person> personList = Arrays.asList(
53
+				new Person("Daenerys", "Targaryen"),
54
+				new Person("John", "Snow"));
55
+
56
+		when(personService.getAll()).thenReturn(personList);
57
+
58
+		mockMvc.perform(get("/person"))
59
+				.andExpect(status().isOk())
60
+				.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
61
+				.andExpect(MockMvcResultMatchers.jsonPath("$", hasSize(2)))
62
+
63
+				.andExpect(MockMvcResultMatchers.jsonPath("$[0].firstName", is("Daenerys")))
64
+				.andExpect(MockMvcResultMatchers.jsonPath("$[0].firstName", is("Targaryen")))
65
+				.andExpect(MockMvcResultMatchers.jsonPath("$[0].id", is(1)))
66
+
67
+				.andExpect(MockMvcResultMatchers.jsonPath("$[1].firstName", is("John")))
68
+				.andExpect(MockMvcResultMatchers.jsonPath("$[0].firstName", is("Snow")))
69
+				.andExpect(MockMvcResultMatchers.jsonPath("$[1].id", is(2)))
70
+		;
71
+
72
+		verify(personService, times(1)).getAll();
73
+		verifyNoMoreInteractions(personService);
74
+	}
75
+
16 76
 }