3

我使用 maven 在 pom.xml 中为我的放心项目添加了以下依赖项列表:

<dependency>
    <groupId>com.jayway.restassured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>2.1.0</version>
    <scope>test</scope>
</dependency>

现在我正在尝试运行以下示例代码:

import static com.jayway.restassured.RestAssured.*;
import static com.jayway.restassured.matcher.RestAssuredMatchers.*;
import static org.hamcrest.Matchers.*;
import java.util.*;
import org.testng.Assert;
import org.testng.annotations.*;

public class TestNGTest1 {

    private Collection collection;

    @Test
    public void testGetSingleUserProgrammatic() {
        Response res = get("/service/single-user");            
        assertEquals(200, res.getStatusCode());
        String json = res.asString();
        JsonPath jp = new JsonPath(json);
        assertEquals("test@hascode.com", jp.get("email"));
        assertEquals("Tim", jp.get("firstName"));
        assertEquals("Testerman", jp.get("lastName"));
        assertEquals("1", jp.get("id"));
    }
}

但这在做的时候会抛出错误mvn test

错误是:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:testCompile (default-testCompile) on project GBAppAuomation: Compilation failure: Compilation failure:
[ERROR] /home/jay/temp/GB_MVN_Proj/GBAppAuomation/src/test/java/com/glassbeam/app/TestNGTest1.java:[50,2] error: cannot find symbol
[ERROR] class TestNGTest1
[ERROR] /home/jay/temp/GB_MVN_Proj/GBAppAuomation/src/test/java/com/glassbeam/app/TestNGTest1.java:[53,2] error: cannot find symbol

对应的符号是getetc,放心使用。

正如这里所问的是完整的错误消息::

Here is the complete error message::   **

[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ GBAppAuomation ---
[INFO] Compiling 1 source file to /home/jay/temp/GB_MVN_Proj/GBAppAuomation/target/test-classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /home/jay/temp/GB_MVN_Proj/GBAppAuomation/src/test/java/com/glassbeam/app/TestNGTest1.java:[50,1] error: cannot find symbol
[ERROR]  class TestNGTest1
/home/jay/temp/GB_MVN_Proj/GBAppAuomation/src/test/java/com/glassbeam/app/TestNGTest1.java:[50,17] error: cannot find symbol
[ERROR]  class TestNGTest1
/home/jay/temp/GB_MVN_Proj/GBAppAuomation/src/test/java/com/glassbeam/app/TestNGTest1.java:[53,2] error: cannot find symbol
[ERROR]  class TestNGTest1
/home/jay/temp/GB_MVN_Proj/GBAppAuomation/src/test/java/com/glassbeam/app/TestNGTest1.java:[53,20] error: cannot find symbol
[INFO] 4 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.384s
[INFO] Finished at: Sat Jan 04 15:26:57 IST 2014
[INFO] Final Memory: 16M/202M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:testCompile (default-testCompile) on project GBAppAuomation: Compilation failure: Compilation failure:
[ERROR] /home/jay/temp/GB_MVN_Proj/GBAppAuomation/src/test/java/com/glassbeam/app/TestNGTest1.java:[50,1] error: cannot find symbol
[ERROR] class TestNGTest1
[ERROR] /home/jay/temp/GB_MVN_Proj/GBAppAuomation/src/test/java/com/glassbeam/app/TestNGTest1.java:[50,17] error: cannot find symbol
[ERROR] class TestNGTest1
[ERROR] /home/jay/temp/GB_MVN_Proj/GBAppAuomation/src/test/java/com/glassbeam/app/TestNGTest1.java:[53,2] error: cannot find symbol
[ERROR] class TestNGTest1
[ERROR] /home/jay/temp/GB_MVN_Proj/GBAppAuomation/src/test/java/com/glassbeam/app/TestNGTest1.java:[53,20] error: cannot find symbol
4

2 回答 2

1

该问题似乎与依赖关系有关。

IDE 中是否有任何编译错误?

因为下面一行的界面Response(可能是第50行的错误)

Response res = get("/service/single-user");

属于 package com.jayway.restassured.response,而import类中唯一相关的restassured

import static com.jayway.restassured.RestAssured.*;
import static com.jayway.restassured.matcher.RestAssuredMatchers.*; 

以及来自的JsonPath类(可能是错误的第 53 行)

JsonPath jp = new JsonPath(json);

属于包com.jayway.jsonpath,也不进口。这个包也应该作为依赖包含在 POM 中:

<dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    <version>0.9.1</version>
</dependency>
于 2014-01-05T13:45:59.187 回答
0

请注意groupId。其中有两个可用。Restassured jars 需要的是:io.rest-assured.

目前,这是最新版本:

<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>3.0.3</version>
<scope>test</scope>
</dependency>

如果有更新的版本可用,您只需将依赖项中的版本号从 3.0.3 更改为新版本即可。

供您参考,不再需要添加独立的 JsonPath 依赖项,因为它现在完全嵌入到上面给出的放心工件中

于 2017-09-30T13:45:19.717 回答