postgresql - How can I check that Java JDBC code uses the right column names? -


i have test suite of end-to-end tests. supposed catch typos in sql statements, bad table or column names (anything db schema , java code disagree), or missing db permissions. don't want rely on data in database (too complicated set up); basic test.

import java.sql.*; import org.junit.test;  public class typotest {     private connection getconnection() throws exception {         string connectionstring = "jdbc:postgresql://127.0.0.1:5432/db";         string driverclassname = "org.postgresql.ds.pgconnectionpooldatasource";         class.forname(driverclassname).newinstance();         return drivermanager.getconnection(connectionstring, "robert", "");     }      @test     public void runquery() throws exception {         try (connection connection = getconnection();              preparedstatement ps = connection.preparestatement("select relname pg_catalog.pg_class");              resultset data = ps.executequery()) {             while (data.next()) {                 data.getstring("relname");             }         }     } } 

when run above test, fails if have typo in select statement. (good.) if have typo in column name in data.getstring("typo here"), won't caught if table queried not have data because loop never entered. keep test (setup) simple, don't want insert data tables first.

i guess make column names constants , dry code , rid of problem.

however, wondering if there easier way... lazy , don't want edit queries. there better way unit-test sql?

i using postgres 9.5 , jdbc 4.

i guess have answer seek sake of answering, can try using result-set-metadata using select * table , checking column names against query (you'd have parse query string guess...).

i believe work empty tables note have not tested empty table scenario.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -