hjg
2024-01-20 4a3404efc438b16044fd9170814e6545a3f86fae
提交 | 用户 | 时间
58d006 1 package com.mandi.system.test;
A 2
3 import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
4 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
5 import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
6
7
8 import org.junit.Before;
9 import org.junit.Test;
10 import org.junit.runner.RunWith;
11 import org.springframework.beans.factory.annotation.Autowired;
12 import org.springframework.test.context.ContextConfiguration;
13 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
14 import org.springframework.test.context.web.WebAppConfiguration;
15 import org.springframework.test.web.servlet.MockMvc;
16 import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
17 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
18 import org.springframework.transaction.annotation.Transactional;
19 import org.springframework.web.context.WebApplicationContext;
20 import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
21
22 @RunWith(SpringJUnit4ClassRunner.class)  
23 @WebAppConfiguration  
24 @ContextConfiguration(locations = { "classpath:applicationContext.xml" })  
25 @Transactional 
26 public class Systemcontest {
27     @Autowired  
28     private WebApplicationContext wac;  
29     private MockMvc mockMvc;  
30     
31     private  RequestMappingHandlerMapping rmhm;
32   
33     @Before  
34     public void setup() {  
35         // webAppContextSetup 注意上面的static import  
36         // webAppContextSetup 构造的WEB容器可以添加fileter 但是不能添加listenCLASS  
37         // WebApplicationContext context =  
38         // ContextLoader.getCurrentWebApplicationContext();  
39         // 如果控制器包含如上方法 则会报空指针  
40         this.mockMvc = webAppContextSetup(this.wac).build();  
41     } 
42     @Test
43     public void test(){
44         try {
45             MockHttpServletRequestBuilder rq=MockMvcRequestBuilders.post("/admin/modulecon/rolemodule.htm");
46             rq.param("id", "37").param("pagesize", "15").param("pages", "1").param("tid", "12");
47             mockMvc.perform(rq).andExpect(status().isOk()).andDo(print());
48         } catch (Exception e) {
49             e.printStackTrace();
50         } 
51     }
52 }