How to quickly create a test database for Cucumber-jvm?

I use cucumber-jvm to test the behavior of the legacy system I'm working on. I have to use Java 1.5 and Hibernate 3.3, updating is not an option. Since during my tests it stores some objects in a database, I created a new development database.

My concern is that I have to manually record the entries (using sql script) every time I restart my tests or they fail. And anyone who wants to launch them will have to do the same. I want to quickly and automatically clear the test database, either:

  • Create an empty database and populate it with what I need, or
  • Using an existing database, discarding records before starting tests.

What I still have: I use the cucumber-junit plugin, and the RunTests class is redirected 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 tried using the hibernate.hbm2ddl.auto property with a create-drop value and using the import.sql file to prepare the database, but it takes a lot of time to run the tests and it seems that it does not detect my import.sql file.

Unfortunately, using Maven and its excellent maven-sql-plugin is not an option (I suggested switching to Maven to no avail). Is there an alternative?

+4
source share
1 answer

I did it!

I used this ScriptRunner :

 @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); } } } 
+1
source

Source: https://habr.com/ru/post/1498677/


All Articles