SpringBoot Controller Post接口單元測試示例
概述
在日常的開發(fā)中,我們一般會定義一個service層,用于實現(xiàn)業(yè)務邏輯,并且針對service層會有與之對應的齊全的覆蓋率高的單元測試。而對于controller層,一般不怎么做單元測試,因為主要的核心業(yè)務邏輯都在service層里,controller層只是做轉(zhuǎn)發(fā),調(diào)用service層接口而已。但是還是建議使用單元測試簡單的將controller的方法跑一下,看看轉(zhuǎn)發(fā)和數(shù)據(jù)轉(zhuǎn)換的代碼是否能正常工作。
在Spring Boot里對controller層進行單元測試非常簡單,只需要幾個注解和一點點輔助代碼即可搞定。
依賴的包
<dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> </dependency>
使用的Spring Boot 版本
2.0.4.RELEASE
代碼
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment =SpringBootTest.WebEnvironment.MOCK,classes = TestApplication.class)
@AutoConfigureMockMvc
public class UserControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private UserService userService;
@Test
@DisplayName("測試controller方法")
void test() throws Exception {
User param = new User();
param.setUserId(1111);
List<Address> addressList = new ArrayList<>();
Address address = new Address();
address.setName("我的地址");
addressList.add(address);
param.setAddressList(addressList);
MvcResult mvcResult = mockMvc.perform(
post("/xxx/test")
.contentType(MediaType.APPLICATION_JSON)
.content(JSON.toJSONString(param)))
.andReturn();
System.out.println(mvcResult.getResponse().getContentAsString());
}
}
@RequestMapping(value = "/xxx", method = RequestMethod.POST)
public Object test(@RequestBody(required = false)User user) throws Exception {
}
如果你只是想簡單的跑一下controller層,不想真正的去執(zhí)行service方法的話,需要使用@MockBean將對應的service類mock掉。
@MockBean private UserService userService;
使用Spring Boot Test的時候,它需要一個ApplicationContext,我們可以在@SpringBootTest注解中使用classes屬性來指定。
@SpringBootTest(webEnvironment =SpringBootTest.WebEnvironment.MOCK,classes = TestApplication.class)
TestApplication的代碼很簡單。
@SpringBootApplication
public class TestApplication {
public static void main(String[] args){
SpringApplicationBuilder builder = new SpringApplicationBuilder();
builder.environment(new StandardEnvironment());
builder.sources(TestApplication.class);
builder.main(TestApplication.class);
builder.run(args);
}
}
接下來我們只需要使用MockMvc發(fā)送post請求即可。如果controller層的post方法是帶@RequestBody注解的,可以先將入?yún)ο筠D(zhuǎn)換成JSON字符串。這里使用的是fastjson。
JSON.toJSONString(param)
經(jīng)過測試,如上代碼能正常工作。
總結
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關內(nèi)容請查看下面相關鏈接
相關文章
Spring boot實現(xiàn)文件上傳實例(多文件上傳)
本篇文章主要介紹了Spring boot實現(xiàn)文件上傳實例(多文件上傳),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
Java高并發(fā)BlockingQueue重要的實現(xiàn)類詳解
這篇文章主要給大家介紹了關于Java高并發(fā)BlockingQueue重要的實現(xiàn)類的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01
淺析Java中關鍵詞volatile底層的實現(xiàn)原理
在 Java 并發(fā)編程中,有 3 個最常用的關鍵字:synchronized、ReentrantLock 和 volatile,這篇文章主要來和大家聊聊volatile底層的實現(xiàn)原理,感興趣的可以了解下2024-02-02
優(yōu)化spring?boot應用后6s內(nèi)啟動內(nèi)存減半
這篇文章主要為大家介紹了優(yōu)化spring?boot后應用6s內(nèi)啟動內(nèi)存減半的優(yōu)化示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪2022-02-02
Mybatis-Plus開發(fā)提速器mybatis-plus-generator-ui詳解
這篇文章主要介紹了Mybatis-Plus開發(fā)提速器mybatis-plus-generator-ui,本文簡要介紹一款基于Mybatis-Plus的代碼自助生成器,文章通過實例集成的方式來詳細講解mybatis-plus-generator-ui,從相關概念到實際集成案例,以及具體的擴展開發(fā)介紹,需要的朋友可以參考下2022-11-11

