http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
推荐一个轻量级开源Java web框架 Spark framework 资料地址:http://sparkjava.com/ 源码: 配置启动端口: public static void init(){ port(4567); } Get请求 public static void getExample(){ get("/getTest", (request, response) -> { // .. Show something .. String resul
接口测试框架 RestAssured它主要用于Java的接口测试, RestAssured框架和Java项目有比较好的兼容性,可以方便地集成到持续集成流程中。 RestAssured框架在如何验证响应状态码、头部、内容等提供了丰富的验证方法,RestAssured的log().all()方法可以打印详细日志,方便进行接口测试出现问题后定位问题。 RestAssured框架在Schema验证通过JsonSchemaValidator 验证 JSON 结构是否符合规范,引用schema文件时,并调用matchesJsonSchemaInClasspath 全局代理设置:RestAssured.proxy = host("proxy-host").withPort(8080);单次请求代理设置:@Testpublic void testWithProxy( https://untrusted-cert-api.com") .when() .get("/data") .then() .statusCode(200);}RestAssured
response.getCookie("cookieName"); 上述这些已几乎可满足日常工作所需,如有需要可在官网进一步研究,官网还提供了获取同名多值的header和cookie等方法: 相关参考链接: RESTAssured
在 test 包下创建一个 RestAssuredGetRequest 类,用来发送不带参数 GET 请求,代码如下: import io.restassured.RestAssured; import io.restassured.http.ContentType; import io.restassured.response.Response; import org.junit.jupiter.api.Assertions ; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import static io.restassured.RestAssured.given ; public class RestAssuredRequest { @BeforeAll public static void setup(){ RestAssured.baseURI public class RestAssuredPostRequest { @BeforeAll public static void setup(){ RestAssured.baseURI
Java 版本 import io.restassured.RestAssured; import static io.restassured.RestAssured.*; import static io.restassured.specification.ProxySpecification.host; import static org.hamcrest.core.IsEqual.equalTo ; public class Requests { public static void main(String[] args) { RestAssured.proxy = post", data={'school': "第二次请求"}, verify=False) Java 版本 import io.restassured.RestAssured ; import static io.restassured.RestAssured.*; import static io.restassured.specification.ProxySpecification.host
log().all().get("http://127.0.0.1:8000/restAssured.json"). then(). log().all().get("http://127.0.0.1:8000/restAssured.json"). then(). get("http://127.0.0.1:8000/restAssured.xml"). then(). get("http://127.0.0.1:8000/restAssured.xml"). then(). get("http://127.0.0.1:8000/restAssured.xml"). then().
args"]["name"] == "hogwarts"Java 版本import org.junit.jupiter.api.Test;import static io.restassured.RestAssured.given 200Java版本import org.junit.jupiter.api.Test;import java.util.HashMap;import java.util.Map;import static io.restassured.RestAssured.given org.junit.jupiter.api.Test;import java.io.File;import java.io.IOException;import java.util.HashMap;import static io.restassured.RestAssured.given
given().log().all() (Rest-assured的官方文档:https://github.com/rest-assured/rest-assured) 解决方法: 1.翻阅资料,可以通过RestAssured.config 来改变日志方面的配置,因此尝试从这里入手 RestAssured.config = RestAssured.config().logConfig(new LogConfig()); 2.发现一种解决方法 需要新建一个类来将logger转为outputstream ToLoggerPrintStream loggerPrintStream = new ToLoggerPrintStream(logger); RestAssured.config = RestAssured.config().logConfig(new LogConfig(loggerPrintStream.getPrintStream(), true)); ToLoggerPrintStream = RestAssured.config().logConfig(new LogConfig(loggerPrintStream.getPrintStream(), true)); *
Create new package under test package named com.restassured, create a java class named QuickstartTest import classes as static imports, then we can using the given when then statement import static io.restassured.RestAssured .*; import static io.restassured.matcher.RestAssuredMatchers.*; import static org.hamcrest.Matchers.* The given method class name is RestAssured, we are using static keyword for importing the classes
在Java生态中,有几个流行的接口测试框架,比如RestAssured、JUnit、TestNG、Spring Test等。 一、快速入门RestAssured + JUnit 51. 添加 Maven 依赖<dependencies> <! 编写测试用例import org.junit.jupiter.api.Test;import static io.restassured.RestAssured. (使用 RestAssured 或 HttpClient) wireMock.stop();}根据项目的情况,可以采取以下方式。 简单 REST 测试RestAssured + JUnit 5;Spring Boot 项目Spring Test + MockMvc;复杂测试流程TestNG + RestAssured;微服务契约测试
requests.post("https://httpbin.ceshiren.com/post") assert r.status_code == 200 JAVA版本 Java 需要通过添加 RestAssured import io.restassured.RestAssured; import io.restassured.config.HttpClientConfig; import io.restassured.config.RestAssuredConfig ; import org.junit.jupiter.api.Test; import static io.restassured.RestAssured.given; public class ReqTimeoutTest httpbin.ceshiren.com/get").then().statusCode(200).log().all(); } @Test void timeout2(){ RestAssured.config
get("id"); } @Test public void whenGetAllBooks_thenOK() { Response response = RestAssured.get Book book = createRandomBook(); createBookAsUri(book); Response response = RestAssured.get = createRandomBook(); String location = createBookAsUri(book); Response response = RestAssured.get } @Test public void whenGetNotExistBookById_thenNotFound() { Response response = RestAssured.get { Book book = createRandomBook(); book.setAuthor(null); Response response = RestAssured.given
args"]["name"] == "hogwarts"Java 版本import org.junit.jupiter.api.Test;import static io.restassured.RestAssured.given 200Java版本import org.junit.jupiter.api.Test;import java.util.HashMap;import java.util.Map;import static io.restassured.RestAssured.given org.junit.jupiter.api.Test;import java.io.File;import java.io.IOException;import java.util.HashMap;import static io.restassured.RestAssured.given
text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NjYzNTA5MQ==,size_16,color_FFFFFF,t_70#pic_center] 3.2 RestAssured.config ().logConfig 3.2.1 方法实现 在研究过程中发现RestAssured提供了logConfig方法,可以将原本在Console中打印的信息指定格式化输出到文件中,具体用法如下(这里指演示重点实现原理部分 PrintStream printStream = new PrintStream(new WriterOutputStream(fileWriter), true)) { RestAssured.config = RestAssured.config().logConfig(LogConfig.logConfig().defaultStream(printStream)); given().XXX.log when().XXX then().log().all(); } catch (IOException e) { e.printStackTrace(); } } RestAssured
import static io.restassured.RestAssured.*; public class Requests { public static void main(String
113.89.10.187", "url": "https://httpbin.ceshiren.com/post" } Java 版本 import static io.restassured.RestAssured
"origin": "113.89.10.187", "url": "https://httpbin.ceshiren.com/post" } Java 版本 import static io.restassured.RestAssured
org.junit.jupiter.api.Test;import java.io.IOException;import java.util.LinkedHashMap;import static io.restassured.RestAssured.given
测试(使用restassured进行测试): RestAssured.baseURI = "http://" + mockServer.getContainerIpAddress(); RestAssured.port MockServerContainer mockServer = new MockServerContainer(); @Test public void v() { RestAssured.baseURI = "http://" + mockServer.getContainerIpAddress(); RestAssured.port = mockServer.getServerPort