【问题标题】:No such method error while using RestAssuredMockMvc使用 RestAssuredMockMvc 时没有此类方法错误
【发布时间】:2015-08-07 13:27:08
【问题描述】:

我正在使用 RestAssuredMockMvc 对 spring mvc 控制器进行单元测试。 这是我的代码:

import com.xyz.api.controller.UserController;
import com.jayway.restassured.module.mockmvc.RestAssuredMockMvc;
import org.junit.Before;
import org.junit.Test;

import javax.servlet.http.HttpServletResponse;

import static org.hamcrest.Matchers.equalTo;

public class UserControllerRest {

    private String userName = "xyz@example.com";
    private String expectedResult = "Hello " + userName;

    @Before
    public void initialize() {
        RestAssuredMockMvc.standaloneSetup(new UserController());
    }

    @Test
    public void checkResponseAndBody() throws Exception {
        RestAssuredMockMvc.given().param("userName", userName).when().get("/users").print();
        RestAssuredMockMvc.given().param("userName", userName).when().get("/users").then().assertThat().statusCode(200).and().body(equalTo(expectedResult));
    }
}

checkResponseAndBody() 中的第一条语句打印:Hello xyz@example.com 但是第二行给出了以下错误:

java.lang.NoSuchMethodError: com.jayway.restassured.internal.ValidatableResponseOptionsImpl.<init>(Ljava/lang/String;Lcom/jayway/restassured/internal/ResponseParserRegistrar;Lcom/jayway/restassured/config/RestAssuredConfig;Lcom/jayway/restassured/response/Response;Lcom/jayway/restassured/response/ExtractableResponse;)V
    at com.jayway.restassured.module.mockmvc.internal.ValidatableMockMvcResponseImpl.<init>(ValidatableMockMvcResponseImpl.java:37)
    at com.jayway.restassured.module.mockmvc.internal.MockMvcRestAssuredResponseImpl.then(MockMvcRestAssuredResponseImpl.java:36)
    at com.giddh.api.restassured.UserControllerRest.checkResponseAndBody(UserControllerRest.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

我已经看过示例,甚至尝试使用相同的代码,但错误仍然存​​在。我该如何解决这个问题?

【问题讨论】:

    标签: java spring unit-testing rest-assured


    【解决方案1】:

    您可能尚未将所有依赖项添加到类路径中。正如download 页面所示,您还需要下载rest-assured-2.5.0-dist.zip 的内容并将其放入您的类路径中。

    如果您使用的是 Maven,那么仅依赖 spring-mock-mvc 模块就足够了:

    <dependency>
          <groupId>com.jayway.restassured</groupId>
          <artifactId>spring-mock-mvc</artifactId>
          <version>${rest-assured.version}</version>
          <scope>test</scope>
    </dependency>
    

    或者毕业:

    testCompile "com.jayway.restassured:spring-mock-mvc:${rest-assured.version}"
    

    【讨论】:

      【解决方案2】:

      在使用 Maven 时,我遇到了这种情况(令人惊讶的是,Gradle 没有给我这个问题,它处理得很好,不像 Maven)。

      对我来说,这是一个依赖冲突。我的 pom.xml 中有 Apache Phoenix 作为依赖项

          <groupId>org.apache.phoenix</groupId>
          <artifactId>phoenix</artifactId>
          <version>4.13.2-cdh5.11.2</version>
      

      这个 Apache Phoenix jar 依赖于 httpclient version 4.0.1 jar,它覆盖了我放心的 jar 所依赖的版本(即 httpclient version 4.3.5)。默认情况下,Maven 已排除此更高版本 (4.5.3)。 为了解决这个问题,我在我的 Apache Phoenix pom 部分下为旧版本添加了 exclusion,如下所示:

                  <dependency>
      
                    <groupId>org.apache.phoenix</groupId>
                    <artifactId>phoenix-core</artifactId>
                    <version>4.13.2-cdh5.11.2</version>
      
                    <exclusions>
                      <exclusion>
                         <groupId>org.apache.httpcomponents</groupId>
                         <artifactId>httpclient</artifactId>
                      </exclusion>
                    </exclusions>
      
                </dependency>
      

      这样,我的restassured jar 中的httpclient 版本被取代,一切正常。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-09-30
        • 2018-08-05
        • 2022-01-21
        • 2021-10-13
        • 1970-01-01
        • 2014-06-20
        • 1970-01-01
        相关资源
        最近更新 更多