Spring-Boot单元测试(转)

https://blog.csdn.net/qq383264679/article/details/50542290(转)

闲谈

很遗憾,事与愿违,关于Spring-Boot整合Mybaits的文章就不出来,需要的朋友可以加java群:238132240。里面有Spring-Boot整合Mybaits的Demo,有事务,整合了通用Mapper,分页工具pageHelper.

正篇

spring-boot如何进行单元格测试?想必是一个很重要的环节,在Spring传统项目中,使用单元格测试不言而喻。这里分别介绍从MVC分层的思想分别进行阐述:

Spring-Boot需要的maven依赖:

org.springframework.bootspring-boot-starter-testtest

会自动添加junit和spring-testjar。

dao层

示例代码:

package dao;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.SpringApplicationConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;importcom.lgy.ApplicationMain;importcom.lgy.dao.UserDao;importcom.lgy.model.User;@RunWith(SpringJUnit4ClassRunner.class)@SpringApplicationConfiguration(classes = ApplicationMain.class)public class DaoTest {    @Autowired    private UserDao userDao;@Test    public void test() {        User user = new User();user.setUsername("ttt");user.setNack("他们");userDao.insert(user);System.out.println(userDao);}}

service层

示例代码:

package dao;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.SpringApplicationConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;importcom.lgy.ApplicationMain;importcom.lgy.model.User;importcom.lgy.service.UserService;@RunWith(SpringJUnit4ClassRunner.class)@SpringApplicationConfiguration(classes = ApplicationMain.class)public class ServiceTest {    @Autowired    private UserService userService;@Test    public void test() {        User user = new User();user.setUsername("test");user.setNack("haha");userService.addUser(user);System.out.println("OK");}}

controller层

实例代码:

package dao;import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.boot.test.SpringApplicationConfiguration;import org.springframework.mock.web.MockServletContext;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.test.context.web.WebAppConfiguration;import org.springframework.test.web.servlet.MockMvc;import org.springframework.test.web.servlet.ResultActions;import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;import org.springframework.test.web.servlet.result.MockMvcResultMatchers;import org.springframework.test.web.servlet.setup.MockMvcBuilders;importcom.lgy.controller.TestController;@RunWith(SpringJUnit4ClassRunner.class)@SpringApplicationConfiguration(classes = MockServletContext.class)@WebAppConfigurationpublic class ControllerTest {    private MockMvc mvc;@Before    public void setUp() {        mvc = MockMvcBuilders.standaloneSetup(new TestController()).build();}    @Test    public void testController() {        try {            ResultActions actions = mvc.perform(MockMvcRequestBuilders.get("/test/json"));actions.andExpect(status().isOk());  //测试请求反馈状态String json ="{\"aaa\":\"bbbb\"}";actions.andExpect(MockMvcResultMatchers.content().json(json)); //断言返回json数据} catch (Exception e) {            e.printStackTrace();}    }}

其中: 

@SpringApplicationConfiguration(classes = ApplicationMain.class) 

是初始化Spring-Boot容器的意思,它会初始化各个Contiruge配置文件。

你可能感兴趣的:(Spring-Boot单元测试(转))