forked from chenjiangtao/veryjava.spring.boot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemoTest.java
More file actions
49 lines (42 loc) · 1.59 KB
/
Copy pathDemoTest.java
File metadata and controls
49 lines (42 loc) · 1.59 KB
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
package cn.veryjava;
import cn.veryjava.service.DemoService;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
/**
* 描述: TODO:
* 包名: cn.veryjava.
* 作者: barton.
* 日期: 16-11-3.
* 项目名称: veryjava.spring.boot
* 版本: 1.0
* JDK: since 1.8
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {UnitTestApplication.class, MockServletContext.class})
public class DemoTest {
@Autowired
private DemoService demoService;
@Autowired
private WebApplicationContext context;
@Test
public void testDemo() {
Assert.assertEquals("Hello World! Hello sunshineasbefore!", demoService.demo("sunshineasbefore"));
}
@Test
public void testControllerDemo() throws Exception {
MockMvc mvc = MockMvcBuilders.webAppContextSetup(this.context).build();
RequestBuilder e = get("/demo").param("name", "sunshineasbefore");
String response = mvc.perform(e).andReturn().getResponse().getContentAsString();
Assert.assertEquals("Hello World! Hello sunshineasbefore!", response);
}
}