使用带有Jboss的J2EE进行每次测试后回滚

时间:2015-05-18 13:39:42

标签: java java-ee jpa testing jboss

我在上一个项目中使用了hibernate + Spring。设置持久性测试非常容易,因为在测试类的顶部使用以下注释,每次测试后将回滚数据库中的更改。

@TransactionConfiguration(defaultRollback=true)

现在我正在使用带有arquillian和Jboss应用服务器的J2EE。有没有办法同样地做到这一点?

2 个答案:

答案 0 :(得分:0)

据Google称,@TransactionConfiguration是Spring Framework 4.0的一部分。 因此,您应该在新项目中包含Spring - 即使它仅用于测试。

答案 1 :(得分:0)

使用内存数据库代替。更快,不必担心回滚。

您可以在每次测试之前或每个套件之前运行完整的架构创建。

触发器,Sprocs等更多问题 - 但无论如何你都不应该使用它们。

以此为例进行子类化

public class BaseTest {
    private static EJBContainer ejbContainer;

        @Resource
        protected DataSource dataSource;
        protected String dataFilename = "data.sql"; // the default can be overriden in subclass

        @BeforeClass
        public static void startTheContainer() throws Exception {

            // boot the container and perform dependency injection
            final Properties p = new Properties();
            p.put("yourDB", "new://Resource?type=DataSource");
            p.put("yourDB.JdbcDriver", "org.hsqldb.jdbcDriver");
            String schema = System.getProperty("testDatabaseSchema", "racing");
            p.put("yourDB.JdbcUrl", "jdbc:hsqldb:mem:" + schema);
            p.put("yourDB.TestOnBorrow", "false");
            p.put("javax.persistence.provider", "org.hibernate.ejb.HibernatePersistence");
            p.put("openejb.embedded.remotable", "false");
            p.put("openejb.descriptors.output", "false");
            p.put("openejb.jmx.active", "false");
            p.put("hsql.disabled", "false");

            // Logging config
            // Info from http://openejb.apache.org/configuring-logging-in-tests.html
            p.put("log4j.appender.C.layout", "org.apache.log4j.PatternLayout");
            p.put("log4j.appender.C.layout.ConversionPattern", "%-5p %d{ABSOLUTE} %c: %m%n");
            p.put("log4j.category.OpenEJB.options", "warn");

            ejbContainer = EJBContainer.createEJBContainer(p);

        }

        @AfterClass
        public static void afterClass() {
            if (ejbContainer != null) {
                ejbContainer.close();
            }
        }

        @Before
        public void setUp() throws Exception {
            ejbContainer.getContext()
                        .bind("inject", this);

            // check everything booted ok and is ready to go
            assertNotNull("Data source is null", dataSource);

            // setup database with SQL script
            final Connection connection = dataSource.getConnection();
            try {
                String script = getScript("schema.ddl");
                final Statement statement = connection.createStatement();
                statement.execute(script);
                String dataScript = getScript(dataFilename);
                final Statement dataStatement = connection.createStatement();
                dataStatement.execute(dataScript);
                connection.commit();
            } finally {
                connection.close();
            }
        }


        @After
        public void tearDown() throws Exception {
            final Connection connection = dataSource.getConnection();
            try {
                final Statement stmt = connection.createStatement();
                stmt.execute("DROP SCHEMA PUBLIC CASCADE");
                connection.commit();
            } finally {
                connection.close();
            }
        }

        protected String getScript(String scriptFilename) throws Exception {
            URL url = Resources.getResource(scriptFilename);
            return Resources.toString(url, Charsets.UTF_8);
        }