Bladeren bron

Merge branch 'dev' of chenweida/jw2.0 into dev

chenweida 7 jaren geleden
bovenliggende
commit
f7795f7496

+ 17 - 0
svr/svr-demo/src/test/java/com/yihu/jw/common/BaseTest.java

@ -0,0 +1,17 @@
package com.yihu.jw.common;
import org.junit.runner.RunWith;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
 * Created by chenweida on 2017/11/27.
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@SpringBootConfiguration
public class BaseTest {
}

+ 58 - 0
svr/svr-demo/src/test/java/com/yihu/jw/controller/DemoControllerTest.java

@ -0,0 +1,58 @@
package com.yihu.jw.controller;
import com.yihu.jw.common.BaseTest;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
 * @author chenweida
 *
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoControllerTest extends BaseTest {
	@Autowired
	protected WebApplicationContext wac;
	protected MockMvc mockMvc;
	/**
	 * 初始化模拟mvc环境
	 */
	@Before
	public void setup() {
		mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
	}
	/**
	 * 测试查询
	 * @throws Exception
     */
	@Test
	public void whenQuerySuccess() throws Exception {
		String result = mockMvc.perform(
				get("/demo")
						// .param("size", "15")
						// .param("page", "3")
						// .param("sort", "age,desc")
						.contentType(MediaType.APPLICATION_JSON_UTF8))
				.andExpect(status().isOk())
				.andReturn().getResponse().getContentAsString();
		
		System.out.println(result);
	}
}

+ 5 - 4
svr/svr-demo/src/test/java/com/yihu/test/UserControllerTest.java

@ -1,4 +1,4 @@
package com.yihu.test;
package com.yihu.jw.controller;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.fileUpload;
@ -12,6 +12,7 @@ import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import com.yihu.jw.common.BaseTest;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -30,12 +31,12 @@ import org.springframework.web.context.WebApplicationContext;
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserControllerTest {
public class UserControllerTest extends BaseTest {
	@Autowired
	private WebApplicationContext wac;
	protected WebApplicationContext wac;
	private MockMvc mockMvc;
	protected MockMvc mockMvc;
	/**
	 * 初始化模拟mvc环境