[英]Testing with MockMvc into SpringBoot
我對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")));
}
好。 Mi 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")));
}
因此,我希望控制器的響應與測試(“ 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 = []
身體反應是你好,不是嗎? 任何想法?
觀察:該示例在Eclipse Neon中有效,但是現在我使用的是Eclipse的最新版本。 我有很多錯誤(大多數類型不會出現:MockMvc,SpringRunner,SpringBootTest等)。解決方案是更改依賴項的范圍(測試->編譯)。 現在這是依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>compile</scope>
</dependency>
它可以與問題有關嗎?
提前致謝
依賴范圍應該僅是測試。 根據下面的Maven文檔,范圍=測試的描述
此范圍表明依賴關系對於正常使用應用程序不是必需的,並且僅在測試編譯和執行階段可用
如評論中突出顯示的那樣,不要使用war use jar。您可以刪除tomcat依賴項,並且spring boot會看到spring Web依賴項並會自動提供嵌入式tomcat。 如果您打算僅測試控制器行為,則應使用Spring Boot測試片,在這種情況下為Web片。 因此,您可以使用@WebMvcTest注釋測試,以下是一個很好的示例,您絕對應該檢查一下。
https://spring.io/guides/gs/testing-web/
希望這可以幫助
多虧克萊里斯和Whysoseriousson 。 現在可以使用
有了這些想法,我開發了一個新代碼(只有Jar放入pom.xml中),並且將“測試依賴項”的范圍設置為“測試”
[...]
<packaging>war</packaging>
[...]
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
我遇到了一個附加的問題,因為測試文件未放入“測試包”中。 我已將所有測試文件移至“測試包”。 好奇的是它適用於Neon Eclipse,而不是最新版本
我知道( Spring的Test Tutorial ),並且我的示例中的大部分思想都屬於本教程。
感謝您的回答。 問題已關閉
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.