Spring Boot学习——单元测试

本随笔记录使用Spring Boot进行单元测试,主要是Service和API(Controller)进行单元测试。

一、Service单元测试

选择要测试的service类的方法,使用idea自动创建测试类,步骤如下。(注,我用的是idea自动创建,也可以自己手动创建)

自动创建测试类之后目录如下图:

测试类StudentServiceTest.java代码如下:

package *; //自己定义路径

import *.Student; //自己定义路径
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.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.*;

@RunWith(SpringRunner.class)
@SpringBootTest
public class StudentServiceTest {
    @Autowired
    private StudentService studentService;

    @Test
    public void findOne() throws Exception {
        Student student = studentService.findOne(1);
        Assert.assertEquals(new Integer(16), student.getAge());
    }
}

二、API(Controller)单元测试

根据上面的步骤创建单元测试类StudentControllerTest.java,代码如下:

package *; //自己定义路径

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;
import static org.junit.Assert.*;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class StudentControllerTest {
    @Autowired
    private MockMvc mvc;

    @Test
    public void getStudentList() throws Exception {
        mvc.perform(MockMvcRequestBuilders.get("/students"))
                .andExpect(MockMvcResultMatchers.status().isOk());
        //.andExpect(MockMvcResultMatchers.content().string("365"));  //测试接口返回内容
    }

}

三、service和API(Controller)类和方法

StudentController.java代码如下:

package *; //自己定义路径

import *.StudentRepository; //自己定义路径
import *.Student; //自己定义路径
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;

@RestController
public class StudentController {
    @Autowired
    private StudentRepository studentRepository;

    /**
     * 查询学生列表
     * @return
     */
    @GetMapping(value = "/students")
    public List getStudentList(){
        return studentRepository.findAll();
    }
}

StudentService.java代码如下:

package *; //自己定义路径

import *.StudentRepository; //自己定义路径
import *.Student; //自己定义路径
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StudentService {
    @Autowired
    private StudentRepository studentRepository;

    /**
     * 根据ID查询学生
     * @param id
     * @return
     */
    public Student findOne(Integer id){
        return studentRepository.findOne( id);
    }
}

 

作者:aston

来源:https://www.cnblogs.com/aston/p/7259825.html


如果给你带来帮助,欢迎微信或支付宝扫一扫,赞一下。