如何在springboot中进行单元测试
程序员文章站
2022-03-13 11:21:54
...
示例代码:
import static org.hamcrest.Matchers.equalTo; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @RunWith(SpringRunner.class) @WebAppConfiguration @SpringBootTest public class DemoApplicationTests { private MockMvc mvc; @Before public void setUp() throws Exception{ mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build(); } @Test public void contextLoads() throws Exception{ mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().string(equalTo("Hello"))); } }
不要忘记上面的import 静态引用
上一篇: 鸿蒙Harmony OS系统与airpods蓝牙耳机配对使用?
下一篇: 性能优化之MySQL调优篇