我有MockMvc问题到Spring(与Spring启动)。我有一个小的代码来了解测试
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>
我有几个测试。这些作品,但我有与仿制对象或类似的测试问题。例如,在测试中,控制器响应我的文字。
@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")));
}
于是,我想到的是控制器的响应是一样的,测试(“你好”),但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 = []
身体的反应是你好,isn`t是什么?任何想法?
观察:此示例工作在Eclipse的霓虹灯,但现在我使用Eclipse的最新版本。我有很多错误的(最类型不会出现:MockMvc,SpringRunner,SpringBootTest等)的解决方案是改变依赖的范围(测试 - >编译)。现在,这是依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>compile</scope>
</dependency>
它可以有事情做的问题?
提前致谢
依赖的范围应当只测试。按照行家文档下列是为范围=测试的描述
此范围表明,依赖并不需要正常使用的应用程序,并且仅适用于测试编译和执行阶段
正如评论强调,不使用战争使用的jar。你可以删除tomcat的依赖以及春季开机就会看到春天的网络依赖,并会自动提供嵌入式的Tomcat。如果你的意图是仅测试控制器行为,那么你应该用春天开机试片,在这种情况下,网页快讯。所以,你可以注释你的测试与@WebMvcTest下面是一个很好的例子和u一定要检查了这一点。
https://spring.io/guides/gs/testing-web/
希望这可以帮助
由于chrylis和whysoseriousson。现在工作
有了这些想法,我已经开发出一种新的代码(仅罐到pom.xml的),我有“测试的依赖”的范围设置为“测试”
[...]
<packaging>war</packaging>
[...]
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
我有一个aditional的问题,因为测试文件不进入“测试包”。我已经感动了所有的测试文件,以“测试包”。这是curiosus,它在过去的版本工作到霓虹灯Eclipse和不
我知道(Test Tutorial of Spring)和我的大多数例子的想法本教程。
感谢您的回答。问题已关闭