I am using the following solution.
(1) I have a simple interface called Fixture
package com.obecto.fixtures; public interface Fixture { void prepare(); }
(2) I am preparing an SQL device to populate an empty database - this can be done either through entities or by executing SQL as follows:
package com.avaco2.fixtures; import java.util.logging.Logger; import org.apache.commons.dbcp.BasicDataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; import org.springframework.stereotype.Component; import org.springframework.test.jdbc.SimpleJdbcTestUtils; import com.obecto.fixtures.Fixture; @Component(value="logEventsSQLFixture") public class LogEventsSQLFixture implements Fixture { private static String IMPORT_SQL = "import-LogEvents.sql"; private static Logger LOG = Logger.getLogger(LogEventsSQLFixture.class.getName()); @Autowired private BasicDataSource dataSource; @Override public void prepare() { SimpleJdbcTemplate jdbcTemplate = new SimpleJdbcTemplate(dataSource); Resource sqlScript = new ClassPathResource(IMPORT_SQL); try { SimpleJdbcTestUtils.executeSqlScript(jdbcTemplate, sqlScript, true); } catch (Exception e) { LOG.severe("Cannot import " + IMPORT_SQL); } } }
(3) Add your lights to the Test and prepare -them classes in the @Before method.
Always use a different database for tests, so you can safely use the create-drop setting for sleep mode. To reload the context before each test method, you can use the following annotation - @DirtiesContext(classMode=ClassMode.AFTER_EACH_TEST_METHOD)
source share