Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails/Groovy - using multiple @TestMixin

In Grails I have a service that I want to unit test. The service uses these imports:

import grails.converters.JSON
import grails.web.JSONBuilder

I want the service to do get data and convert it to JSON:

def tables = DataProfileStats.withCriteria {
        projections {
            distinct("tableName")               
            }
        };

The helper method I wrote to build the JSON is:

public String buildNodeString(String nodeText)
{
    def builder = new JSONBuilder();

    JSON result = builder.build {
        hello = "world"
        };

    return result.toString();
}

In the unit test I have to add @TestMixin(ControllerUnitTestMixin) so the JSON adapter is loaded. But I also have to add @TestMixin(DomainClassUnitTestMixin) so I can mock the database object.

Any ideas on how to have multiple @TestMixin or is this a design issue with me having a import grails.web.JSONBuilder in a service class? Otherwise, I have to use a JAVA/JSON library or put the JSON stuff in a controller.

This is what I want the test to look like:

@TestMixin(ControllerUnitTestMixin)
@TestMixin(DomainClassUnitTestMixin)
class JsTreeJSONGeneratorServiceTests {

void testSomething() {

    DataProfileStats stats1 = new DataProfileStats();
    stats1.tableName = "table";

    mockDomain(DataProfileStats, stats1);

    JsTreeJSONGeneratorService service = new JsTreeJSONGeneratorService();
    String json = service.buildNodeString();
    assert json != "";

}

}

I get a @TestMixin(ControllerUnitTestMixin) @TestMixin(DomainClassUnitTestMixin) class JsTreeJSONGeneratorServiceTests {

void testSomething() {

    DataProfileStats stats1 = new DataProfileStats();
    stats1.tableName = "table";

    mockDomain(DataProfileStats, stats1);

    JsTreeJSONGeneratorService service = new JsTreeJSONGeneratorService();
    String json = service.buildNodeString();
    assert json != "";

}

}

I get a @TestMixin(ControllerUnitTestMixin) @TestMixin(DomainClassUnitTestMixin) class JsTreeJSONGeneratorServiceTests {

void testSomething() {

    DataProfileStats stats1 = new DataProfileStats();
    stats1.tableName = "table";

    mockDomain(DataProfileStats, stats1);

    JsTreeJSONGeneratorService service = new JsTreeJSONGeneratorService();
    String json = service.buildNodeString();
    assert json != "";

}

}

I get a "Cannot specify duplicate annotation on the same member : grails.test.mixin.TestMixin" exception.

Thanks

like image 353
Scott Avatar asked Oct 26 '11 13:10

Scott


1 Answers

Found it!

@TestMixin([GrailsUnitTestMixin, ControllerUnitTestMixin, DomainClassUnitTestMixin])
like image 109
Scott Avatar answered Oct 04 '22 19:10

Scott