restful接口测试案例

1.添加依赖

 
     org.springframework.boot
     spring-boot-starter-test
     test
 

2.代码示例

@RunWith(SpringRunner.class)
@SpringBootTest
public class RegistControllerTest {

    @Autowired
    private WebApplicationContext wac;
    private MockMvc mockMvc;

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

    @Test
    public void RegisterByUsername() throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.post("/register/username")  //指定接口访问方式POST,GET等
                .param("accountName", "admin")  //添加参数,如访问参数accountName值为admin
                .param("password","123456")
                .param("loginType","2")
                .param("orgId","")
                .contentType(MediaType.APPLICATION_JSON_UTF8))//使用json utf-8
                .andExpect(MockMvcResultMatchers.status().isOk())//返回的状态是200
                .andExpect(MockMvcResultMatchers.jsonPath(".result").value("Success"));
    }
}

 

转载于:https://my.oschina.net/wuyiyi/blog/1817884

你可能感兴趣的:(restful接口测试案例)