[英]Unit test that tests ExpectedException is failing though exception is thrown
[英]@ExpectedException in grails unit tests
有人在grails單元測試中使用過這個注釋嗎? 似乎沒有為我工作。 謝謝。 d
更新:我下面測試的最后一行確實拋出了預期的異常。 但是測試失敗了(這里的堆棧跟蹤太大......)。 我正在使用grails 1.2並在eclipse的junit runner中運行測試。 也許grails使用的是早期版本的junit而不是4?
/**
* Get the EC by a manager of a different company. Should throw exception
*/
@ExpectedException(ServiceAuthorizationException.class)
void testGetEcByNonOwnerManagerOfDifferentCompany() {
mockDomain(ExpenseClaim , [new ExpenseClaim(id:"1",narrative:"marksClaim", employee:userMark, company:dereksCompany)])
def authControl = mockFor(AuthenticateService)
authControl.demand.userDomain(1..1) {-> otherUserMgr }
authControl.demand.ifAllGranted(1..1) {String arg1 -> return "ROLE_COMPANYMANAGER".equals(arg1) } //returns true
def testService = new ExpenseClaimService()
testService.authenticateService = authControl.createMock()
def thrown = false
testService.getExpenseClaim("1")
}
目前僅支持JUnit 3,因此請使用shouldFail()
:
void testGetEcByNonOwnerManagerOfDifferentCompany() {
shouldFail(ServiceAuthorizationException) {
mockDomain(ExpenseClaim , [new ExpenseClaim(id:"1",
narrative:"marksClaim", employee:userMark,
company:dereksCompany)])
def authControl = mockFor(AuthenticateService)
authControl.demand.userDomain(1..1) {-> otherUserMgr }
authControl.demand.ifAllGranted(1..1) {String arg1 ->
"ROLE_COMPANYMANAGER".equals(arg1) } //returns true
def testService = new ExpenseClaimService()
testService.authenticateService = authControl.createMock()
testService.getExpenseClaim("1")
}
}
shouldFail()
實際上更方便,因為每次測試可以多次使用它,並且它返回異常消息,因此您可以根據消息和異常進行斷言。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.