8000 webmvctest · blackbeltcoder/Todoo@688437a · GitHub
[go: up one dir, main page]

Skip to content
10000

Commit 688437a

Browse files
author
n460906
committed
webmvctest
1 parent c762d6c commit 688437a

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/main/java/com/sar/sandpit/ItemController.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,16 @@
1515
@RequestMapping("/todo")
1616
public class ItemController {
1717

18-
@Autowired
18+
// @Autowired
1919
ItemService itemService;
2020

21+
public ItemController(){}
22+
@Autowired
23+
public ItemController(ItemService is) {
24+
itemService=is;
25+
}
26+
27+
2128
@RequestMapping( path ="index", method = RequestMethod.GET)
2229
public String home(){
2330

src/test/java/com/sar/sandpit/ItemControllerTest.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.sar.sandpit;
22

33
import org.hamcrest.CoreMatchers;
4+
import org.junit.Before;
45
import org.junit.Test;
56
import org.junit.runner.RunWith;
67
import org.mockito.ArgumentCaptor;
@@ -13,8 +14,10 @@
1314
import org.springframework.boot.test.mock.mockito.MockBean;
1415
import org.springframework.test.context.junit4.SpringRunner;
1516
import org.springframework.test.web.servlet.MockMvc;
17+
import org.springframework.test.web.servlet.MockMvcBuilder;
1618
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
1719
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
20+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
1821
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
1922

2023
import static org.hamcrest.CoreMatchers.*;
@@ -33,7 +36,7 @@
3336
@WebMvcTest // Just the web context
3437
public class ItemControllerTest {
3538

36-
@Autowired
39+
//@Autowired
3740
MockMvc mockMvc;
3841

3942
@MockBean
@@ -44,9 +47,27 @@ public class ItemControllerTest {
4447
ArgumentCaptor<Item> itemCapture;
4548

4649

50+
@Before
51+
public void init(){
52+
mockMvc =MockMvcBuilders.standaloneSetup(new ItemController(itemService)).build();
53+
}
54+
55+
4756

4857
@Test
4958
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+
5071
mockMvc.perform(
5172
post("/todo/addItem/")
5273
.param("id","1")

0 commit comments

Comments
 (0)
0