mockMvc測試案例


使用mockMvc可模擬http請求,在不啟動服務的情況進行快速測試。

package junit;


import java.util.concurrent.TimeUnit;

import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

@WebAppConfiguration
public class ControllerTestJuint extends BaseJunit{
    protected MockMvc mockMvc;
    @Autowired
    protected WebApplicationContext wac;
    @Before()  //這個方法在每個方法執行之前都會執行一遍
    public void setup() {
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();  //初始化MockMvc對象
    }
    @Test
    public void mockTest() throws Exception{
       ResultActions reaction =this.mockMvc.perform(MockMvcRequestBuilders.post("/sys/out/mockTest")
               .contentType(MediaType.APPLICATION_JSON)//請求體時json
               .param("customerId","7")
               .param("serviceType","all_service")
               .param("params[company_id]","1110000")//組裝map對象
               .param("params[AGE]","0,5")
               );
       reaction.andExpect(MockMvcResultMatchers.status().isOk());
       MvcResult mvcResult =reaction.andReturn();
       System.out.println(mvcResult.getResponse().getContentAsString());
       TimeUnit.SECONDS.sleep(60*60);
    }
}

package junit;

import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath*:applicationContext_*_test.xml"})
@Transactional
public class BaseJunit {

}

使用param方法添加參數,map參數以 ("map[mapkey]","mapvalue") 格式添加參數。

后台接受的controller:

    @RequestMapping(value="/mockTest",method = RequestMethod.POST)
    @ResponseBody
    public BaseMassage customerCreate(Dao dao){
        BaseMassage baseMassage = new BaseMassage();
        try{
            if (StringUtils.isEmpty(dao.getCustomerId())) {
                
            }else if(StringUtils.isEmpty(dao.getServiceType())){
               
            }else if(StringUtils.isEmpty(dao.getParams().get("company_id"))){
  
            }else{

            }
        }catch(Exception e){
        }
        return baseMassage;
    }

Dao:

@Setter
@Getter
public
class Dao { private String customerId; private String serviceType; private Map<String, String> params; }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM