SpringBoot对Controller进行单元测试
package com.bestrivenlf.cn.hyitshop;
import com.bestrivenlf.cn.hyitshop.model.Order;
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.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
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.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class HyitShopApplicationTests {
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(
webApplicationContext).build();
}
@Test
public void TestMQ() throws Exception {
String uri = "/shop/getShopByShopId" + "?shopId=7";
MvcResult mvcResult = this.mockMvc
.perform(MockMvcRequestBuilders.get(uri))
.andReturn();
System.out.println(mvcResult.getResponse().getContentAsString());
}
}