File tree Expand file tree Collapse file tree 2 files changed +30
-2
lines changed
main/java/com/sar/sandpit
test/java/com/sar/sandpit Expand file tree Collapse file tree 2 files changed +30
-2
lines changed Original file line number Diff line number Diff line change 15
15
@ RequestMapping ("/todo" )
16
16
public class ItemController {
17
17
18
- @ Autowired
18
+ // @Autowired
19
19
ItemService itemService ;
20
20
21
+ public ItemController (){}
22
+ @ Autowired
23
+ public ItemController (ItemService is ) {
24
+ itemService =is ;
25
+ }
26
+
27
+
21
28
@ RequestMapping ( path ="index" , method = RequestMethod .GET )
22
29
public String home (){
23
30
Original file line number Diff line number Diff line change 1
1
package com .sar .sandpit ;
2
2
3
3
import org .hamcrest .CoreMatchers ;
4
+ import org .junit .Before ;
4
5
import org .junit .Test ;
5
6
import org .junit .runner .RunWith ;
6
7
import org .mockito .ArgumentCaptor ;
13
14
import org .springframework .boot .test .mock .mockito .MockBean ;
14
15
import org .springframework .test .context .junit4 .SpringRunner ;
15
16
import org .springframework .test .web .servlet .MockMvc ;
17
+ import org .springframework .test .web .servlet .MockMvcBuilder ;
16
18
import org .springframework .test .web .servlet .request .MockMvcRequestBuilders ;
17
19
import org .springframework .test .web .servlet .result .MockMvcResultMatchers ;
20
+ import org .springframework .test .web .servlet .setup .MockMvcBuilders ;
18
21
import sun .reflect .generics .reflectiveObjects .NotImplementedException ;
19
22
20
23
import static org .hamcrest .CoreMatchers .*;
33
36
@ WebMvcTest // Just the web context
34
37
public class ItemControllerTest {
35
38
36
- @ Autowired
39
+ // @Autowired
37
40
MockMvc mockMvc ;
38
41
39
42
@ MockBean
@@ -44,9 +47,27 @@ public class ItemControllerTest {
44
47
ArgumentCaptor <Item > itemCapture ;
45
48
46
49
50
+ @ Before
51
+ public void init (){
52
+ mockMvc =MockMvcBuilders .standaloneSetup (new ItemController (itemService )).build ();
53
+ }
54
+
55
+
47
56
48
57
@ Test
49
58
public void addItem_whenItemDoesnotExit_success () throws Exception {
59
+
60
+
61
+
62
+ final String task = "tasks" ;
63
+ final long id = 1L ;
64
+ Item item = new Item (id , task );
65
+
66
+
67
+ //when(itemService.add(item)).thenReturn( new Item());
68
+ when (itemService .add (item )).thenReturn (item );
69
+
70
+
50
71
mockMvc .perform (
51
72
post ("/todo/addItem/" )
52
73
.param ("id" ,"1" )
You can’t perform that action at this time.
0 commit comments