软件测试
测试方法
单元测试应该具备的特性
设置测试前setup(@before)和测试后tearDown(@After)
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
package com.westos.student.controller;
import com.westos.student.entity.Student;
import com.westos.student.service.StudentServiceInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.sql.ResultSet;
import java.util.List;
@RequestMapping("/api")
@RestController
public class StudentController {
@Autowired
private StudentServiceInterface studentService;
/**
* 前后端分离时一般在api层会给出http状态码
* @param student
* @return
*/
@RequestMapping("/listStudent.do")
public ResponseEntity<List<Student>> listStudent(Student student){
List<Student> result = studentService.listStudent(student);
return new ResponseEntity<List<Student>>(result,HttpStatus.OK);
}
@RequestMapping("/saveStudent.do")
public ResponseEntity<Integer> save(Student student){
Integer result = studentService.save(student);
return new ResponseEntity<Integer>(result,HttpStatus.OK);
}
}
可以选择new 对象进行测试
package com.westos.student.controller;
import com.westos.student.entity.Student;
import org.junit.Before;
import org.junit.Test;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import java.util.List;
import static org.junit.Assert.*;
public class StudentControllerTest4 {
private StudentController studentController;
@Before
public void setUp() throws Exception {
//每次测试前new对象
studentController = new StudentController();
}
@Test
public void listStudent() {
//按照学生id查询
Student student = new Student();
student.setId(1);
ResponseEntity<List<Student>> list = studentController.listStudent(student);
//断言状态码为OK
assertEquals(HttpStatus.OK,list.getStatusCode());
//断言查出来1条数据
assertEquals(1,list.getBody().size());
}
@Test
public void save() {
}
}
如果controller中方法参数是HttpServletRequest或者HttpServletResponse的话。
先添加依赖包
org.springframework
spring-test
然后
MockHttpServletRequest request=new MockHttpServletRequest();
request.setParameter("name","zhangsan");
//然后将request传入。相当于模拟给request传参了。
以上的是利用new对象的方式。现在利用spring注入的方式。
给测试类前加上注解就可以启动spring了
package com.westos.student.controller;
import com.westos.student.entity.Student;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.context.WebApplicationContext;
import java.util.List;
import static org.junit.Assert.*;
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:spring.xml","classpath:spring-mvc.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class StudentControllerTest {
@Autowired
protected WebApplicationContext wac;
@Autowired
private StudentController studentController;
@Before
public void setup(){
assertNotNull(studentController);
}
@Test
public void listStudent() {
ResponseEntity<List<Student>> list = studentController.listStudent(null);
assertEquals(HttpStatus.OK,list.getStatusCode());
assertEquals(2,list.getBody().size());
}
}
spring.xml中加上提供的jdbc。
package com.westos.student.dao;
import com.westos.student.entity.Student;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.context.WebApplicationContext;
import java.util.List;
import static org.junit.Assert.*;
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:spring.xml","classpath:spring-mvc.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class StudentDAOTest {
@Autowired
protected WebApplicationContext wac;
@Autowired
private StudentDAO dao;
@Autowired
private JdbcTemplate jdbcTemplate;
@Before
public void setup(){
assertNotNull(wac);
assertNotNull(dao);
}
@Test
public void listStudent() {
//执行dao的函数
Student stu=new Student();
stu.setName("Jim");
List<Student> list = dao.listStudent(stu);
assertEquals(2,list.get(0).getId().intValue());
assertEquals("Jim",list.get(0).getName());
}
@Test
public void save(){
//删除库中的多余数据
jdbcTemplate.execute("delete from student where id=3");
Student student=new Student();
student.setId(3);
student.setName("张三");
Integer result=dao.save(student);
assertEquals(1,result.intValue());
//及时删除数据
jdbcTemplate.execute("delete from student where id=3");
}
}
方式1,自动装配,使用service中使用dao。
package com.westos.student.service;
import com.westos.student.entity.Student;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.context.WebApplicationContext;
import java.util.List;
import static org.junit.Assert.*;
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:spring.xml","classpath:spring-mvc.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class StudentServiceTest {
@Autowired
protected WebApplicationContext wac;
@Autowired
private StudentService studentService;
@Before
public void setup(){
assertNotNull(studentService);
}
@Test
public void listStudent() {
Student student=new Student();
List<Student> list = studentService.listStudent(student);
assertEquals(2,list.size());
}
}
方式2,独立测试,不使用dao,你写完了service层,你的队友dao层还没写完,那你要等他吗?不等,自己测试自己的。
引入包
org.mockito
mockito-core
2.28.2
test
package com.westos.student.service;
import com.westos.student.dao.StudentDAO;
import com.westos.student.entity.Student;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.context.WebApplicationContext;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:spring.xml","classpath:spring-mvc.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class StudentService2Test {
@Autowired
protected WebApplicationContext wac;
@InjectMocks
private StudentService studentService;
@Mock
private StudentDAO studentDAO;//假的studentDAO
@Before
public void setup(){
//初始化mock
MockitoAnnotations.initMocks(this);
assertNotNull(studentService);
}
@Test
public void listStudent() {
List<Student> result=new ArrayList<Student>();
Student student1=new Student();
student1.setId(1);
student1.setName("John");
result.add(student1);
Student student2=new Student();
student2.setId(2);
student2.setName("Jim");
result.add(student2);
//当调用studentDAO.listStudent(any(Student.class))时,让DAO返回自己设定的值
when(studentDAO.listStudent(any(Student.class))).thenReturn(result);
Student student=new Student();
List<Student> list = studentService.listStudent(student);
assertEquals(2,list.size());
}
@Test
public void save() {
}
}
package com.westos.student.controller;
import com.westos.student.service.StudentServiceInterface;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import static org.junit.Assert.*;
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:spring.xml","classpath:spring-mvc.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class StudentController2Test {
@InjectMocks
private StudentController studentController;
@Mock
private StudentServiceInterface studentService;
@Before
public void setup(){
MockitoAnnotations.initMocks(this);
}
@Test
public void listStudent() {
}
@Test
public void save() {
}
}
因为以上的测试中,如果代码中有拦截器之类的,是拦截不到的,因为直接调用的controller方法。
package com.westos.student.controller;
import com.alibaba.fastjson.JSON;
import com.westos.student.entity.Student;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
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.MvcResult;
import org.springframework.web.context.WebApplicationContext;
import java.util.List;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;
import static org.junit.Assert.*;
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:spring.xml","classpath:spring-mvc.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class StudentController3Test {
private MockMvc mockMvc;
@Autowired
protected WebApplicationContext wac;
@Autowired
private StudentController studentController;
@Before
public void setup(){
//构建全部的controller,测试比较全面
// this.mockMvc=webAppContextSetup(this.wac).build();
//构建指定的controller,加载的controller少,速度较快
this.mockMvc=standaloneSetup(studentController).build();
}
@Test
public void listStudent() throws Exception {
String url="/api/listStudent.do";
MvcResult result = this.mockMvc.perform(post(url).param("name", "Jim")).andExpect(status().isOk()).andReturn();
String str = result.getResponse().getContentAsString();
List<Student> list = JSON.parseArray(str, Student.class);
assertEquals(1,list.size());
assertEquals(2,list.get(0).getId().intValue());
assertEquals("Jim",list.get(0).getName());
//gei session添加东西,跳过登陆(登陆信息存在session中)。
result = this.mockMvc.perform(post(url).sessionAttr("LOGINUSERID","admin")).andExpect(status().isOk()).andReturn();
str = result.getResponse().getContentAsString();
list = JSON.parseArray(str, Student.class);
assertEquals(2,list.size());
}
@Test
public void save() {
}
}
谢谢!