如果我通过方法参数获得连接,如何使用 Mockito 测试我的 Dao 方法?

问题描述

我有一个 DAO 类,它的方法使用给定参数接收的连接

示例

@Override
    public boolean insertCarCategory(Connection connection,CarCategory carCategory) throws MysqLEXContainer.MysqLDBExecutionException,sqlException {
        int rowNum = 0;
        Connection con;
        PreparedStatement statement = null;
        try{
            String query = QueriesUtil.getQuery("insertCarCategory");
            con = connection;
            statement = con.prepareStatement(query);
            statement.setString(1,carCategory.getCarCategory());
            statement.setDouble(2,carCategory.getCostPerOneKilometer());
            statement.setDouble(3,carCategory.getdiscount());
            statement.setBytes(4,ImageUtil.imagetoByte(carCategory.getCarCategoryImage()));
            rowNum = statement.executeUpdate();
        } catch (sqlException e) {
            LOGGER.error(e);
            throw new MysqLEXContainer.MysqLDBExecutionException("Bad execution",e);
        }finally {
            ConnectionUtil.oneMethodToCloseThemAll(null,statement,null);
        }
        return rowNum > 0;
    }

我有使用工厂获取连接并将其传递给 Dao 方法的服务类,我测试了 Service 类。但是我如何测试 Dao 类?

解决方法

您可以使用模拟对象进行测试,查找 Mockito 库 (https://www.tutorialspoint.com/mockito/mockito_overview.htm)

示例测试用例

Java 类和带有测试用例的测试类

public class CarCategory {
    private String carCategory;
    private Double costPerOneKilometer;
    private Double discount;
    private byte[] carCategoryImage;

    public CarCategory(String carCategory,Double costPerOneKilometer,Double discount,byte[] carCategoryImage) {
        this.carCategory = carCategory;
        this.costPerOneKilometer = costPerOneKilometer;
        this.discount = discount;
        this.carCategoryImage = carCategoryImage;
    }

    public String getCarCategory() {
        return carCategory;
    }

    public void setCarCategory(String carCategory) {
        this.carCategory = carCategory;
    }

    public Double getCostPerOneKilometer() {
        return costPerOneKilometer;
    }

    public void setCostPerOneKilometer(Double costPerOneKilometer) {
        this.costPerOneKilometer = costPerOneKilometer;
    }

    public Double getDiscount() {
        return discount;
    }

    public void setDiscount(Double discount) {
        this.discount = discount;
    }

    public byte[] getCarCategoryImage() {
        return carCategoryImage;
    }

    public void setCarCategoryImage(byte[] carCategoryImage) {
        this.carCategoryImage = carCategoryImage;
    }

}


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class CarCategoryDao {

    private static final Logger LOGGER = LoggerFactory.getLogger(CarCategoryDao.class);

    public boolean insertCarCategory(Connection connection,CarCategory carCategory) throws SQLException {
        int rowNum = 0;
        Connection con;
        PreparedStatement statement = null;
        try{
            String query = "insertCarCategory";
            con = connection;
            statement = con.prepareStatement(query);
            statement.setString(1,carCategory.getCarCategory());
            statement.setDouble(2,carCategory.getCostPerOneKilometer());
            statement.setDouble(3,carCategory.getDiscount());
            statement.setBytes(4,carCategory.getCarCategoryImage());
            rowNum = statement.executeUpdate();
        } catch (SQLException e) {
            LOGGER.error("sas",e);
            throw e;
        }
        return rowNum > 0;
    }
}



import com.dao.utils.CarCategory;
import com.dao.utils.CarCategoryDao;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.test.context.junit4.SpringRunner;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;


import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

@RunWith(SpringRunner.class)
public class CarCategoryDaoTest {

    @Mock
    private Connection connection;

    @Mock
    private PreparedStatement statement;

    @InjectMocks
    private CarCategoryDao carCategoryDao;

    @Test
    public void test1() throws SQLException {
        int actualResponse = 0;
        CarCategory cc = new CarCategory("abc",10.2,11.1,new byte[100]);
        
            when(connection.prepareStatement(anyString())).thenReturn(statement);
            when(statement.executeUpdate()).thenReturn(actualResponse);
            boolean result = carCategoryDao.insertCarCategory(connection,cc);
            Assert.assertFalse(result);
        
    }

    @Test
    public void test2()  throws SQLException {
        int actualResponse = 2;
        CarCategory cc = new CarCategory("abc",cc);
            Assert.assertTrue(result);
        
    }

    @Test(expected = SQLException.class)
    public void test3()  throws SQLException {
        int actualResponse = 2;
        CarCategory cc = new CarCategory("abc",new byte[100]);
       
            when(connection.prepareStatement(anyString())).thenReturn(statement);
            when(statement.executeUpdate()).thenReturn(actualResponse);
            when(statement.executeUpdate()).thenThrow(SQLException.class);
            boolean result = carCategoryDao.insertCarCategory(connection,cc);
        
    }

}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...