andy mccullough
andy mccullough

Reputation: 9601

Creating domain objects when testing a grails service

I am trying to create unit tests for testing my grails services. I have the following test

@TestFor(ActivityProcessorService)

@Mock([ActivityProcessorService, Activity])
class ActivityProcessorServiceTests extends GrailsUnitTestCase{
void setUp() {

}

void tearDown() {
    // Tear down logic here
}

void testGenerateDescription() {

    def activity = new Activity(
        //new activity details
        )

   def service = mockFor(ActivityProcessorService)
   def description = service.generateDescription(activity)

   assert description == "something..."
 }
}

My problem is when creating an Activity object and populating all the required fields, it requires me to create several other objects, such as User, Task and some others, where these objects can be quite large, which has a knock on effect that they require the creation of objects etc.

Is there a way that I can create an Activity object but omit the creation of fully populated objects such as Task, User and other large objects?

E.g

def activity = new Activity(
        task: new Task(),
        user: new User(),
        ... and so on
        )

where Task and User are mocked up rather than creating full objects such as

def activity = new Activity(
        task: new Task(
                title : "task title"
                description : "task description"
                ... and so on
            ),
        user: new User(
                firstName : "john",
                lastName : "smith",
                ... and so on
            ),
        ... and so on
        )

as this will make a rather large overhead for creating such a small and simple test.

Upvotes: 0

Views: 1291

Answers (1)

dmahapatro
dmahapatro

Reputation: 50275

Refer this release notes, you have to manually specify Task and User in @Mock or @Build (build-test-data-plugin: 2.0.5)

Upvotes: 1

Related Questions