clean database after each test

This commit is contained in:
Athou
2023-08-23 20:21:45 +02:00
parent 66d1eb3f1f
commit b37680333c
4 changed files with 40 additions and 18 deletions

View File

@@ -0,0 +1,34 @@
package com.commafeed;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.codahale.metrics.MetricRegistry;
import io.dropwizard.testing.ResourceHelpers;
import io.dropwizard.testing.junit5.DropwizardAppExtension;
public class CommaFeedDropwizardAppExtension extends DropwizardAppExtension<CommaFeedConfiguration> {
public CommaFeedDropwizardAppExtension() {
super(CommaFeedApplication.class, ResourceHelpers.resourceFilePath("config.test.yml"));
}
@Override
public void after() {
super.after();
// clean database after each test
DataSource dataSource = getConfiguration().getDataSourceFactory().build(new MetricRegistry(), "cleanup");
try (Connection connection = dataSource.getConnection();
PreparedStatement statement = connection.prepareStatement("DROP ALL OBJECTS")) {
statement.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException("could not cleanup database", e);
}
}
}