Grails的服务交易(Grails Service Transactions)

2019-10-19 04:13发布

我试图让一个Grails服务中工作的交易,但我没有得到我期望的结果。 谁能告诉我,如果我做错事,如果我的假设是关闭?

我的域类:

class Account {

  static constraints = {
    balance(min: 0.00)
  }

  String companyName
  BigDecimal balance = 0.00
  Boolean active = true

  String toString() {
    return "${companyName} : ${balance}"
  }
}

我的服务:

class AccountService {

  static transactional = true

  def transfer(Account source, Account destination, amount) throws RuntimeException {

    if (source.active && destination.active) {
      source.balance -= amount

      if (!source.save(flush: true)) {
        throw new RuntimeException("Could not save source account.")
      } else {
        destination.balance += amount

        if (!destination.save(flush: true)) {
          throw new RuntimeException("Could not save destination account.")
        }
      }
    } else {
      throw new RuntimeException("Both accounts must be active.")
    }
  }

  def someMethod(Account account) throws RuntimeException {

    account.balance = -10.00

    println "validated: ${account.validate()}"

    if(!account.validate()) {
      throw new RuntimeException("Rollback!")
    }
  }
}

我的单元测试:进口grails.test *。

class AccountServiceTests extends GrailsUnitTestCase {

  def AccountService

  protected void setUp() {
    super.setUp()
    mockDomain(Account)
    AccountService = new AccountService()
  }

  protected void tearDown() {
    super.tearDown()
  }

  void testTransactional() {
    def account = new Account(companyName: "ACME Toy Company", balance: 2000.00, active: true)

    def exception = null

    try {
      AccountService.someMethod(account)
    } catch (RuntimeException e) {
      exception = e
    }

    assert exception instanceof RuntimeException

    println "exception thrown: ${exception.getMessage()}"

    assertEquals 2000.00, account.balance
  }
}

结果:

Testsuite: AccountServiceTests
Tests run: 1, Failures: 1, Errors: 0, Time elapsed: 1.068 sec
------------- Standard Output ---------------
--Output from testTransactional--
validated: false
exception thrown: Rollback!
------------- ---------------- ---------------
------------- Standard Error -----------------
--Output from testTransactional--
------------- ---------------- ---------------

Testcase: testTransactional took 1.066 sec
    FAILED
expected:<2000.00> but was:<-10.00>
junit.framework.AssertionFailedError: expected:<2000.00> but was:<-10.00>
    at AccountServiceTests.testTransactional(AccountServiceTests.groovy:89)
    at _GrailsTest_groovy$_run_closure4.doCall(_GrailsTest_groovy:203)
    at _GrailsTest_groovy$_run_closure4.call(_GrailsTest_groovy)
    at _GrailsTest_groovy$_run_closure2.doCall(_GrailsTest_groovy:147)
    at _GrailsTest_groovy$_run_closure1_closure19.doCall(_GrailsTest_groovy:113)
    at _GrailsTest_groovy$_run_closure1.doCall(_GrailsTest_groovy:96)
    at TestApp$_run_closure1.doCall(TestApp.groovy:66)
    at gant.Gant$_dispatch_closure4.doCall(Gant.groovy:324)
    at gant.Gant$_dispatch_closure6.doCall(Gant.groovy:334)
    at gant.Gant$_dispatch_closure6.doCall(Gant.groovy)
    at gant.Gant.withBuildListeners(Gant.groovy:344)
    at gant.Gant.this$2$withBuildListeners(Gant.groovy)
    at gant.Gant$this$2$withBuildListeners.callCurrent(Unknown Source)
    at gant.Gant.dispatch(Gant.groovy:334)
    at gant.Gant.this$2$dispatch(Gant.groovy)
    at gant.Gant.invokeMethod(Gant.groovy)
    at gant.Gant.processTargets(Gant.groovy:495)
    at gant.Gant.processTargets(Gant.groovy:480)

我的期望:

当帐户被授予负平衡,它不应该验证(它没有),一个RuntimeException应甩(它是),以及帐户应该回滚到之前的状态(平衡:2000),这是它分崩离析。

我缺少的是在这里吗?

Answer 1:

单元测试是刚刚的Groovy或Java类,所以没有Spring应用程序上下文,因此没有交易的支持。 你不得不嘲笑,对于一个单元测试,但不会是事务性的测试,只是嘲笑的质量。 将它转换为一个集成测试,不上的服务电话新的,使用依赖注入:

class AccountServiceTests extends GroovyTestCase {

  def AccountService

  void testTransactional() {
    def account = new Account(companyName: "ACME Toy Company", balance: 2000.00,
                              active: true)
    account.save()
    assertFalse account.hasErrors()

    String message = shouldFail(RuntimeException) {
      AccountService.someMethod(account)
    }

    println "exception thrown: $message"

    assertEquals 2000.00, account.balance
  }
}

需要注意的是实际的异常可能是一个包装例外的同时抛出异常作为它的原因。



Answer 2:

我试过的代码,但我看到在集成测试同样的问题。 我使用的Grails 1.2

根据吉拉GRAILS-3765 ,这是一个已知问题,仍处于打开状态。 (我不知道为什么它只是说“影响版本1.0.4”当1.1.x版本已经出来很长时间)。

基于这几点,我认为这是Grails中的错误。 该吉拉注有一种变通方法,但我还没有尝试过。 根据这个问题,但是,它仍然会在运行应用程序时的工作; 这可以手动进行确认。



Answer 3:

什么版本的Grails,你运行? V1.1.1具有这样的事务=真正的不正常的错误。



文章来源: Grails Service Transactions