Issue
I am trying to write a unit test case for a class where I check if a table exists in the database or not. I have used the DatabaseMetadata
interface and Resultset
to implement it. Now I am trying to write a unit test case for that but while mocking
databasemetadata getTables()
method, My result set is null. Can anyone please tell me what I am doing wrong? Also Is there any better way of checking tables in a database?
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);
}
Solution
You mock method with parameters databaseMetaData.getTables(null, null, null, null))
, because in the setup method:
String tableName = null;
String[] types = null;
But when you call checkForTableInDataBase
method, inside it calls getTables
method with different parameters:
String tableName = entityName.toUpperCase();
String[] types = {"TABLE"};
so you should re-write you when method:
when(databaseMetaData.getTables(null, null, "task".toUpperCase(), {"TABLE"})).thenReturn(resultSet);
of course it will work only in case if you use "task" as an input parameter for dataBaseTableScanner.checkForTableInDataBase()
Answered By - Anatolii Chub
Answer Checked By - Mildred Charles (JavaFixing Admin)