我正试图为一个类写一个单元测试用例,在那里我检查数据库中是否存在表。我使用DatabaseMetadata
接口和Resultset
接口来实现它。现在我试着写一个单元测试用例但是当mock
databasemetadata可以获得的()
方法,结果集为空。有人能告诉我我做错了什么吗?还有,有没有更好的方法来检查数据库中的表?
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.core.JsonProcessingException;
@Component
public class DataBaseTableScanner {
public DataBaseTableScanner() {
super();
}
private DataSource datasource;
@Autowired
public DataBaseTableScanner(DataSource datasource) {
this.datasource = datasource;
}
public void setDataSource(DataSource datasource) {
this.datasource = datasource;
}
public boolean checkForTableInDataBase(String entityName) throws SQLException {
try(Connection con = datasource.getConnection()){
DatabaseMetaData databaseMetaData = con.getMetaData();
String tableName = entityName.toUpperCase();
String[] types = {"TABLE"};
resultSet = databaseMetaData.getTables(null, null, tableName, types);
boolean tableExistFlag = resultSet.next();
if(tableExistFlag) {
System.out.println("Check Table:: "+ tableName + " :Table exits in Database!!");
}else {
System.out.println("Check Table:: "+ tableName + " :Table does not exits in Database!!");
}
return tableExistFlag;
}
}
}
My Unit test case
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mapstruct.BeforeMapping;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class DataBaseTableScannerTest {
@InjectMocks
DataBaseTableScanner dataBaseTableScanner;
@Mock
DataSource datasource;
@Mock
Connection connection;
@Mock
DatabaseMetaData databaseMetaData;
@Mock
ResultSet resultSet;
@Before
public void setUp() throws TenantResolvingException, TenantNotFoundException, SQLException {
dataBaseTableScanner.setDataSource(datasource);
when(datasource.getConnection()).thenReturn(connection);
when(connection.getMetaData()).thenReturn(databaseMetaData);
String tableName = null;
String[] types = null;
// String tableName = "task";
// String[] types = {"TABLE"};
when(databaseMetaData.getTables(null, null, tableName, types)).thenReturn(resultSet);
}
@Test
public void testIsTableExists() throws SQLException {
boolean tbexists = dataBaseTableScanner.checkForTableInDataBase("task");
assertTrue(tbexists);
}
用参数databaseMetaData.getTables(null, null, null, null))
模拟方法,因为在setup方法中:
String tableName = null;
String[] types = null;
但是当你调用checkForTableInDataBase
方法时,它内部调用getTables
方法与不同的参数:
String tableName = entityName.toUpperCase();
String[] types = {"TABLE"};
所以你应该重写你当方法:
when(databaseMetaData.getTables(null, null, "task".toUpperCase(), {"TABLE"})).thenReturn(resultSet);
当然只有在你使用"task"作为dataBaseTableScanner.checkForTableInDataBase()