I have a Grails integration test that extends GroovyTestCase with two test methods. The first method executes successfully, but the second fails with a groovy.lang.MissingMethodException:
Failure: testMapBudgetFailure(com.ross.budget.BudgetServiceTests)
groovy.lang.MissingMethodException: No signature of method:
com.ross.budget.Budget.save() is applicable for argument types: () values: []
Possible solutions: save(), save(boolean), save(java.util.Map), wait(), last(), any()
at
com.ross.budget.BudgetServiceTests.testMapBudgetFailure(BudgetServiceTests.groovy:45)
Even though the exact same method call b.save() is in the first method. If I comment the first method the second test runs as expected. Why are the two test methods behaving differently?
Full class listing:
package com.ross.budget
import grails.test.mixin.*
import org.junit.*
/**
* See the API for {@link grails.test.mixin.services.ServiceUnitTestMixin} for usage instructions
*/
@TestFor(BudgetService)
class BudgetServiceTests extends GroovyTestCase {
BudgetService budgetService
void testMapBudgetSuccess() {
Budget b = new Budget()
b.month = new Date(2012, 9, 1)
b.amount = new BigDecimal(10.0)
b.save()
Account a = new Account()
a.name = "Test"
a.institution = "Test"
a.description = "Test Account"
a.save()
Transaction t = new Transaction()
t.account = a
t.postDate = new Date(2012, 9, 5)
t.amount = 10.0
t.save()
boolean result = budgetService.mapTransaction(t)
assertTrue("Returned failed match.", result)
assertNotNull("No budget set", t.budget)
}
void testMapBudgetFailure() {
Budget b = new Budget()
b.month = new Date(112, 5, 1)
b.amount = new BigDecimal(10.0)
b.save()
Account a = new Account()
a.name = "Test"
a.institution = "Test"
a.description = "Test Account"
a.save()
Transaction t = new Transaction()
t.account = a
t.postDate = new Date(112, 6, 5)
t.amount = 10.0
t.save()
boolean result = budgetService.mapTransaction(t)
assertFalse("Returned match.", result)
assertNull("Budget set", t.budget)
}
}
I know the code is copy paste and not lovely. It’s quick test case for a personal project
According to the Grails doc, you should either use
@TestForfor a unit-test or extendGroovyTestCasefor an integration test, not both.