Skip to content

Instantly share code, notes, and snippets.

@jandrewthompson
Created February 26, 2013 22:29
Show Gist options
  • Save jandrewthompson/5042920 to your computer and use it in GitHub Desktop.
Save jandrewthompson/5042920 to your computer and use it in GitHub Desktop.
Mocking java.sql.Connection with JOOQ file based input provider
# This is a sample test database for MockFileDatabase
# Its syntax is inspired from H2's test script files
# When this query is executed...
select id, description, amount from account;
> id description amount
> -- ----------- ------
> 1 slushy 200
> 2 foo 100
@ rows: 2
select id, name, email from user;
> id name email
> -- ---- -----
> 1 andrew [email protected]
> 2 lester [email protected]
> 3 chester [email protected]
@ rows: 3
package com.jthompson;
import java.io.File;
import java.sql.Connection;
import java.sql.ResultSet;
import lombok.extern.java.Log;
import org.jooq.tools.jdbc.MockConnection;
import org.jooq.tools.jdbc.MockDataProvider;
import org.jooq.tools.jdbc.MockFileDatabase;
import org.testng.annotations.Test;
@Log
public class TestJooqConnection
{
@Test
public void TestConn() throws Exception
{
MockDataProvider provider = new MockFileDatabase(new File("src/test/java/com/jthompson/input.sql"));
Connection conn = new MockConnection(provider);
ResultSet rs = conn.createStatement().executeQuery("select id, description, amount from account");
while(rs.next())
{
log.info("ID: " + rs.getString("id"));
log.info("DESCRIPTION: " + rs.getString("description"));
log.info("AMOUNT: " + rs.getString("amount"));
}
ResultSet rs2 = conn.createStatement().executeQuery("select id, name, email from user");
while(rs2.next())
{
log.info("ID: " + rs2.getString("id"));
log.info("NAME: " + rs2.getString("name"));
log.info("EMAIL: " + rs2.getString("email"));
}
}
}
@enricopulatzo
Copy link

Looks like its current form doesn't play well with Spring JDBC due to SQLFeatureNotSupportedExceptions. Additional effort will be needed to fake those.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment