Created
May 5, 2014 22:40
-
-
Save stigberg/11549228 to your computer and use it in GitHub Desktop.
USing embedded HSQLDB for testing jdbc calls, etc.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package no.mulletman; | |
| import org.junit.Test; | |
| import org.springframework.jdbc.core.JdbcTemplate; | |
| import org.springframework.jdbc.core.RowMapper; | |
| import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; | |
| import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; | |
| import java.sql.ResultSet; | |
| import java.sql.SQLException; | |
| import java.util.List; | |
| import static org.junit.Assert.*; | |
| public class AppTest { | |
| @Test | |
| public void testSqlAgainsEmbeddedDB() throws Exception { | |
| final EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); | |
| final EmbeddedDatabase db = builder.addScripts("schema.sql", "data.sql").build(); | |
| final JdbcTemplate jdbcTemplate = new JdbcTemplate(db); | |
| final List<DataObject> wsHandlers = jdbcTemplate.query("SELECT * FROM HANDLERS", | |
| new RowMapper<DataObject>() { | |
| @Override | |
| public DataObject mapRow(ResultSet resultSet, int i) throws SQLException { | |
| return new DataObject(resultSet.getLong("ID")); | |
| } | |
| }); | |
| db.shutdown(); | |
| assertEquals(4, wsHandlers.size()); | |
| } | |
| private class DataObject { | |
| private Long id; | |
| private DataObject(Long id) { | |
| this.id = id; | |
| } | |
| public Long getId() { | |
| return id; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment