引入依赖

  1. <dependency>
  2. <groupId>com.squareup.retrofit2</groupId>
  3. <artifactId>retrofit</artifactId>
  4. <version>2.4.0</version>
  5. </dependency>
  6. <!-- https://mvnrepository.com/artifact/com.squareup.retrofit2/converter-gson -->
  7. <dependency>
  8. <groupId>com.squareup.retrofit2</groupId>
  9. <artifactId>converter-gson</artifactId>
  10. <version>2.3.0</version>
  11. </dependency>
  12. <!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/logging-interceptor -->
  13. <dependency>
  14. <groupId>com.squareup.okhttp3</groupId>
  15. <artifactId>logging-interceptor</artifactId>
  16. <version>3.10.0</version>
  17. </dependency>
  18. <!--json schema end-->
  19. <dependency>
  20. <groupId>com.github.fge</groupId>
  21. <artifactId>json-schema-validator</artifactId>
  22. <version>2.2.6</version>
  23. </dependency>

一、封装HttpBase

  1. public class HttpBase {
  2. public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
  3. private Retrofit retrofit;
  4. private String host;
  5. /**
  6. * 构造方法(1个参数)
  7. * 只传Host,默认没有使用拦截器。
  8. *
  9. * @param host 访问域名host
  10. */
  11. public HttpBase(String host) {
  12. init(host, null);
  13. }
  14. /**
  15. * 构造方法(2个参数)
  16. * 只传Host,默认使用日志拦截器。
  17. *
  18. * @param host 访问域名host
  19. * @param interceptor 自定义拦截器
  20. */
  21. public HttpBase(String host, Interceptor interceptor) {
  22. init(host, interceptor);
  23. }
  24. /**
  25. * 初始化方法
  26. *
  27. * @param host 访问域名host
  28. * @param interceptor 自定义拦截器
  29. */
  30. private void init(String host, Interceptor interceptor) {
  31. OkHttpClient.Builder client = getHttpClient(interceptor);
  32. retrofit = new Retrofit.Builder()
  33. .baseUrl(host)
  34. .client(client.build())
  35. .addConverterFactory(GsonConverterFactory.create(new Gson())) // 使用Gson进行响应转换
  36. .build();
  37. }
  38. /**
  39. * 获取HttpClient.Builder 方法。
  40. * 默认添加了,基础日志拦截器
  41. *
  42. * @param interceptor 拦截器
  43. * @return HttpClient.Builder对象
  44. */
  45. private OkHttpClient.Builder getHttpClient(Interceptor interceptor) {
  46. HttpLoggingInterceptor logging = getHttpLoggingInterceptor();
  47. OkHttpClient.Builder builder = new OkHttpClient.Builder()
  48. .connectTimeout(10, TimeUnit.SECONDS)
  49. .retryOnConnectionFailure(true);
  50. if (interceptor != null) {
  51. builder.addInterceptor(interceptor);
  52. }
  53. builder.addInterceptor(logging);
  54. return builder;
  55. }
  56. /**
  57. * 日志拦截器
  58. *
  59. * @return
  60. */
  61. private HttpLoggingInterceptor getHttpLoggingInterceptor() {
  62. HttpLoggingInterceptor logging = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
  63. @Override
  64. public void log(String message) {
  65. Reporter.log("RetrofitLog--> " + message, true);
  66. }
  67. });
  68. logging.setLevel(HttpLoggingInterceptor.Level.BODY);//Level中还有其他等级. 设置打印内容级别到Body。
  69. return logging;
  70. }
  71. /**
  72. * retrofit构建方法
  73. *
  74. * @param clazz 泛型类
  75. * @param <T> 泛型类
  76. * @return 泛型类
  77. */
  78. public <T> T create(Class<T> clazz) {
  79. return retrofit.create(clazz);
  80. }
  81. public String getHost() {
  82. return host;
  83. }
  84. public void setHost(String host) {
  85. this.host = host;
  86. }
  87. }

二、定义待测接口

  1. public interface ISearch {
  2. @GET("j/search_tags")
  3. Call<MovieResponseVO> searchTags(@Query("type") String type, @Query("source") String source);
  4. }

三、二次封装,复用初始化代码

  1. public class HttpSearch extends HttpBase {
  2. private ISearch iSearch;
  3. public HttpSearch(String host) {
  4. super(host);
  5. iSearch = super.create(ISearch.class);
  6. }
  7. public Response<Map> searchTags(String type, String source) throws IOException {
  8. Call<Map> call = iSearch.searchTags(type, source);
  9. return call.execute();
  10. }
  11. }

四、编写用例

  1. public class SearchTagsTest {
  2. private static Properties properties;
  3. private static HttpSearch implSearch;
  4. private static String SCHEMA_PATH = "parameters/search/schema/SearchTagsMovie.json";
  5. @BeforeSuite
  6. public void beforeSuite() throws IOException {
  7. InputStream stream = this.getClass().getClassLoader().getResourceAsStream("env.properties");
  8. properties = new Properties();
  9. properties.load(stream);
  10. String host = properties.getProperty("douban.host");
  11. implSearch = new HttpSearch(host);
  12. stream = this.getClass().getClassLoader().getResourceAsStream("parameters/search/SearchTagsParams.properties");
  13. properties.load(stream);
  14. stream = this.getClass().getClassLoader().getResourceAsStream("");
  15. stream.close();
  16. }
  17. @Test(description = "电影首页。类别:type=movie source=index")
  18. public void testcase1() throws IOException {
  19. String type = properties.getProperty("testcase1.req.type");
  20. String source = properties.getProperty("testcase1.req.source");
  21. Response<Map> response = implSearch.searchTags(type, source);
  22. Map body = response.body();
  23. Assert.assertNotNull(body, "response.body()");
  24. // 响应返回内容想通过schema标准校验
  25. JsonSchemaUtils.assertResponseJsonSchema(SCHEMA_PATH, JSONObject.toJSONString(body));
  26. //校验具体的值
  27. }
  28. @Test(description = "Tv首页。类别:type=tv source=index")
  29. public void testcase2() throws IOException {
  30. String type = properties.getProperty("testcase2.req.type");
  31. String source = properties.getProperty("testcase2.req.source");
  32. Response<Map> response = implSearch.searchTags(type, source);
  33. Map body = response.body();
  34. Assert.assertNotNull(body, "response.body()");
  35. JsonSchemaUtils.assertResponseJsonSchema(SCHEMA_PATH, JSONObject.toJSONString(body));
  36. //校验具体的值
  37. }
  38. }

五、拦截器实现

  1. public class MyInterceptor implements Interceptor {
  2. @Override
  3. public Response intercept(Chain chain) throws IOException {
  4. Request request = chain.request();
  5. Response response = chain.proceed(request);
  6. long time = response.receivedResponseAtMillis() - response.sentRequestAtMillis();
  7. if (time > 100) {
  8. MyReporter.report.log(Status.WARNING, MyReporter.getTestName() + " 接口耗时:" + time);
  9. }
  10. return response;
  11. }
  12. }

注意:需要实现自定义请求头时,在拦截器中进行定义
六、JsonSchema信息获取
https://jsonschema.net/ 访问并获取