Skip to content

Commit 2a23bc4

Browse files
committed
test(postgres): stub current_schema() so the provider fixtures pass again
resolveSchema() issues `SELECT current_schema()` on its own Statement before each method's real query. Seven tests predated that call and broke: those stubbing only prepareStatement got the unstubbed-mock default null from createStatement() NullPointerException: Cannot invoke "java.sql.Statement.executeQuery(String)" because "stmt" is null at resolveSchema(...:797) and those stubbing createStatement handed resolveSchema the shared ResultSet, so its getString(1) tripped strict stubbing against getString("tablename"). resolveSchema() is the first createStatement() caller in every method that reaches the database, so returning a dedicated schemaStatement first and the shared statement afterwards routes each to the right place. It answers "public", keeping these fixtures on the historical schema so they go on asserting the behaviour they were written for rather than the search_path change itself. setUp() uses lenient() because the pure-Java tests (getDatabaseType, getDefaultSchema) never touch the Connection and strict stubbing would fail them over an unused stub. Three tests re-stub createStatement locally and had to prepend schemaStatement themselves; getForeignKeys_returnsRelationships is left alone because getForeignKeys does not call resolveSchema. 11/11 pass.
1 parent f5196eb commit 2a23bc4

1 file changed

Lines changed: 28 additions & 3 deletions

File tree

backend/src/test/java/com/dbaagent/provider/postgres/PostgresIntrospectionProviderTest.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,31 @@ class PostgresIntrospectionProviderTest {
3535
@Mock
3636
private ResultSet resultSet;
3737

38+
// resolveSchema() issues `SELECT current_schema()` on its own Statement before
39+
// any method's real query. These mocks answer that call so the fixtures below
40+
// keep testing what they were written to test.
41+
@Mock
42+
private Statement schemaStatement;
43+
44+
@Mock
45+
private ResultSet schemaResultSet;
46+
3847
@BeforeEach
39-
void setUp() {
48+
void setUp() throws SQLException {
4049
provider = new PostgresIntrospectionProvider();
50+
51+
// lenient(): the pure-Java tests (getDatabaseType, getDefaultSchema, …) never
52+
// touch the Connection, and strict stubbing would fail them over an unused stub.
53+
//
54+
// resolveSchema() is the FIRST createStatement() caller in every method that
55+
// reaches the database, so returning schemaStatement first and the shared
56+
// statement afterwards routes each to the right place. Answering "public"
57+
// keeps these fixtures on the historical schema, so they go on asserting the
58+
// behaviour they were written for rather than the search_path change itself.
59+
lenient().when(connection.createStatement()).thenReturn(schemaStatement, statement);
60+
lenient().when(schemaStatement.executeQuery(anyString())).thenReturn(schemaResultSet);
61+
lenient().when(schemaResultSet.next()).thenReturn(true);
62+
lenient().when(schemaResultSet.getString(1)).thenReturn("public");
4163
}
4264

4365
@Test
@@ -47,7 +69,8 @@ void getDatabaseType_returnsPostgres() {
4769

4870
@Test
4971
void getDatabaseObjects_returnsTables() throws SQLException {
50-
when(connection.createStatement()).thenReturn(statement);
72+
// schemaStatement first: resolveSchema() runs before the objects query.
73+
when(connection.createStatement()).thenReturn(schemaStatement, statement);
5174
when(statement.executeQuery(anyString())).thenReturn(resultSet);
5275
when(connection.prepareStatement(anyString())).thenReturn(preparedStatement);
5376
when(preparedStatement.executeQuery()).thenReturn(resultSet);
@@ -209,7 +232,8 @@ void getTableStats_bindsEveryPlaceholderInTheStatsQuery() throws SQLException {
209232

210233
@Test
211234
void scanSchema_returnsSchemaMetadata() throws SQLException {
212-
when(connection.createStatement()).thenReturn(statement);
235+
// schemaStatement first: resolveSchema() runs before the tables query.
236+
when(connection.createStatement()).thenReturn(schemaStatement, statement);
213237
when(statement.executeQuery(anyString())).thenReturn(resultSet);
214238

215239
when(resultSet.next())
@@ -281,6 +305,7 @@ void scanSchema_fallsBackToExactCountWhenEstimateMissing() throws SQLException {
281305
ResultSet foreignKeysResultSet = mock(ResultSet.class);
282306

283307
when(connection.createStatement()).thenReturn(
308+
schemaStatement, // resolveSchema() runs before the tables query
284309
statement,
285310
exactCountStatement,
286311
columnsStatement,

0 commit comments

Comments
 (0)