BAEL-4789 Check if a database table exists (#10565)

* BAEL-4789 Check if a database table exists

* BAEL-4789 fix db credentials

* BAEL-4789 fix test method name
This commit is contained in:
mdabrowski-eu
2021-03-21 18:38:10 +01:00
committed by GitHub
parent 45764829f7
commit dd288575da
3 changed files with 126 additions and 0 deletions
@@ -0,0 +1,70 @@
package com.baeldung.tableexists;
import org.junit.jupiter.api.Test;
import java.sql.Connection;
import java.sql.SQLException;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class TableCheckerUnitTest {
@Test
void givenCreatedTable_shouldFindTable() throws SQLException, ClassNotFoundException {
// given
Connection connection = DatabaseConfig.connect();
DatabaseConfig.createTables(connection);
// when
boolean tableExists = TableChecker.tableExists(connection, "EMPLOYEE");
// then
TableChecker.printAllTables(connection, null);
assertTrue(tableExists);
DatabaseConfig.dropTables(connection);
}
@Test
void givenCreatedTable_shouldFindTableWithSQL() throws SQLException, ClassNotFoundException {
// given
Connection connection = DatabaseConfig.connect();
DatabaseConfig.createTables(connection);
// when
boolean tableExists = TableChecker.tableExistsSQL(connection, "EMPLOYEE");
// then
TableChecker.printAllTables(connection, null);
assertTrue(tableExists);
DatabaseConfig.dropTables(connection);
}
@Test
void givenNoTable_shouldNotFindTable() throws SQLException, ClassNotFoundException {
// given
Connection connection = DatabaseConfig.connect();
// when
boolean tableExists = TableChecker.tableExists(connection, "EMPLOYEE");
// then
TableChecker.printAllTables(connection, null);
assertFalse(tableExists);
}
@Test
void givenNoTable_shouldNotFindTableWithSQL() throws SQLException, ClassNotFoundException {
// given
Connection connection = DatabaseConfig.connect();
// when
boolean tableExists = TableChecker.tableExistsSQL(connection, "EMPLOYEE");
// then
TableChecker.printAllTables(connection, null);
assertFalse(tableExists);
}
}