-
Notifications
You must be signed in to change notification settings - Fork 115
/
SpringMvc_2_Test.java
57 lines (45 loc) · 2.07 KB
/
SpringMvc_2_Test.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package me.chanjar.spring2;
import me.chanjar.web.Foo;
import me.chanjar.web.FooController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
@EnableWebMvc
@WebAppConfiguration
@ContextConfiguration(classes = { FooController.class })
@TestExecutionListeners(listeners = MockitoTestExecutionListener.class)
public class SpringMvc_2_Test extends AbstractTestNGSpringContextTests {
@Autowired
private WebApplicationContext wac;
@MockBean
private Foo foo;
private MockMvc mvc;
@BeforeMethod
public void prepareMockMvc() {
this.mvc = webAppContextSetup(wac).build();
}
@Test
public void testController() throws Exception {
when(foo.checkCodeDuplicate(anyString())).thenReturn(true);
this.mvc.perform(get("/foo/check-code-dup").param("code", "123"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string("true"));
}
}