gpt4 book ai didi

java - 在 Spring Boot 中使用 MockMvc 进行测试

转载 作者:行者123 更新时间:2023-11-28 21:18:53 25 4
gpt4 key购买 nike

我在将 MockMvc 引入 Spring(使用 Spring Boot)时遇到问题。我有一个学习测试的小代码

import static org.hamcrest.Matchers.containsString;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class ApplicationTest04_ownServer {

@Autowired
private MockMvc mockMvc;

@Test
public void shouldReturnDefaultMessage() throws Exception {
this.mockMvc
.perform(get("/"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(containsString("Hello")));
}

好的。我的pom.xml是这个

[...]
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.19.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>org.springframework</groupId>
<artifactId>Prueba</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Prueba</name>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>compile</scope>
</dependency>

</dependencies>

我有几个测试。可行,但我在使用 Mock Object 或类似工具进行测试时遇到问题。例如,在测试中, Controller 回复我一条文本。

@Controller
public class HomeController {

@RequestMapping("/")
public @ResponseBody String greeting() {
return "Hello";
}

}

在测试中(上面第一段代码),是这样的代码

@Test
public void shouldReturnDefaultMessage() throws Exception {
this.mockMvc
.perform(get("/"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(containsString("Hello")));
}

因此,我希望 Controller 的响应与测试的响应(“hello”)相同,但 Junit 测试是错误的。

java.lang.SecurityException: class "org.hamcrest.Matchers"'s signer information does not match 

打印结果

MockHttpServletRequest:
HTTP Method = GET
Request URI = /
Parameters = {}
Headers = {}

Handler:
Type = hello.HomeController
Method = public java.lang.String hello.HomeController.greeting()

Async:
Async started = false
Async result = null

Resolved Exception:
Type = null

ModelAndView:
View name = null
View = null
Model = null

FlashMap:
Attributes = null

MockHttpServletResponse:
Status = 200
Error message = null
Headers = {Content-Type=[text/plain;charset=UTF-8], Content-Length=[5]}
Content type = text/plain;charset=UTF-8
Body = Hello
Forwarded URL = null
Redirected URL = null
Cookies = []

MockHttpServletRequest:
HTTP Method = GET
Request URI = /
Parameters = {}
Headers = {}

Handler:
Type = hello.HomeController
Method = public java.lang.String hello.HomeController.greeting()

Async:
Async started = false
Async result = null

Resolved Exception:
Type = null

ModelAndView:
View name = null
View = null
Model = null

FlashMap:
Attributes = null

MockHttpServletResponse:
Status = 200
Error message = null
Headers = {Content-Type=[text/plain;charset=UTF-8], Content-Length=[5]}
Content type = text/plain;charset=UTF-8
Body = Hello
Forwarded URL = null
Redirected URL = null
Cookies = []

body react 是你好,不是吗?有什么想法吗?

观察:这个例子在 Eclipse Neon 中有效,但现在我使用 Eclipse 的最新版本。我有很多错误(大多数类型没有出现:MockMvc、SpringRunner、SpringBootTest 等)解决方案是更改依赖项的范围(测试 --> 编译)。现在这是依赖

    <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>compile</scope>
</dependency>

这与问题有关吗?

提前致谢

最佳答案

依赖范围应该只是测试。根据下面的 Maven 文档,是对 scope = test 的描述

  • 测试

该作用域表明该依赖对于应用程序的正常使用而言不是必需的,仅在测试编译和执行阶段可用

正如评论中强调的那样,不要使用 war 使用 jar。您可以删除 tomcat 依赖项,spring boot 将看到 spring web 依赖项并自动提供嵌入式 tomcat。如果您的目的是仅测试 Controller 行为,那么您应该使用 spring boot 测试切片,在本例中为 web 切片。所以你可以用 @WebMvcTest 注释你的测试下面是一个很好的例子,你一定要检查一下。

https://spring.io/guides/gs/testing-web/

希望对你有帮助

关于java - 在 Spring Boot 中使用 MockMvc 进行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54543010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com