如果我通过方法参数获得连接,如何使用 Mockito 测试我的 Dao 方法?
How can I test my Dao method with Mockito If I get connection by method parameters?
我有一个 DAO class 其方法使用由给定参数接收的连接
例子
@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;
}
我有服务 class 使用工厂获取连接并将其传递给 Dao 方法,我测试了服务 class.But 如何测试 Dao class?
您可以使用模拟对象进行测试,查找 Mockito 库 (https://www.tutorialspoint.com/mockito/mockito_overview.htm)
示例测试用例
Java 类 和测试 class 用测试用例
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", 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.assertTrue(result);
}
@Test(expected = SQLException.class)
public void test3() throws SQLException {
int actualResponse = 2;
CarCategory cc = new CarCategory("abc", 10.2, 11.1, 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);
}
}
我有一个 DAO class 其方法使用由给定参数接收的连接
例子
@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;
}
我有服务 class 使用工厂获取连接并将其传递给 Dao 方法,我测试了服务 class.But 如何测试 Dao class?
您可以使用模拟对象进行测试,查找 Mockito 库 (https://www.tutorialspoint.com/mockito/mockito_overview.htm)
示例测试用例
Java 类 和测试 class 用测试用例
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", 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.assertTrue(result);
}
@Test(expected = SQLException.class)
public void test3() throws SQLException {
int actualResponse = 2;
CarCategory cc = new CarCategory("abc", 10.2, 11.1, 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);
}
}