MockMVC测试Controller中常见的请求方式

TestController.java
@RestController
public class TestController {

    private final String PATH = "D:\\Develop\\JavaEE\\laboratory";

    @GetMapping(value = "/mockTestA")
    public String mockTestA(@RequestParam("a") String a,
                            @RequestParam("b") String b) {
        return a + b;
    }

    @PostMapping(value = "/mockTestB")
    public String mockTestB(@RequestParam(value = "a", defaultValue = "1") String a,
                            @RequestParam(value = "b", defaultValue = "2") String b) {
        return a + b;
    }

    @PostMapping(value = "/mockTestC")
    public User mockTestC(@RequestBody User user) {
        return user;
    }

    @PostMapping(value = "/file")
    public String file(MultipartFile file) throws IOException {
        File localFile = new File(PATH, "test.txt");

        file.transferTo(localFile);

        return file.getName();
    }
    
}
User.java
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class User {

    private String id;

    private String username;
}
TestControllerTest
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestControllerTest {

    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;

    @Before
    public void setUp() {
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
    }

    @Test
    public void mockTestA() throws Exception {
        String result = mockMvc.perform(get("/mockTestA")
                .param("a", "1")        // 添加参数
                .param("b", "2")
                .contentType(MediaType.APPLICATION_JSON_UTF8))      // 设置数据格式
                .andDo(print())     // 打印输出发出请求的详细信息
                .andExpect(status().isOk())     // 对返回值进行断言
                .andReturn().getResponse().getContentAsString();        // 获取方法的返回值

        Assert.assertEquals("12", result);
    }

    @Test
    public void mockTestB() throws Exception {
        String result = mockMvc.perform(post("/mockTestB")
                .param("a", "10")
                .param("b", "11")
                .contentType(MediaType.APPLICATION_JSON_UTF8))
                .andDo(print())
                .andExpect(status().isOk())
                .andReturn().getResponse().getContentAsString();

        Assert.assertEquals("1011", result);
    }

    @Test
    public void mockTestC() throws Exception {
        Map<String, String> map = new HashMap<>();
        map.put("id", "123");
        map.put("username", "tom");
        String content = JSONObject.toJSONString(map);

        String result = mockMvc.perform(post("/mockTestC")
                .contentType(MediaType.APPLICATION_JSON_UTF8)
                .content(content))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.id").value("123"))
                .andReturn().getResponse().getContentAsString();

        Assert.assertNotNull(result);
    }

    @Test
    public void file() throws Exception {
        String result = mockMvc.perform(fileUpload("/file")
                .file(new MockMultipartFile("file", "test.txt",
                        "multipart/form-data",
                        "hello upload".getBytes("UTF-8"))))
                .andDo(print())
                .andExpect(status().isOk())
                .andReturn().getResponse().getContentAsString();
        Assert.assertEquals("file", result);
    }
}
  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值