Grails Integration Tests with Multiple Services

What is the best practice testing Grails service that depends on another service? By default, mixin TestFor correctly enters the service under test, for example:

@TestFor(TopService) 
class TopServiceTests {
    @Test
    void testMethod() {
        service.method()
    }
}

but if my TopService (service) instance relies on another service, like InnerService:

class TopService {
    def innerService
}

innerService will not be available, dependency injection does not seem to populate this variable. How do I proceed?

+5
source share
2 answers

@TestFor, extend GroovyTestCase. ( , @Mock). .

GroovyTestCase,

def topService

, .

unit test setUp. :

@TestFor(TopService) 
class TopServiceTests {
    @Before public void setUp() {
        service.otherService = new OtherService()
    }
    ...
+8

CustomerRegistrationServiceTest, CustomerRegistrationService PasswordService.

CustomerRegistrationService :

class CustomerRegistrationService {
    def passwordService

CustomerRegistrationServiceTest :

@TestFor(CustomerRegistrationService)
@Mock(Customer)
class CustomerRegistrationServiceTests extends GrailsUnitTestMixin {

    void setUp() {
        mockService(PasswordService)
    }

, CustomerRegistrationService, PasswordService

+1

All Articles