引入依赖
<dependency><groupId>com.squareup.retrofit2</groupId><artifactId>retrofit</artifactId><version>2.4.0</version></dependency><!-- https://mvnrepository.com/artifact/com.squareup.retrofit2/converter-gson --><dependency><groupId>com.squareup.retrofit2</groupId><artifactId>converter-gson</artifactId><version>2.3.0</version></dependency><!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/logging-interceptor --><dependency><groupId>com.squareup.okhttp3</groupId><artifactId>logging-interceptor</artifactId><version>3.10.0</version></dependency><!--json schema end--><dependency><groupId>com.github.fge</groupId><artifactId>json-schema-validator</artifactId><version>2.2.6</version></dependency>
一、封装HttpBase
public class HttpBase {public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");private Retrofit retrofit;private String host;/*** 构造方法(1个参数)* 只传Host,默认没有使用拦截器。** @param host 访问域名host*/public HttpBase(String host) {init(host, null);}/*** 构造方法(2个参数)* 只传Host,默认使用日志拦截器。** @param host 访问域名host* @param interceptor 自定义拦截器*/public HttpBase(String host, Interceptor interceptor) {init(host, interceptor);}/*** 初始化方法** @param host 访问域名host* @param interceptor 自定义拦截器*/private void init(String host, Interceptor interceptor) {OkHttpClient.Builder client = getHttpClient(interceptor);retrofit = new Retrofit.Builder().baseUrl(host).client(client.build()).addConverterFactory(GsonConverterFactory.create(new Gson())) // 使用Gson进行响应转换.build();}/*** 获取HttpClient.Builder 方法。* 默认添加了,基础日志拦截器** @param interceptor 拦截器* @return HttpClient.Builder对象*/private OkHttpClient.Builder getHttpClient(Interceptor interceptor) {HttpLoggingInterceptor logging = getHttpLoggingInterceptor();OkHttpClient.Builder builder = new OkHttpClient.Builder().connectTimeout(10, TimeUnit.SECONDS).retryOnConnectionFailure(true);if (interceptor != null) {builder.addInterceptor(interceptor);}builder.addInterceptor(logging);return builder;}/*** 日志拦截器** @return*/private HttpLoggingInterceptor getHttpLoggingInterceptor() {HttpLoggingInterceptor logging = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {@Overridepublic void log(String message) {Reporter.log("RetrofitLog--> " + message, true);}});logging.setLevel(HttpLoggingInterceptor.Level.BODY);//Level中还有其他等级. 设置打印内容级别到Body。return logging;}/*** retrofit构建方法** @param clazz 泛型类* @param <T> 泛型类* @return 泛型类*/public <T> T create(Class<T> clazz) {return retrofit.create(clazz);}public String getHost() {return host;}public void setHost(String host) {this.host = host;}}
二、定义待测接口
public interface ISearch {@GET("j/search_tags")Call<MovieResponseVO> searchTags(@Query("type") String type, @Query("source") String source);}
三、二次封装,复用初始化代码
public class HttpSearch extends HttpBase {private ISearch iSearch;public HttpSearch(String host) {super(host);iSearch = super.create(ISearch.class);}public Response<Map> searchTags(String type, String source) throws IOException {Call<Map> call = iSearch.searchTags(type, source);return call.execute();}}
四、编写用例
public class SearchTagsTest {private static Properties properties;private static HttpSearch implSearch;private static String SCHEMA_PATH = "parameters/search/schema/SearchTagsMovie.json";@BeforeSuitepublic void beforeSuite() throws IOException {InputStream stream = this.getClass().getClassLoader().getResourceAsStream("env.properties");properties = new Properties();properties.load(stream);String host = properties.getProperty("douban.host");implSearch = new HttpSearch(host);stream = this.getClass().getClassLoader().getResourceAsStream("parameters/search/SearchTagsParams.properties");properties.load(stream);stream = this.getClass().getClassLoader().getResourceAsStream("");stream.close();}@Test(description = "电影首页。类别:type=movie source=index")public void testcase1() throws IOException {String type = properties.getProperty("testcase1.req.type");String source = properties.getProperty("testcase1.req.source");Response<Map> response = implSearch.searchTags(type, source);Map body = response.body();Assert.assertNotNull(body, "response.body()");// 响应返回内容想通过schema标准校验JsonSchemaUtils.assertResponseJsonSchema(SCHEMA_PATH, JSONObject.toJSONString(body));//校验具体的值}@Test(description = "Tv首页。类别:type=tv source=index")public void testcase2() throws IOException {String type = properties.getProperty("testcase2.req.type");String source = properties.getProperty("testcase2.req.source");Response<Map> response = implSearch.searchTags(type, source);Map body = response.body();Assert.assertNotNull(body, "response.body()");JsonSchemaUtils.assertResponseJsonSchema(SCHEMA_PATH, JSONObject.toJSONString(body));//校验具体的值}}
五、拦截器实现
public class MyInterceptor implements Interceptor {@Overridepublic Response intercept(Chain chain) throws IOException {Request request = chain.request();Response response = chain.proceed(request);long time = response.receivedResponseAtMillis() - response.sentRequestAtMillis();if (time > 100) {MyReporter.report.log(Status.WARNING, MyReporter.getTestName() + " 接口耗时:" + time);}return response;}}
注意:需要实现自定义请求头时,在拦截器中进行定义
六、JsonSchema信息获取
https://jsonschema.net/ 访问并获取
