【问题标题】:Rolling Back after each test using J2EE with Jboss使用 J2EE 和 Jboss 在每次测试后回滚
【发布时间】:2015-05-18 13:39:41
【问题描述】:

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

@TransactionConfiguration(defaultRollback=true)

现在我将 J2EE 与 arquillian 和 Jboss 应用服务器一起使用。有没有类似的方法?

【问题讨论】:

    标签: java jakarta-ee jpa testing jboss


    【解决方案1】:

    据谷歌告诉我,@TransactionConfiguration 是 Spring Framework 4.0 的一部分。 所以你应该在你的新项目中包含 Spring——即使它只是为了测试。

    【讨论】:

    • 我用不了spring,当前使用的栈是jboss
    【解决方案2】:

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

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

    触发器、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);
            }
    

    【讨论】:

    • 即使在两次测试之间?我的意思是,在为特定的 junit 类运行测试之后,它会在为下一个类运行测试之前回滚吗?
    • @Mari 我们为每个测试创建一个新数据库 - 仍然比使用真实数据库快得多。
    • 是否有任何特殊配置可以为每个测试创建一个新的数据库,还是默认配置?
    • @Mari 查看更新,例如,它使用 hsqldb,但我更喜欢 H2,设置类似。
    • 在您的回答中看到了更新。我不确定它是否会以这种方式工作,因为我正在使用 arquillian 和 h2
    猜你喜欢
    • 2012-11-26
    • 2018-07-21
    • 1970-01-01
    • 2023-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多