1. 程式人生 > >springboot 單元測試-哪些靜態的引入

springboot 單元測試-哪些靜態的引入

package com.example.demo;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNot.not;
import static org.hamcrest.core.IsNull.notNullValue;
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.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;  
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;  
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;  
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*; 
import java.io.UnsupportedEncodingException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Before;
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.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;


import com.cyb.MyBootStarter;
import com.cyb.date.DateUtil;
import com.cyb.po.UserLoginLog;
import com.cyb.service.LoginLogServiceImpl;


/**
 * 作者 : iechenyb<br>
 * 類描述: 需要單獨開啟h2伺服器才能訪問<br>
 * 修復hibernate更新不顯示bug
 * spring.jpa.properties.hibernate.current_session_context_class
 * =org.springframework.orm.hibernate4.SpringSessionContext 建立時間: 2017年12月4日
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = MyBootStarter.class)
@ActiveProfiles("prod")
@WebAppConfiguration
@ContextConfiguration(locations = { "classpath:application-tansaction.xml", "classpath:application-bean.xml" })
// @ImportResource("classpath:application-tansaction.xml")
public class MVCMockTest {
	Log logger = LogFactory.getLog(getClass());


	@Autowired
	LoginLogServiceImpl service;


	@Autowired
	private WebApplicationContext context;


	private MockMvc mockMvc;


	@Before
	public void init() {
		System.out.println("init...");
		mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
	}
	// 判斷返回值,是否達到預期,測試示例中的返回值的結構如下{"errcode":0,"errmsg":"OK","p2pdata":null}


	public void get() throws UnsupportedEncodingException, Exception {
		// 模擬傳送請求
		String result = mockMvc
				.perform(MockMvcRequestBuilders.get("/user") // 發往/user的get請求,可以換成post,put,delete方法執行相應請求
						.param("username", "xxx") // get請求時填寫引數的位置
						.contentType(MediaType.APPLICATION_JSON_UTF8) // utf編碼
						.content("")) // post和put請求填寫引數的位置
				.andExpect(status().isOk()).andExpect(jsonPath("$.length()").value(3)) // 期望的json返回結果
				.andReturn().getResponse().getContentAsString(); // 對返回字串的json內容進行判斷
		System.out.println(result);
	}


	@Test
	public void print() throws Exception {
		// 呼叫介面,傳入新增的使用者引數
		mockMvc.perform(
				MockMvcRequestBuilders.post("/user/adduser")
				.contentType(MediaType.APPLICATION_JSON_UTF8).content(""))
				.andExpect(status().isOk())
				.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
				// 使用jsonPath解析返回值,判斷具體的內容
				.andExpect(jsonPath("$.errcode", is(0)))
				.andExpect(jsonPath("$.p2pdata", notNullValue()))
				.andExpect(jsonPath("$.p2pdata.id", not(0)))
				.andExpect(jsonPath("$.p2pdata.name", is("testuser2")));
		System.out.println("execute...");
	}


	// 建立物件更新
	@Test
	public void updateTest() throws Exception {
		UserLoginLog my = service.getLog(1L);
		System.out.println("查詢id為1的使用者名稱:" + my.getUsername());
		UserLoginLog log = new UserLoginLog();
		log.setId(33L);
		log.setCount(100L);
		log.setLoginTime(DateUtil.timeToMilis());
		log.setLastTime(DateUtil.timeToMilis());
		service.updateTest(log);
		System.out.println("*****************");
		// Thread.sleep(10*1000);
	}
}