SliM 在 Spring 应用程序上测试事务方法期间出现 SqlException

发布于 2024-12-19 22:45:01 字数 2492 浏览 2 评论 0原文

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

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

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

查询表 SliM FitNesse 页面上的测试步骤

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

|Query: Test Case   |   test_file_setup.csv   |
|     name     |     phone     |     email     |
|     tom      |     123       |  [email protected] |
|     dick     |     456       | [email protected] |
|     harry    |     789       |[email protected] |

:测试通过

2 测试页测试具有无效行的测试文件,因此当它执行该行时,它仅回滚该行,但所有其他行都会继续,但是......测试失败并回滚所有行并关闭数据源。

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

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

这是我的测试类

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 实例)。然而结果却证明我错了?!

When running Fitnesse SliM test a transactional method on spring application, a SQLException thrown - Data Source is Closed.

Here is what happends:
In my application, method importFile(String filename) reads a csv file and update data to a few tables in database. This is a transactional method of spring application. So that if updating one row of the file throws an exception, all updates of this row rollbacks and all other rows (including previous rows and next rows) keep on updating.

See related link: How to define rollback transactions which are called inside of a loop with spring aop?

The testing step on Query table SliM FitNesse page

1 SetUp page sets up the testing data with a setup file

|Query: Test Case   |   test_file_setup.csv   |
|     name     |     phone     |     email     |
|     tom      |     123       |  [email protected] |
|     dick     |     456       | [email protected] |
|     harry    |     789       |[email protected] |

result: testing passed

2 Test page tests a testing file with a invalid row, so that when it executes this row it only rollbacks this row, but all other rows proceeds, however... the test failed and rollback all rows and closed the data source.

3 So when the tearDown which tries to test with a valid file and clean up the database failed too... thus the data source was closed

This is one SliM test but there is a query table on each SetUp, Test and tearDown page. My guessing is one Fitness test only init one Spring context instance. Tell me if I am wrong. However, this method is declared as a transaction on spring config. It rollbacks as expected, but the test doesn't.

Here is my testing class

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
     ...
  }
}

It seems that on the test page, Fitnesse starts a new testing process (creates a new TestClass instance) on SetUp, Test, TearDown. However, the result proves my wrong?!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文