Created
July 22, 2015 06:45
-
-
Save asv/2c465fd7887fa1706324 to your computer and use it in GitHub Desktop.
dbunit (springtestdbunit) postgresql truncate + reset sequences
This file contains 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 foo; | |
import com.github.springtestdbunit.TransactionDbUnitTestExecutionListener; | |
import com.github.springtestdbunit.annotation.DbUnitConfiguration; | |
import org.junit.runner.RunWith; | |
import org.springframework.boot.test.SpringApplicationConfiguration; | |
import org.springframework.test.annotation.DirtiesContext; | |
import org.springframework.test.context.TestExecutionListeners; | |
import org.springframework.test.context.TestPropertySource; | |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; | |
import org.springframework.test.context.support.DirtiesContextTestExecutionListener; | |
@RunWith(SpringJUnit4ClassRunner.class) | |
@SpringApplicationConfiguration(classes = TestAppConfig.class) | |
@TestPropertySource(locations = "/test.properties") | |
@TestExecutionListeners({ | |
DependencyInjectionTestExecutionListener.class, | |
DirtiesContextTestExecutionListener.class, | |
TransactionDbUnitTestExecutionListener.class | |
}) | |
@DbUnitConfiguration(databaseOperationLookup = PostgresDatabaseOperationLookup.class) | |
@DirtiesContext | |
public abstract class DbTestBase { | |
} |
This file contains 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 foo; | |
import com.github.springtestdbunit.annotation.DatabaseOperation; | |
import com.github.springtestdbunit.operation.DatabaseOperationLookup; | |
import org.dbunit.DatabaseUnitException; | |
import org.dbunit.database.IDatabaseConnection; | |
import org.dbunit.dataset.IDataSet; | |
import org.dbunit.operation.AbstractOperation; | |
import org.dbunit.operation.CompositeOperation; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import java.util.Arrays; | |
import java.util.Map; | |
import java.util.concurrent.ConcurrentHashMap; | |
import java.util.stream.Collectors; | |
public final class PostgresDatabaseOperationLookup implements DatabaseOperationLookup { | |
private static Map<DatabaseOperation, org.dbunit.operation.DatabaseOperation> OPERATION_LOOKUP; | |
static { | |
// TODO: migrate to ImmutableMap. | |
OPERATION_LOOKUP = new ConcurrentHashMap<>(); | |
OPERATION_LOOKUP.put(DatabaseOperation.CLEAN_INSERT, | |
new CompositeOperation( | |
new TruncateWithRestartSequences(), | |
org.dbunit.operation.DatabaseOperation.INSERT)); | |
// ... | |
} | |
@Override | |
public org.dbunit.operation.DatabaseOperation get(final DatabaseOperation operation) { | |
return OPERATION_LOOKUP.get(operation); | |
} | |
private static class TruncateWithRestartSequences extends AbstractOperation { | |
@Override | |
public void execute( | |
final IDatabaseConnection connection, | |
final IDataSet dataSet | |
) throws DatabaseUnitException, SQLException { | |
try(final Statement statement = connection.getConnection().createStatement()) { | |
final String tables = Arrays.stream(dataSet.getTableNames()) | |
.collect(Collectors.joining(", ")); | |
statement.execute("TRUNCATE " + tables + " RESTART IDENTITY CASCADE"); | |
} | |
} | |
} | |
} |
This file contains 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 foo; | |
import com.github.springtestdbunit.annotation.DatabaseSetup; | |
import org.junit.Test; | |
import org.junit.Assert; | |
@DatabaseSetup("something_db.xml") | |
public class SomethingTest extends DbTestBase { | |
@Test | |
public void testSomething() throws Exception { | |
Assert.assertTrue(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment