In Grails 3.1.12 möchte ich Unit-Test einen Dienst:Grails Spock Unit-Test erfordert Mock Transaktionsmanager
@Transactional
class PlanService {
List<Plan> getPlans(Map params) {
def currentUser = (User)springSecurityService.getCurrentUser()
return Plan.findAllByCompany(currentUser.employer, params)
}
}
So:
@TestFor(PlanService)
@Mock([Plan, User, Company])
class PlanServiceSpec extends Specification {
void "Retrieve plan from the current user"() {
setup:
// create and save entities here
when: "the plans are retrieved"
def params = null
def plans = service.getPlans(params)
then: "the result should only include plans associated to the current user's company"
plans.size() == 2
}
den Test von der Konsole ausgeführt wird:
grails> test-app my.PlanServiceSpec -unit
schlägt mit:
my.FundingPlanServiceSpec > Retrieve plan from the current user FAILED
java.lang.IllegalStateException at PlanServiceSpec.groovy:48
und im Prüfbericht (HTML):
java.lang.IllegalStateException: No transactionManager was specified.
Using @Transactional or @Rollback requires a valid configured transaction manager.
If you are running in a unit test ensure the test has been properly configured
and that you run the test suite not an individual test method.
Nun, wenn ich die @Transactional
Anmerkung im Dienst auf Kommentar, der Test bestanden, aber das ist nicht die richtige Umsetzung. Ich bin in der Lage, um das Problem zu umgehen, indem Sie den Transaktionsmanager verspotten:
service.transactionManager = Mock(PlatformTransactionManager) {
getTransaction(_) >> Mock(TransactionStatus)
}
Aber das scheint sehr peinlich, wenn nicht falsch.
Gibt es eine Beschwörung, die ich vergessen habe aufzurufen?
EDIT: sieht ähnlich aus an old bug, aber es wurde mehr als ein Jahr geschlossen.
hatte ich völlig übersehen, dass Punkt. Das ist eine bessere Problemumgehung. Vielen Dank. – youri