diff --git a/mybatisplus-spring-boot/pom.xml b/mybatisplus-spring-boot/pom.xml index a5635be4a7fa3f7a67caf13c437aa8a6ff64197f..805c8e19158eb333cc7fb2f4f1eac1a9554cb599 100644 --- a/mybatisplus-spring-boot/pom.xml +++ b/mybatisplus-spring-boot/pom.xml @@ -39,6 +39,24 @@ mybatis-plus 1.4.6 + + + + org.springframework.boot + spring-boot-starter-test + + + com.jayway.restassured + rest-assured + 2.3.3 + test + + + + com.alibaba + fastjson + 1.2.13 + diff --git a/mybatisplus-spring-boot/src/test/java/com/baomidou/springboot/test/RestTest.java b/mybatisplus-spring-boot/src/test/java/com/baomidou/springboot/test/RestTest.java new file mode 100644 index 0000000000000000000000000000000000000000..0963060e9b034157e5087f36c913f49363163bbc --- /dev/null +++ b/mybatisplus-spring-boot/src/test/java/com/baomidou/springboot/test/RestTest.java @@ -0,0 +1,59 @@ +package com.baomidou.springboot.test; + +import static com.jayway.restassured.RestAssured.given; + +import org.hamcrest.Matcher; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.internal.matchers.Equals; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.alibaba.fastjson.JSONObject; +import com.baomidou.springboot.Application; +import com.jayway.restassured.RestAssured; +import com.jayway.restassured.matcher.ResponseAwareMatcher; +import com.jayway.restassured.response.Response; +import com.jayway.restassured.response.ValidatableResponse; + +@RunWith(SpringJUnit4ClassRunner.class) //1. +@SpringBootTest(classes = Application.class, webEnvironment=WebEnvironment.RANDOM_PORT ) // 2.SpringBoot入口类,配置起server随机端口 +public class RestTest { + + @Value("${local.server.port}") //3 + int port; + + @Before + public void doBefore(){ + RestAssured.port = port;//4: 告诉restAssured使用哪个端口来访问 + } + + @Test + public void postTest(){ + JSONObject parm = new JSONObject(); + parm.put("userId", "your id"); + parm.put("name", "your name"); + ValidatableResponse response = (ValidatableResponse) given().contentType("application/json") + .request().body(parm.toJSONString()) + .when().post("/user/test1").then() + //断言,类似Assert + .body("id", new Equals(1)) + //可以接多个断言 + .body("name", new ResponseAwareMatcher() { + @Override + public Matcher matcher(Response response) throws Exception { + return new Equals("三毛"); + } + }) + ; + JSONObject json = JSONObject.parseObject(response.extract().asString());//获取返回的json数据(2) + //自己写一些代码 + System.out.println(json); + + } +} + +