@SpringBootTest public class Chapter11ApplicationTests { private MockMvc mvc; @Before public void setUp() { mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build(); } @Test public void getHello() throws Exception { mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().string(equalTo("Hello World"))); } } |
@SpringBootTest(classes = Chapter43Application.class) public class FileTest { @Autowired protected WebApplicationContext context; protected MockMvc mvc; @BeforeEach public void setUp() { mvc = MockMvcBuilders.webAppContextSetup(context).build(); } @Test public void uploadFile() throws Exception { MockMultipartFile file = new MockMultipartFile( "file", "hello.txt", MediaType.TEXT_PLAIN_VALUE, "Hello, World!".getBytes() ); final MvcResult result = mvc.perform( MockMvcRequestBuilders .multipart("/upload") .file(file)) .andDo(print()) .andExpect(status().isOk()) .andReturn(); } } |
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) | Powered by Discuz! X3.2 |