В Grails у меня есть служба, которую я хочу протестировать. Сервис использует следующие импорты:
import grails.converters.JSON
import grails.web.JSONBuilder
Я хочу, чтобы сервис получал данные и преобразовывал их в JSON:
def tables = DataProfileStats.withCriteria {
projections {
distinct("tableName")
}
};
Для создания JSON я написал вспомогательный метод:
public String buildNodeString(String nodeText)
{
def builder = new JSONBuilder();
JSON result = builder.build {
hello = "world"
};
return result.toString();
}
В модульном тесте я должен добавить @TestMixin(ControllerUnitTestMixin), чтобы JSON-адаптер был загружен. Но я также должен добавить @TestMixin(DomainClassUnitTestMixin), чтобы я мог подражать объекту базы данных.
Есть идеи, как иметь несколько @TestMixin или это проблема дизайна, связанная с тем, что у меня есть import grails.web.JSONBuilder в классе сервиса? В противном случае мне придется использовать библиотеку JAVA/JSON или поместить JSON в контроллер.
Вот как я хочу, чтобы выглядел тест:
@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 != "";
}
}
Я получаю @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 != "";
}
}
Я получаю @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 != "";
}
}
Я получаю исключение "Cannot specify duplicate annotation on the same member : grails.test.mixin.TestMixin".
Спасибо