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.ContextConfiguration;
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.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* Created by admin on 2020/7/24.
*/
@ContextConfiguration
@WebAppConfiguration
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MixAppeventApplication.class)
public class AppEventControllerTests {
private MockMvc mvc;
private String token = "token";
@Autowired
private WebApplicationContext webApplicationContext;
@Before
public void setUp() {
mvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Test
public void testAppEventController() throws Exception {
RequestBuilder request;
request = MockMvcRequestBuilders.post("/app/opt/event/open-app")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", token)
.header("X-Device-IMEI", "123456");
MvcResult mvcResult = mvc.perform(request)
.andExpect(status().isOk()).andReturn();
System.out.println(mvcResult);
}
}
MockMvc的使用
最新推荐文章于 2025-02-25 21:30:33 发布