我正在使用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 = "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)
我已经看过这些示例,甚至尝试使用相同的代码,但仍然存在错误。我该如何解决这个问题?
答案 0 :(得分: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}"
答案 1 :(得分: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,它覆盖了我放心的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
jar中的restassured
版本就被取代了,一切正常。