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
Found it!
@TestMixin([GrailsUnitTestMixin, ControllerUnitTestMixin, DomainClassUnitTestMixin])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With