Spring、Spring MVC、mybatis框架使用junit完成單元測試

之前對一個方法是否執行正確的判斷上,總是憑自己的感覺或等到最後調用並執行的時候纔可以看出開。今天使用junit完成了對方法的測試,現對該方法進行記錄,希望可以幫助到你們。

  • 添加相關測試依賴
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.7</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>${org.springframework-version}</version>
    <scope>test</scope>
</dependency>
  • 這裏以我的一個簡單項目來演示
    1. 在對應的包下選擇並創建測試用例
      image
    2. 填寫測試用例名稱並選擇類中的相關方法,並點擊Finish完成測試用例的創建
      這裏寫圖片描述
      這裏寫圖片描述
      這裏寫圖片描述
    3. 在測試類中添加相關注解
      註解
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:root-context.xml", "classpath*:servlet-context.xml" })  //引入兩個配置文件
@TransactionConfiguration(defaultRollback = true, transactionManager = "transactionmanager")  //使用事務,避免運行測試對數據的影響
@Transactional

4 .再次就完成了測試類的創建和配置
5. 注入所需要的bean

package com.example.oa.test;

import static org.junit.Assert.*;
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.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

import com.example.oa.domain.Employee;
import com.example.oa.service.EmployeeService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:root-context.xml", "classpath*:servlet-context.xml" })
@TransactionConfiguration(defaultRollback = true, transactionManager = "transactionmanager")
@Transactional
public class EmployeeTest {
    @Autowired
    private EmployeeService employeeService;   //注入所需的bean(記得要在實現類上添加相關注解)

    @Test
    public void testIsUser() {
        boolean hasuser = employeeService.getuserByUPR("890", "7987", 0);
        assertEquals(true, hasuser);
    }

    @Test
    public void testForgetPassword() {
        Employee e = new Employee();
        e.setEmployee_account("6");
        e.setRole(0);
        e.setPassword("555");
        boolean isupdate = employeeService.updatePassword(e);
        assertEquals(true, isupdate);
    }

    @Test
    public void testDeleteEmployee() {
        Employee e = new Employee();
        e.setEmployee_id(7);
        boolean isdelete = employeeService.deleteEmployee(e);
        assertEquals(true, isdelete);
    }

    @Test
    public void testAddEmployee() {
        Employee e = new Employee();
        e.setEmployee_account("555");
        e.setUsername("555");
        e.setPassword("666");
        e.setAge(45);
        e.setAddress("四川省成都市");
        e.setDepartmentid(2);
        e.setRole(0);
        boolean isadded = employeeService.addEmployee(e);
        assertEquals(true, isadded);
    }

}
  • . 運行測試
    這裏寫圖片描述
    這裏寫圖片描述
    *. 項目結構:
    項目結構

若存在問題,請留言

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章