I am using cucumber-jvm to test the behaviour of the legacy system I'm working on at work. I have to use Java 1.5 and Hibernate 3.3, upgrading is not an option. Since, during my tests, it stores some objects in the database, I have created a new development database.
What bothers me is that I have to manually drop the records (using a sql script) everytime I'll rerun my tests, or they'll fail. And anyone else wanting to run them would have to do the same. I want to quickly and automatically clean my test database, by either:
What I have so far: I'm using the cucumber-junit plugin, and the RunTests class redirects to my test database:
@RunWith(Cucumber.class)
@Cucumber.Options(
features = "test/resources/cucumber",
format = "html:target/cucumber"
)
public class RunTests {
private static Configuration configuration;
@BeforeClass
public static void preparaBase() {
// gets the mapped configuration to the db
configuration = HibernateUtil.getConfiguration();
configuration.setProperty("hibernate.connection.url", "test-db-url");
configuration.setProperty("hibernate.connection.username", "user");
configuration.setProperty("hibernate.connection.password", "pass");
// configuration.setProperty("hibernate.hbm2ddl.auto", "create-drop");
// rebuilds the configuration using my test database
HibernateUtil.rebuildSessionFactory(configuration);
}
}
I have tried using the hibernate.hbm2ddl.auto
property with the create-drop
value and using the import.sql
file to prepare the database, but it takes ages to start the tests and it seems that it's not detecting my import.sql file.
Sadly, using Maven and its excellent maven-sql-plugin is not an option (I have suggested a switch to Maven, to no avail). Is there an alternative?
I did it!
I used this ScriptRunner class as such:
@RunWith(Cucumber.class)
@Cucumber.Options(
features = "test/resources/cucumber",
format = "html:target/cucumber"
)
public class RunTests {
private static Configuration configuration;
String url = "test-db-url";
String user = "user";
String pass = "pass";
@BeforeClass
public static void preparaBase() {
// gets the mapped configuration to the db
configuration = HibernateUtil.getConfiguration();
configuration.setProperty("hibernate.connection.url", url);
configuration.setProperty("hibernate.connection.username", user);
configuration.setProperty("hibernate.connection.password", pass);
// rebuilds the configuration using my test database
HibernateUtil.rebuildSessionFactory(configuration);
// executes a script stored in test/resources/cucumber
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(url, user, pass);
ScriptRunner runner = new ScriptRunner(conn, false, true);
runner.runScript(new BufferedReader(new FileReader("test/resources/cucumber/db.sql")));
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
}