1
1
package com .springbrasil .repository .controller ;
2
2
3
+ import static org .hamcrest .CoreMatchers .instanceOf ;
3
4
import static org .mockito .Matchers .any ;
4
5
import static org .mockito .Matchers .anyInt ;
6
+ import static org .mockito .Matchers .eq ;
5
7
import static org .mockito .Mockito .doReturn ;
8
+ import static org .mockito .Mockito .doThrow ;
6
9
import static org .mockito .Mockito .times ;
7
10
import static org .mockito .Mockito .verify ;
8
11
import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
12
15
13
16
import java .util .Arrays ;
14
17
18
+ import javax .ws .rs .NotFoundException ;
19
+
15
20
import org .junit .Before ;
21
+ import org .junit .Rule ;
16
22
import org .junit .Test ;
23
+ import org .junit .rules .ExpectedException ;
17
24
import org .junit .runner .RunWith ;
18
25
import org .springframework .beans .factory .annotation .Autowired ;
19
26
import org .springframework .boot .test .autoconfigure .web .servlet .WebMvcTest ;
22
29
import org .springframework .http .MediaType ;
23
30
import org .springframework .test .context .junit4 .SpringRunner ;
24
31
import org .springframework .test .web .servlet .MockMvc ;
32
+ import org .springframework .web .util .NestedServletException ;
25
33
26
34
import com .springbrasil .repository .helper .JsonHelper ;
27
35
import com .springbrasil .repository .model .Repository ;
@@ -39,6 +47,9 @@ public class RepositoryControllerUnitTest {
39
47
@ Autowired
40
48
private MockMvc mvc ;
41
49
50
+ @ Rule
51
+ public ExpectedException exception = ExpectedException .none ();
52
+
42
53
private Repository repository ;
43
54
44
55
@ Before
@@ -49,6 +60,8 @@ public void setUp() {
49
60
repository .setType (RepositoryType .GITHUB );
50
61
doReturn (repository ).when (repositoryService ).save (any (Repository .class ));
51
62
doReturn (new PageImpl <>(Arrays .asList (repository ))).when (repositoryService ).getAll (anyInt (), anyInt ());
63
+ doReturn (repository ).when (repositoryService ).get (eq ("6e71d0d568e134c029203593b00a0103e7cdf30b" ));
64
+ doThrow (new NotFoundException ("unexisting_repository" )).when (repositoryService ).get (eq ("unexisting_repository" ));
52
65
}
53
66
54
67
@ Test
@@ -74,5 +87,26 @@ public void shouldGetAllRepositories() throws Exception {
74
87
75
88
verify (repositoryService , times (1 )).getAll (0 , 10 );
76
89
}
90
+
91
+ @ Test
92
+ public void shouldGetOneRepository () throws Exception {
93
+ String response = JsonHelper .getResponseFileAsString ("repository/get_one_repository_success.json" );
94
+
95
+ mvc .perform (get ("/repositories/6e71d0d568e134c029203593b00a0103e7cdf30b" ).accept (MediaType .APPLICATION_JSON_UTF8_VALUE ))
96
+ .andExpect (status ().isOk ())
97
+ .andExpect (content ().json (response ));
98
+
99
+ verify (repositoryService , times (1 )).get ("6e71d0d568e134c029203593b00a0103e7cdf30b" );
100
+ }
101
+
102
+ @ Test
103
+ public void shouldThrowErrorWhenGetUnexistingRepository () throws Exception {
104
+ exception .expect (NestedServletException .class );
105
+ exception .expectCause (instanceOf (NotFoundException .class ));
106
+
107
+ mvc .perform (get ("/repositories/unexisting_repository" )
108
+ .accept (MediaType .APPLICATION_JSON_UTF8_VALUE ))
109
+ .andExpect (status ().isNotFound ());
110
+ }
77
111
78
112
}
0 commit comments