【问题标题】:Rest assured with TestNG ins not working with Maven project请放心,TestNG 插件不能与 Maven 项目一起使用
【发布时间】:2014-01-21 16:35:51
【问题描述】:

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

<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

对应符号为get等,放心使用。

这里问的是完整的错误信息::

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

【问题讨论】:

  • 你能显示完整的错误信息吗?
  • 这里是完整的错误信息:: 编辑了带有完整信息的问题。 @khmarbaise

标签: java rest maven testng rest-assured


【解决方案1】:

问题似乎与依赖有关。

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

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

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

属于包com.jayway.restassured.response,而与restassured相关的类中唯一的imports是

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>

【讨论】:

  • 谢谢,问题已修复。导入包:import static com.jayway.restassured.RestAssured.expect;导入静态 com.jayway.restassured.RestAssured.get;导入静态 com.jayway.restassured.RestAssured.given; + 添加了你的.. 现在一切正常.. 我被卡住的唯一地方是stackoverflow.com/questions/20931722/… .
【解决方案2】:

请注意groupId。其中有两个可用。放心的罐子需要的是: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 依赖项,因为它现在已完全嵌入到上面给出的可靠工件中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-20
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    • 2016-01-12
    • 1970-01-01
    • 2013-04-16
    • 2013-11-25
    相关资源
    最近更新 更多