Unit testing of DAO layer interface in Spring

Unit testing plays a pivotal role in the software development process, and the role and importance of good unit testing goes without saying. Basically all java applications will deal with the database, and the test of the DAO layer interface involves the preparation, maintenance, verification and cleaning of the database test data. Unit tests should be repeatable and independent, so unit test data should not pollute the database. Many people insert the data manually in the unit test of the DAO layer interface. When they run the unit test for the second time, they will get a duplicate key error. The process of data cleaning is also done manually, or through try-catch. -finally block to clean up, which is naturally more difficult to automate testing. In fact, I personally think that using spring's support for transaction management in the spring framework can easily achieve the repeatability and isolation of DAO layer interface testing.

Example Description
Suppose there is a Student table, and now the StudentService class is tested. The persistence layer framework uses Mybatis here, and the related classes and configuration files are as follows:
Student entity class:
public class Student {  
    private Integer id;  
  
    public Student(String name, String sex, Byte age, String tel) {  
        this.name = name;  
        this.sex = sex;  
        this.age = age;  
        this.tel = tel;  
    }  
  
    public Student() {  
  
    }  
  
    private String name;  
  
    private String sex;  
  
    private Byte age;  
  
    private String tel;  
  
    public Integer getId() {  
        return id;  
    }  
  
    public void setId(Integer id) {  
        this.id = id;  
    }  
  
    public String getName() {  
        return name;  
    }  
  
    public void setName(String name) {  
        this.name = name == null ? null : name.trim();  
    }  
  
    public String getSex() {  
        return sex;  
    }  
  
    public void setSex(String sex) {  
        this.sex = sex == null ? null : sex.trim();  
    }  
  
    public Byte getAge() {  
        return age;  
    }  
  
    public void setAge(Byte age) {  
        this.age = age;  
    }  
  
    public String getTel() {  
        return tel;  
    }  
  
    public void setTel(String tel) {  
        this.tel = tel == null ? null : tel.trim();  
    }  
}  

StudentMapper interface:
public interface StudentMapper {  
    int insert(Student record);  
  
    Student selectByPrimaryKey(Integer id);  
  
    int updateByPrimaryKey(Student record);  
}  

StudentService service interface:
public interface StudentService {  
    public Student getStudentsById(int StudentsId);  
    public int insertStudent(Student s);  
    public void updateStudent(Student s);  
}  

StudentServiceImpl interface implementation:
public class StudentServiceImpl implements StudentService {  
  
    @Autowired  
    private StudentMapper studentMapper;  
    public Student getStudentsById(int StudentsId) {  
        return studentMapper.selectByPrimaryKey(StudentsId);  
    }  
  
    public int insertStudent(Student s) {  
        return studentMapper.insert(s);  
    }  
  
    public void updateStudent(Student s) {  
        studentMapper.updateByPrimaryKey(s);  
    }  
}  

unit test:
@RunWith(SpringJUnit4ClassRunner.class)  
@ContextConfiguration(locations = {"classpath:spring.xml"})  
@Transactional  
@Rollback(true)  
public class StudentServiceTest {  
  
    @Autowired  
    private StudentService studentService;  
  
    @Test  
    public void testInsertStudent() {  
        Student s = new Student("test", "male", (byte) 23, "110");  
        studentService.insertStudent(s);  
        Assert.assertEquals(studentService.getStudentsById(s.getId()).getName(),"test");  
        Assert.assertEquals(studentService.getStudentsById(s.getId()).getAge().intValue(), 23);  
    }  
  
    @Test  
    public void testUpdateStudent() {  
        Student s = new Student("test", "male", (byte) 23, "110");  
        studentService.insertStudent(s);  
        Assert.assertEquals(studentService.getStudentsById(s.getId()).getName(),"test");  
        Assert.assertEquals(studentService.getStudentsById(s.getId()).getAge().intValue(), 23);  
  
        s.setAge((byte)25);  
        s.setName("test2");  
        studentService.updateStudent(s);  
        Assert.assertEquals(studentService.getStudentsById(s.getId()).getName(),"test2");  
        Assert.assertEquals(studentService.getStudentsById(s.getId()).getAge().intValue(), 25);  
    }  
}  

The @Transactional annotation tag enables transaction support in this test class, so that all tests will be automatically rolled back after execution, no dirty data will be generated in the database, and any changes to the database will not need to be cleaned up by yourself.
@Rollback(true) sets the transaction rollback. In fact, the default @Transactional annotation defaultRollback is true by default, and it can be omitted here.
The execution result is shown in the following figure:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326206272&siteId=291194637