Spring Boot 模拟创建的 POST 响应
Posted
技术标签:
【中文标题】Spring Boot 模拟创建的 POST 响应【英文标题】:Spring boot mock CREATED POST response 【发布时间】:2018-10-21 07:11:27 【问题描述】:对 spring-boot 完全陌生。
我正在尝试为我的 POST 方法编写一个测试,该方法返回一个带有资源路径的 CREATED 响应。我遇到了各种各样的问题,因为我认为这些问题非常简单。在下面的代码中是我当前的测试类,它似乎因
而崩溃ClassNotFoundException
在我的方法的第一行。
@RunWith(SpringRunner.class)
@WebMvcTest(AResource.class)
public class AResourceTest
@Autowired
private MockMvc mockMvc;
@MockBean
private AResource aResourceMock;
@Test
public void testPOST() throws Exception
Response.ResponseBuilder created = Response.created(UriBuilder.fromPath("/123").build());
when(aResourceMock.create(123)).thenReturn(created.build());
mockMvc.perform(MockMvcRequestBuilders
.request(HttpMethod.POST, "/account")
.content("\t\"id\": \"123\""))
.andExpect(MockMvcResultMatchers.status().isCreated());
知道如何正确模拟我的回复吗?谢谢
编辑: 下面的堆栈跟踪
java.lang.RuntimeException: java.lang.ClassNotFoundException: com.sun.ws.rs.ext.RuntimeDelegateImpl
at javax.ws.rs.ext.RuntimeDelegate.findDelegate(RuntimeDelegate.java:122)
at javax.ws.rs.ext.RuntimeDelegate.getInstance(RuntimeDelegate.java:91)
at javax.ws.rs.core.UriBuilder.newInstance(UriBuilder.java:69)
at javax.ws.rs.core.UriBuilder.fromPath(UriBuilder.java:111)
at com.powerman.RestController.AResourceTest.canPOST(AResourceTest.java:34)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
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.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
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.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.ClassNotFoundException: com.sun.ws.rs.ext.RuntimeDelegateImpl
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at javax.ws.rs.ext.FactoryFinder.newInstance(FactoryFinder.java:62)
at javax.ws.rs.ext.FactoryFinder.find(FactoryFinder.java:155)
at javax.ws.rs.ext.RuntimeDelegate.findDelegate(RuntimeDelegate.java:105)
... 34 more
我将添加我的依赖项,以防它们有帮助:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.0.1.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>21.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.17.1</version>
</dependency>
</dependencies>
【问题讨论】:
你需要在你的maven依赖中添加spring-test,例如:我不确定您要做什么以及为什么要模拟 Response
。当您为您的资源运行测试时,通常您想要做的是运行集成测试。这需要使用客户端库向端点发出实际请求。 MockMvc
仅在您使用 Spring MVC 作为 REST 框架时有效。但在您的情况下,您使用的是 Jersey(我假设,基于您帖子中的 jax-rs 标签。
就客户端库而言,您可以使用Jersey/JAX-RS Client API,也可以使用Spring Boot 的TestRestTemplate
,就像在official Spring Boot/Jersey sample project 中一样。要检查 POST/Created 资源的已创建 URI,您应该检查响应中的 Location
标头。这是在您执行Response.created(URI)
时在服务器端设置的。例如,假设这是您的资源
@Path("customers")
public class CustomersResource
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response createCustomer(Customer customer, @Context UriInfo uriInfo)
int customerId = // create customer and get the resource id
UriBuilder builder = uriInfo.getAbsolutePathBuilder();
builder.path(Integer.toString(customerId));
return Response.created(builder.build()).build();
Jersey 会自动将Location
设置为传递给created()
的URI 的值
那么(如果您使用 Jersey 客户端),那么您的测试可能类似于
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ExampleResourceTest
@LocalServerPort
private int port;
private Client client = ClientBuilder.newClient();
@Test
public void testCustomerLocationOnPost()
Customer customer = new Customer("Jane", "Doe");
URI resourceUri = UriBuilder.fromUri("http://localhost")
.port(port).path("api").path("customers").build();
Response response = client.target(resourceUri).request().post(Entity.json(customer));
// test that we get the correct status code
assertThat(response.getStatus())
.isEqualTo(Response.Status.CREATED.getStatusCode());
// test that the location header is correct.
assertThat(response.getHeaderString(HttpHeaders.LOCATION))
.isEqualTo(UriBuilder.fromUri(resourceUri).path("123").build().toString());
就依赖项而言,要将 Jersey 与 Spring Boot 一起使用,您需要使用 spring-boot-started-jersey
依赖项。还要摆脱您拥有的 jersey-core
依赖项。有关完整示例,请参见上面的链接项目。
【讨论】:
在进行发布请求时导致InjectionManagerFactory not found
。调查一下
它与您在 Jersey 中使用的依赖版本有关。这个问题从 2.26 开始,要修复它,您需要添加 jersey-hk2 依赖项。
调查提到这一点的***.com/questions/44088493/…。但是,我在ClientBuilder.newClient();
上回到java.lang.ClassNotFoundException: org.glassfish.jersey.client.JerseyClientBuilder
。我不知道为什么它这么乱。如果不让它变得更容易,那么 spring-boot 的意义何在:/
您需要使用spring-boot-starter-jersey
。并摆脱jersey-core
。看我链接的官方例子。
Here's 另一个简单的应用程序设置(您只需添加 spring-boot-starter-test 即可)。这里是docs。其实很简单。如果您不知道自己在做什么并且阅读了错误的学习资源,这只会变得复杂。【参考方案2】:
尝试添加到您的 pom.xml:
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.27</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>2.27</version>
<scope>provided</scope>
</dependency>
【讨论】:
没有影响以上是关于Spring Boot 模拟创建的 POST 响应的主要内容,如果未能解决你的问题,请参考以下文章
向服务器发送 POST 请求,服务器的响应为空,Angular - Spring-boot
Spring Boot + Spring Security 应用程序中 POST/PUT/DELETE 请求的 403 响应
我想在 Spring Boot 集成测试中创建 bean 之前模拟服务器
Spring Boot - 如何通过带有查询参数的 url 发送 POST 请求并将响应存储在方法中?