使用TestNG测试数据库不回滚

时间:2017-10-30 18:48:41

标签: java mysql spring testng spring-test

我正在尝试使用DB和TestNG进行一些基本的自动化测试,但它不起作用。第一次运行成功,正如我所料,第二次运行并不是因为第一次运行从未回滚过。我看了几个地方的例子,似乎是正确的。任何人都知道我错过了什么

    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.11</version>
        <scope>test</scope>
    </dependency>

代码:

import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
import org.springframework.transaction.annotation.Transactional;
import org.testng.annotations.Test;

@ContextConfiguration(classes = AutomatedTest.Context.class)
public class RollbackTest extends AbstractTransactionalTestNGSpringContextTests {


  @Test
  @Rollback
  @Transactional
  public void testThing() throws Exception {

    Class<? extends RollbackTest> c = this.getClass();
    String path = String.format("/%s.sql", c.getName().replaceAll("\\.", "/"));
    super.executeSqlScript(path, false);
  }

  @Configuration
  @PropertySource("db.properties")
  static class Context {

    @Bean
    public DataSource dataSource(
        @Value("${datasource.url}") String url,
        @Value("${datasource.username}") String user,
        @Value("${datasource.password}") String pass,
        @Value("${datasource.driver-class-name}") String driver) throws PropertyVetoException {
      ComboPooledDataSource ds = new ComboPooledDataSource();
      ds.setUser(user);
      ds.setPassword(pass);
      ds.setJdbcUrl(url);
      ds.setDriverClass(driver);
      return ds;
    }

    @Bean
    public Connection connection(DataSource dataSource) throws SQLException {
      Connection c = dataSource.getConnection();
      System.out.println("Connection is " + c);
      return c;
    }

    @Bean
    public DataSourceTransactionManager txMan(DataSource ds) {
      return new DataSourceTransactionManager(ds);
    }
  }
}

SQL:

CREATE SCHEMA FOO;

CREATE TABLE FOO.BAZ (
  ID int PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(256) NOT NULL
);

INSERT INTO FOO.BAZ (name) values('christian');

错误:

CREATE SCHEMA FOO

org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of class path resource [automated/RollbackTest.sql]: CREATE SCHEMA FOO; nested exception is java.sql.SQLException: Can't create database 'FOO'; database exists

1 个答案:

答案 0 :(得分:1)

返回@Bean的{​​{1}}方法看起来非常可疑。所以我建议你删除它。

为了在之前执行SQL脚本,在 之后执行测试方法,理想情况下应该查看Spring的Connection注释。

此外,您可以安全地删除测试方法上的@Sql@Rollback声明。

  • @Transactional是默认行为。
  • @Rollback已在AbstractTransactionalTestNGSpringContextTests上声明。

此致

Sam( Spring TestContext Framework的作者