我正在使用以下代码尝试放心的API测试,
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.http.ContentType;
import com.jayway.restassured.mapper.ObjectMapper;
import com.jayway.restassured.response.Response;
import jdk.nashorn.internal.parser.JSONParser;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.jayway.restassured.RestAssured.given;
import static com.jayway.restassured.RestAssured.when;
import static com.jayway.restassured.path.json.JsonPath.from;
import static org.hamcrest.Matchers.hasItem;
public class apiTestj {
public static Response response;
public static String jsonAsString;
@BeforeClass
public static void setupURL() {
// here we setup the default URL and API base path to use throughout the tests
RestAssured.baseURI = "http://api.wunderground.com";
RestAssured.basePath = "/api/dddd88ab2ff59656/geolookup/";
}
@BeforeClass
public static void callZipcodeAPI() {
response =
when().
get("/q/77001.json").
then().
contentType(ContentType.JSON). // check that the content type return from the API is JSON
extract().response(); // extract the response
// Convert the JSON response to a string, and save it in a variable called 'jsonAsString'
jsonAsString = response.asString();
}
@Test
public void checkAllCities()
{
//System.out.println(jsonAsString);
List<String> airport_cities = from(jsonAsString).get("location.nearby_weather_stations.airport.station.city");
System.out.println(airport_cities.size());
for (int i = 0; i < airport_cities.size(); i++) {
System.out.println(airport_cities.get(i));
}
String c = from(jsonAsString).get("location.nearby_weather_stations.airport.station[-1].city");
System.out.println("The last element in the list : " + c);
System.out.println("---------------------------------------------------");
List<String> pws_neighborhood = from(jsonAsString).get("location.nearby_weather_stations.pws.station.neighborhood");
System.out.println(pws_neighborhood.size());
for (int i = 0; i < pws_neighborhood.size(); i++) {
//System.out.println(i);
System.out.println(pws_neighborhood.get(i));
}
}
}
和以下pom文件 -
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yourcompany.restassured</groupId>
<artifactId>restassuredtest</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<directory>${project.basedir}/target</directory>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<finalName>${project.artifactId}-${project.version}</finalName>
<testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>${project.basedir}/src/test/resources</directory>
</testResource>
</testResources>
</build>
<repositories>
<repository>
<id>central</id>
<name>bintray</name>
<url>http://jcenter.bintray.com</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-service</artifactId>
<version>0.13.1</version>
</dependency>
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libfb303</artifactId>
<version>0.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.19</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.19</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>0.13.0</version>
</dependency>
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-core</artifactId>
<version>1.2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>rest-assured</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>pl.pragmatists</groupId>
<artifactId>JUnitParams</artifactId>
<version>1.0.4</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<!-- Needs to be the same version that REST Assured depends on -->
<version>2.4.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<scm.branch>master</scm.branch>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<groovy.version>2.4.4</groovy.version>
<gmaven.version>1.5</gmaven.version>
<hamcrest.version>1.3</hamcrest.version>
<jackson1.version>1.9.11</jackson1.version>
<jackson2.version>2.4.3</jackson2.version>
<maven-javadoc.version>2.9.1</maven-javadoc.version>
</properties>
</project>
结束了这个错误,虽然我没有导入testng-
Exception in thread "main" java.lang.NoClassDefFoundError: com/beust/jcommander/ParameterException
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:117)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.ClassNotFoundException: com.beust.jcommander.ParameterException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 6 more
我在IntelliJ 2016.1.1上使用Java 8,JDK 1.8。如果你能帮我解决这个问题,我真的很感激。