Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DbUnitHelper {
- private Connection conexao;
- private DatabaseConnection conexaoDBUnit;
- private String xmlFolder;
- public DbUnitHelper(String xmlFolder) {
- this.xmlFolder = xmlFolder;
- try {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- conexao = DriverManager.getConnection("jdbc:mysql://localhost/teste_dbunit", "root", "root");
- conexaoDBUnit = new DatabaseConnection(conexao);
- DatabaseConfig config = conexaoDBUnit.getConfig();
- config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new MySqlDataTypeFactory());
- } catch (Exception e) {
- throw new RuntimeException("Erro inicializando DBUnit", e);
- }
- }
- public void execute(DatabaseOperation operation, String xml) {
- try {
- InputStream is = getClass().getResourceAsStream("/" + xmlFolder + "/" + xml);
- FlatXmlDataSetBuilder builder = new FlatXmlDataSetBuilder();
- IDataSet dataSet = builder.build(is);
- operation.execute(conexaoDBUnit, dataSet);
- } catch (Exception e) {
- throw new RuntimeException("Erro executando DbUnit", e);
- }
- }
- public void close() {
- try {
- conexaoDBUnit.close();
- conexao.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement