-
Notifications
You must be signed in to change notification settings - Fork 28
Api test(Rest-assured and RetroFit) #91
base: master
Are you sure you want to change the base?
Changes from all commits
0f336a4
2d67dc6
8a5c2c6
8e412e3
83da138
aa14bab
ecdf10a
1bd0eb6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <parent> | ||
| <artifactId>school-2016</artifactId> | ||
| <groupId>ru.qatools.school</groupId> | ||
| <version>1.0-SNAPSHOT</version> | ||
| </parent> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <artifactId>api-tests-module</artifactId> | ||
|
|
||
| <dependencies> | ||
|
|
||
| <dependency> | ||
| <groupId>com.jayway.restassured</groupId> | ||
| <artifactId>rest-assured</artifactId> | ||
| <version>2.9.0</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.squareup.retrofit2</groupId> | ||
| <artifactId>retrofit</artifactId> | ||
| <version>2.0.2</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.google.code.gson</groupId> | ||
| <artifactId>gson</artifactId> | ||
| <version>2.6.2</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.squareup.retrofit2</groupId> | ||
| <artifactId>converter-gson</artifactId> | ||
| <version>2.0.2</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.hamcrest</groupId> | ||
| <artifactId>hamcrest-all</artifactId> | ||
| <version>1.3</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>ru.yandex.qatools.allure</groupId> | ||
| <artifactId>allure-junit-adaptor</artifactId> | ||
| <version>1.4.23</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>ru.yandex.qatools.matchers</groupId> | ||
| <artifactId>collection-matchers</artifactId> | ||
| <version>1.3</version> | ||
| </dependency> | ||
|
|
||
| </dependencies> | ||
|
|
||
|
|
||
| </project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package ru.qatools.school.apitests; | ||
|
|
||
| import com.jayway.restassured.response.Response; | ||
| import org.apache.http.HttpStatus; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import com.jayway.restassured.builder.RequestSpecBuilder; | ||
| import com.jayway.restassured.specification.RequestSpecification; | ||
| import ru.qatools.school.apitests.data.City; | ||
| import ru.qatools.school.apitests.data.WeatherInfo; | ||
| import ru.yandex.qatools.allure.annotations.Title; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| import static com.jayway.restassured.RestAssured.given; | ||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.hamcrest.core.Is.is; | ||
|
|
||
| /** | ||
| * @author Gavrilov_IS | ||
| */ | ||
| public class RestAssureAPITest { | ||
| private static final String MAIN_PAGE = "http://weather.lanwen.ru/"; | ||
| private static final String BASE_PATH = "api"; | ||
| private final String CITY = "Moscow"; | ||
| private final int CITY_LIMIT = 5; | ||
|
|
||
| private RequestSpecification reqSpec; | ||
|
|
||
| @Before | ||
| public void init() { | ||
| reqSpec = new RequestSpecBuilder().build() | ||
| .baseUri(MAIN_PAGE) | ||
| .basePath(BASE_PATH); | ||
| } | ||
|
|
||
| @Test | ||
| @Title("Проверяем, что запрос обработан и город в ответе соответствует запрошенному") | ||
| public void shouldGetInfoForCityInRequest() throws IOException { | ||
| WeatherInfo response = given().spec(reqSpec) | ||
| .param("city", CITY) | ||
| .expect().statusCode(HttpStatus.SC_OK) | ||
| .get("weather") | ||
| .as(WeatherInfo.class); | ||
| assertThat("Город в ответе должен соответствовать запрошенному", response.getCity(), is(CITY)); | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| @Title("Запрос на /cities с параметром limit=CITY_LIMIT вернул указанное количество записей") | ||
| public void shouldGetExpectedCountOfCities() throws IOException { | ||
| Response response = given().spec(reqSpec) | ||
| .param("limit", 5) | ||
| .get("cities"); | ||
| response.then().assertThat() | ||
| .statusCode(HttpStatus.SC_OK); | ||
| City[] cities = response.as(City[].class); | ||
| assertThat("Количество городов должно соответствовать указанному лимиту", cities.length, is(CITY_LIMIT)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package ru.qatools.school.apitests; | ||
|
|
||
| import org.apache.http.HttpStatus; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import static org.junit.Assert.*; | ||
| import retrofit2.Response; | ||
| import retrofit2.Retrofit; | ||
| import retrofit2.converter.gson.GsonConverterFactory; | ||
| import ru.qatools.school.apitests.data.City; | ||
| import ru.qatools.school.apitests.data.WeatherInfo; | ||
| import ru.qatools.school.apitests.interfaces.WeatherAPI; | ||
| import ru.yandex.qatools.allure.annotations.Title; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
|
|
||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.hamcrest.core.Is.is; | ||
|
|
||
| /** | ||
| * @author Gavrilov_IS | ||
| */ | ||
| public class RetrofitAPITest { | ||
| private final String BASE_URL = "http://weather.lanwen.ru/api/"; | ||
| private WeatherAPI weather; | ||
| private final String CITY = "Moscow"; | ||
| private final int CITY_LIMIT = 5; | ||
|
|
||
| Retrofit client = new Retrofit.Builder() | ||
| .baseUrl(BASE_URL) | ||
| .addConverterFactory(GsonConverterFactory.create()) | ||
| .build(); | ||
|
|
||
| @Before | ||
| public void init() { | ||
| weather = client.create(WeatherAPI.class); | ||
| } | ||
|
|
||
| @Test | ||
| @Title("Проверяем, что запрос обработан и город в ответе соответствует запрошенному") | ||
| public void shouldGetInfoForCityInRequest() throws IOException { | ||
| Response<WeatherInfo> response = weather.getWeather(CITY).execute(); | ||
| assertThat("Успешная обработка запроса", response.code(), is(HttpStatus.SC_OK)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это сообщение появится если ассерт упадет, похоже, обработка запроса будет неуспешная |
||
| assertThat("Город в ответе должен соответствовать запрошенному", response.body().getCity(), is(CITY)); | ||
| } | ||
|
|
||
| @Test | ||
| @Title("Запрос на /cities с параметром limit=CITY_LIMIT вернул указанное количество записей") | ||
| public void shouldGetExpectedCountOfCities() throws IOException { | ||
| Response<List<City>> response = weather.getCitiesWithLimit(CITY_LIMIT).execute(); | ||
| assertThat("Успешная обработка запроса", response.code(), is(HttpStatus.SC_OK)); | ||
| assertEquals("Город в ответе должен соответствовать запрошенному", response.body().size(), CITY_LIMIT); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Копипаст, сообщение вообще не о том |
||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package ru.qatools.school.apitests.data; | ||
|
|
||
| /** | ||
| * Created by Gavrilov_IS on 11.05.2016. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. У тебя то |
||
| */ | ||
| public class City { | ||
| private int UID; | ||
| private String name; | ||
| private String country; | ||
|
|
||
| public String getName(){ | ||
| return name; | ||
| } | ||
|
|
||
| public int getUID(){ | ||
| return UID; | ||
| } | ||
|
|
||
| public String getCountry(){ | ||
| return country; | ||
| } | ||
|
|
||
| public void setName(String name){ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Не очень понятно, зачем тебе сеттеры у этих объектов |
||
| this.name = name; | ||
| } | ||
|
|
||
| public void setUID(int UID){ | ||
| this.UID = UID; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package ru.qatools.school.apitests.data; | ||
|
|
||
| /** | ||
| * @author Gavrilov_IS | ||
| */ | ||
| public class Temperature { | ||
| private String unit; | ||
| private double value; | ||
|
|
||
| public String getUnit(){ | ||
| return unit; | ||
| } | ||
|
|
||
| public double getValue(){ | ||
| return value; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString(){ | ||
| return "unit = " + unit + "; digit = " + value + "; \r\n"; | ||
| } | ||
|
|
||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package ru.qatools.school.apitests.data; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * @author Gavrilov_IS | ||
| */ | ||
| public class WeatherInfo { | ||
| private String city; | ||
| private List<Temperature> temperatures; | ||
|
|
||
| public String getCity(){ | ||
| return city; | ||
| } | ||
|
|
||
| public List<Temperature> getListOfTemperatures(){ | ||
| return temperatures; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package ru.qatools.school.apitests.interfaces; | ||
|
|
||
| import retrofit2.Call; | ||
| import retrofit2.http.GET; | ||
| import retrofit2.http.Query; | ||
| import ru.qatools.school.apitests.data.City; | ||
| import ru.qatools.school.apitests.data.WeatherInfo; | ||
|
|
||
| import java.util.List; | ||
|
|
||
|
|
||
| /** | ||
| * @author Gavrilov_IS | ||
| */ | ||
| public interface WeatherAPI { | ||
| @GET("cities") | ||
| Call<List<City>> getCities(); | ||
|
|
||
| @GET("cities") | ||
| Call<List<City>> getCitiesWithLimit(@Query ("limit") int lim); | ||
|
|
||
| @GET("suggest") | ||
| Call<List<City>> getSuggest(@Query ("query") String str); | ||
|
|
||
| @GET("weather") | ||
| Call<WeatherInfo> getWeather(@Query("city") String city); | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,7 +11,7 @@ | |
| </parent> | ||
|
|
||
| <artifactId>commons-module</artifactId> | ||
| <packaging>jar</packaging> | ||
| <packaging>pom</packaging> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А зачем? |
||
|
|
||
| <name>Commons Module</name> | ||
|
|
||
|
|
||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ты тайтлы будешь видеть в аллюре и тестпалме, там нет CITY_LIMIT