SpringBoot集成单元测试

Spring基本单元测试
  • pom.xml 引入依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
  • 在测试目录添加测试类
    SpringBoot集成单元测试_第1张图片
1.注入服务直接测试
@RunWith(SpringRunner.class)
@SpringBootTest

//只注入依赖的bean,加快测试速度
//@ContextConfiguration(classes = {
//        AccountController.class,
//        SysLoginService.class,
//        SysLoginFallBack.class
//})
public class AccountControllerTest {

    @Autowired
    SysLoginService sysLoginService;

    @Test
    public void testLogin(){
        LoginDTO loginDTO = new LoginDTO();
        loginDTO.setLoginType(LoginType.mobile);
        loginDTO.setAccount("888888");
        loginDTO.setPassword("123456");
        ReturnEntity returnEntity = sysLoginService.Login(loginDTO);
        //断言:测试返回实体不为null
        Assert.assertNotNull(returnEntity);
        //断言:测试接口状态成功与否
        Assert.assertEquals("0000" , returnEntity.getResultCode());
        //查看token
        System.out.println(returnEntity.getResultObj());
    }
}
2.使用MockMvc进行测试
import com.alibaba.fastjson.JSON;
import com.egovchina.partybuilding.common.enums.LoginType;
import com.egovchina.partybuilding.common.pojo.dto.LoginDTO;
import com.egovchina.partybuilding.common.util.ReturnEntity;
import org.junit.Assert;
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.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MockMvcBuilder;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
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;


@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc//配置mock
public class AccountControllerMockTest {

    @Autowired
    private MockMvc mvc;

    @Test
    public void testLogin() throws Exception{
        LoginDTO loginDTO = new LoginDTO();
        loginDTO.setLoginType(LoginType.mobile);
        loginDTO.setAccount("888888");
        loginDTO.setPassword("123456");
        String requestUrl = "/users/login";
        MvcResult result = mvc.perform(
                    MockMvcRequestBuilders.post(requestUrl)
                            .content(JSON.toJSONString(loginDTO))
                            .contentType(MediaType.APPLICATION_PROBLEM_JSON_UTF8_VALUE)
                            .accept(MediaType.APPLICATION_PROBLEM_JSON_UTF8_VALUE)
                )
                .andExpect(status().isOk())
                .andDo(print())
                .andReturn();
        String content =  result.getResponse().getContentAsString();
        //测试返回实体不为null
        Assert.assertNotNull(content);
        ReturnEntity returnEntity = JSON.parseObject(content , ReturnEntity.class);
        //测试返回实体不为null
        Assert.assertNotNull(returnEntity);
        //测试接口状态成功与否
        Assert.assertEquals("0000" , returnEntity.getResultCode());
        //查看token
        System.out.println(returnEntity.getResultObj());
    }
}

还可以在pom.xml中配置启动项目跳过测试

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <!-- 启动项目跳过测试 -->
        <maven.test.skip>true</maven.test.skip>
</properties>        

你可能感兴趣的:(SpringBoot,项目开发,spring,boot)