- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在将 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/
我有一个想要测试的 REST 端点。通过 POST 请求命中此端点会上传远程 git 存储库中的文件。我正在尝试使用mockMvc测试对此端点的POST调用(我只想查看返回状态“isOk()”)——我
当我使用 WebApplicationContext 创建 MockMvc 以使用 Spring MVC 3.2.3 进行 restful-webservice 测试时,如下所示: @Autowire
我尝试测试注入(inject) UserPrincipal 的 mvcControllers 的访问: restPockMockMvc .perform(get("/pocs").principal(
我有一个带有 MySQL 数据库的简单 REST 应用程序,一切正常,但是在测试时我们是否需要创建一个虚拟对象并对其进行测试,或者通过模拟数据库进行测试?虚拟对象有相当大的构造函数和嵌套类,这是一个很
我有一个使用 MockMvc 的 springBoot 2.1.9.RELEASE 应用程序。 我想知道是否有办法从文件中获取正文内容 mockMvc.perform(post("/hostel")
我想测试包含请求正文的请求类型 post 的其余 Controller 方法。所有其他获取方法在发布后都可以正常工作。我期待 200 OK 但它返回 417。我的目的是让它通过我运行测试但失败了。我是
我正在尝试添加 Cucumber到我已经在使用 spring-test 和 JUnit 的 Spring Web MVC 项目。我已经编写的非 Cucumber 集成测试 Autowiring 了 W
我想向 Controller 发送一个由文件和简单类型组成的复杂对象。 public class ContributionNew { private List elementsToAdd;
我正在为我的 Spring 数据休息存储库编写测试用例: @RepositoryRestResource(collectionResourceRel = "teams", path = "teams"
当我模拟传递有效id时,测试运行成功,但是当我传递无效id时,返回返回404,这是预期的,但没有响应,当我正常的Http请求时,它返回响应正文,消息未找到,状态404。我在这里做错了什么? @RunW
当我使用以下 Controller /测试配置收到“请求的映射错误”时,目前正在努力解决问题。 Controller : @Slf4j @Validated @RestController @Requ
在我的测试中,我设置了 MockMvc @Before 中的对象像这样 mockMvc = MockMvcBuilders.webAppContextSetup(context)
有人有任何提示,或者有人知道如何测试 HTTP 响应对象返回的“错误消息”吗? @Autowired private WebApplicationContext ctx; private MockMv
我正在为 Spring Rest 服务编写测试,它将 url 重定向到另一个 Spring 服务。所以目标是使用书签名称找到书签。第一个服务使用 Bookmark 名称获取 BookmarkId,然后
我需要测试一个调用异步服务的 Controller 。 Controller 代码 @RequestMapping(value = "/path", method = RequestMethod.PO
我刚刚使用 MockMvc 为 Controller 创建了一个简单的集成测试。一切正常,但即使 Controller 方法返回某些内容也没有任何响应。这是 Controller : import d
我编写了 Controller ,它为每个映射使用不同的值。现在我将其折射为所有映射使用相同的值,但是我不知道如何进行测试,因为它对每个映射都返回 404。 这是我的 Controller : @Cr
我想在 Spring Boot 中为我的 Controller 创建一些测试。具体来说,我想创建一个测试来处理表单以添加新项目。该项目属于 Drug 类,并且有一个集合作为 参数: @ManyToMa
使用 Spring boot 2 和 Spring mvc。我正在尝试使用 mockMvc 测试我的休息 Controller @PostMapping( value = "/
大多数时候,我们不是在普通的 JUnit 断言中添加注释,而是向断言添加一条消息,以解释为什么这是断言: Person p1 = new Person("Bob"); Person p2 = new
我是一名优秀的程序员,十分优秀!