Skip to content

Instantly share code, notes, and snippets.

@arey
Created September 6, 2013 06:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arey/6460147 to your computer and use it in GitHub Desktop.
Save arey/6460147 to your computer and use it in GitHub Desktop.
Test case mixing both the DbSetup and the Spring frameworks
package com.javametmoi.test.dbsetup;
import static com.ninja_squad.dbsetup.Operations.insertInto;
import static com.ninja_squad.dbsetup.Operations.sequenceOf;
import static org.junit.Assert.assertEquals;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.javametmoi.test.dbsetup.TransactionAwareDestination;
import com.ninja_squad.dbsetup.destination.Destination;
import com.ninja_squad.dbsetup.generator.ValueGenerators;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Transactional;
import com.ninja_squad.dbsetup.DbSetup;
import com.ninja_squad.dbsetup.operation.Operation;
/**
* Blog example of a test case mixing both the DbSetup and the Spring frameworks.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@Transactional
public class TestSpringDbSetup {
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
private Destination destination;
@Configuration
static class Config {
@Bean
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript("schema.sql")
.build();
}
@Bean
public JdbcTemplate jdbcTemplate() {
return new JdbcTemplate(dataSource());
}
@Bean
Destination destination() {
return new TransactionAwareDestination(dataSource(), transactionManager());
}
}
@Test
public void indexCustomerWithSimilarName() throws SQLException {
// Prepare
Operation operation = insertInto("CUSTOMER")
.withGeneratedValue("ID", ValueGenerators.sequence())
.columns("NAME")
.values("Antoine")
.values("ANTOINE")
.values("Antoinette")
.values("Pierre-Antoine")
.build();
new DbSetup(destination, operation).launch();
// Exercise
// ...
// Verify
int count = jdbcTemplate.queryForObject("select count(*) from customer",
Integer.class);
assertEquals(4, count);
}
private static final int CUSTOMER_1 = 1;
@Test
public void indexSingleCustomer() {
Operation operation = insertInto("CUSTOMER")
.columns("ID", "NAME")
.values(CUSTOMER_1, "James")
.build();
new DbSetup(destination, operation).launch();
int id = jdbcTemplate.queryForObject("select id from customer where id=?", Integer.class, CUSTOMER_1);
assertEquals(CUSTOMER_1, id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment