spring mvc test by mock

本文主要介绍spring mvc mock测试

我们在写spring mvc controller时,一般情况下都是在浏览器测试
但是有时候,我们写的controller提供的是api接口,这时我们该怎么测试呢?
这时spring mvc给我们提供了一个很好的测试方法,mock test

多的不说了,直接看代码:

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
58
59
60
61
62
63
64
65
66
67
68
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
import java.util.Date;
import javax.annotation.Resource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.test.BasicRespMessage;
import org.test.controller.TestController;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.context.WebApplicationContext;

@ContextConfiguration( locations = { "classpath*:spring/app-config.xml", "classpath*:spring/mvc-config.xml" } )
@WebAppConfiguration
@RunWith( SpringJUnit4ClassRunner.class )
public class TestController extends AbstractTransactionalJUnit4SpringContextTests {
@Resource
private WebApplicationContext wac;

private MockMvc mockMvc;

@Before
public void setup()
{
this.mockMvc = webAppContextSetup( this.wac ).build();
}


@Test
@Rollback( false )
public void test0() throws Exception
{
TestController controller = (TestController) this.applicationContext.getBean( "testController" );
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
request.setMethod( "POST" );
request.addParameter( "subject", "数学" );
request.addParameter( "grade", "66" ); /* 六年级 */
request.addParameter( "time", new Date().getTime() + "" );
request.addParameter( "sign", new Date().getTime() + "" );
BasicRespMessage m = controller.test0( request, response );
System.out.println( "--------->" + m.getStatusCode() + "<===>" + m.getStatusDesc() + "<===>" + m.getBody() );
}


@Test
public void test1() throws Exception
{
this.mockMvc.perform( /* */
put( "/test/test0" ) /* */
.param( "subject", "数学" ) /* */
.param( "grade", "66" ) /* // 六年级 */
.param( "time", new Date().getTime() + "" ) /* */
.param( "sign", new Date().getTime() + "" ) ) /* */
.andExpect( status().isOk() ) /* */
.andDo( print() )
.andReturn();
}
}

test0 method 是直接调用controller的method
test1 method 是请求url地址