spring-boot学习写一个简单的单元测试
Posted KoMiles 少壮不努力,长大搞IT。
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring-boot学习写一个简单的单元测试相关的知识,希望对你有一定的参考价值。
application.yml
server: port: 8090 servlet: context-path: /springboot
HelloController
package com.komiles.study.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/hello") public class HelloController { @GetMapping("/test") public String test(@RequestParam(value = "name", defaultValue = "1234") String name) { return name; } }
HelloControllerTest
package com.komiles.study.controller; 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; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; @RunWith(SpringRunner.class) @SpringBootTest @AutoConfigureMockMvc public class HelloControllerTest { @Autowired private MockMvc mockMvc; @Test public void test() throws Exception { this.mockMvc.perform(MockMvcRequestBuilders.get("/hello/test")) .andExpect(MockMvcResultMatchers.status().isOk()); } }
以上是关于spring-boot学习写一个简单的单元测试的主要内容,如果未能解决你的问题,请参考以下文章