1. 程式人生 > >spring webmvc如何編寫service 和controller的單元測試

spring webmvc如何編寫service 和controller的單元測試

package com.kingnet.xydb.web.controller;
/**
* Created by xiaoj on 2016/5/30.
*/
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.kingnet.xydb.web.basetest.SpringTestCase;
import org.junit.Before
;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
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;
import java.nio.charset.Charset;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static
org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
public class HelloControllerTest extends SpringTestCase {




@Test
public void hello() throws Exception {

this.mockMvc.perform(get("/hello")).andExpect(content().contentType(APPLICATION_JSON_UTF8)).andExpect(jsonPath("$.resultCode").value("00"));
}
}