1

在 Spring 应用程序上运行 Fitnesse SliM 测试事务方法时,抛出 SQLException - 数据源已关闭。

这是发生的事情:在我的应用程序中,方法 importFile(String filename) 读取一个 csv 文件并将数据更新到数据库中的几个表中。这是spring应用程序的事务方法。因此,如果更新文件的一行引发异常,则此行的所有更新都会回滚,并且所有其他行(包括前一行和下一行)都会继续更新。

请参阅相关链接:如何使用 spring aop 定义在循环内调用的回滚事务?

Query table SliM FitNesse 页面上的测试步骤

1 设置页面使用设置文件设置测试数据

|Query: Test Case   |   test_file_setup.csv   |
|     name     |     phone     |     email     |
|     tom      |     123       |  tom@test.com |
|     dick     |     456       | dick@test.com |
|     harry    |     789       |harry@test.com |

结果:测试通过

2 测试页面测试一个包含无效行的测试文件,因此当它执行这一行时,它只回滚这一行,但所有其他行继续进行,但是......测试失败并回滚所有行并关闭数据源。

3 因此,当尝试使用有效文件进行测试并清理数据库的 tearDown 也失败时......因此数据源已关闭

这是一个 SliM 测试,但在每个 SetUp、Test 和 tearDown 页面上都有一个查询表。我的猜测是一项健身测试仅初始化一个 Spring 上下文实例。告诉我我是否错了。但是,此方法在 spring 配置中被声明为事务。它按预期回滚,但测试没有。

这是我的测试课

public class TestCase{

  private ImportFile importFile; 
  private String filename;

  public TestCase(String filename){
     this.filename = filename;
     init();
  }

  public void init(){
     //initialize the spring context
     //and get beans...
     importFile = (ImportFile) context.getBeans("importFile");
     start();
  }

  public void start(){
     //read rows from file...
     for( ... loop per row...){
        //this is a transactional method...
        //it reads the file and update every row to the database
        //it is supposed to row back one row (per loop) 
        //and continue if an exception thrown
        //but it rollback the all rows and close the datasource
        importFile.importFile(filename);
     }
  }

  public List<Object> query(){
     //read from database and return the test result to the Slim query table
     ...
     cleanup();
  }

  public void cleanup(){
     //close the spring context
     ...
  }
}

似乎在测试页面上,Fitnesse 在 SetUp、Test、TearDown 上开始了一个新的测试过程(创建一个新的 TestClass 实例)。然而,结果证明我错了?!

4

0 回答 0