Java 使用J2EE和Jboss在每次测试后回滚

Java 使用J2EE和Jboss在每次测试后回滚,java,jakarta-ee,jpa,testing,jboss,Java,Jakarta Ee,Jpa,Testing,Jboss,我在上一个项目中使用了hibernate+Spring。设置持久性测试非常容易,因为在测试类的顶部使用以下注释,数据库中的更改将在每次测试后回滚 @TransactionConfiguration(defaultRollback=true) 现在我将J2EE与arquillian和Jboss应用服务器一起使用。有类似的方法吗?据谷歌告诉我@TransactionConfiguration是SpringFramework4.0的一部分。 因此,您应该在新项目中包括Spring,即使它只是用于测试。

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

@TransactionConfiguration(defaultRollback=true)
现在我将J2EE与arquillian和Jboss应用服务器一起使用。有类似的方法吗?

据谷歌告诉我@TransactionConfiguration是SpringFramework4.0的一部分。
因此,您应该在新项目中包括Spring,即使它只是用于测试。

使用内存中的db。更快,无需担心回滚

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

触发器、存储过程等更是个问题,但无论如何都不应该使用它们

例如,将其子类化

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);
        }

我不能使用spring,当前使用的堆栈是JBoss,甚至在测试之间?我的意思是,在为特定junit类运行测试之后,它会在为下一个类运行测试之前回滚吗?@Mari我们为每个测试创建一个新的数据库-仍然比使用真实的数据库快得多。是否有任何特殊配置为每个测试创建一个新的数据库,或者是默认的?@Mari请参阅使用hsqldb的更新,但我更喜欢H2的设置将是类似的。看到更新在您的答案。我不确定它是否能以这种方式工作,因为我使用的是arquillian和h2