使用RestAssuredMockMvc时没有这样的方法错误

问题描述 投票:0回答:2

我正在使用RestAssuredMockMvc进行弹簧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 = "[email protected]";
    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 [email protected]但第二行给出以下错误:

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
2个回答
0
投票

您可能没有将所有依赖项添加到类路径中。如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>

或者是gradle:

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

0
投票

当我使用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,它覆盖了我的restassured 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>

这样,我的httpclient罐子里的restassured版本取代了,一切正常。

© www.soinside.com 2019 - 2024. All rights reserved.