W Grails mam usługę, którą chcę przetestować w jednostce. Usługa wykorzystuje te Import:Grails/Groovy - używanie wielu @TestMixin
import grails.converters.JSON
import grails.web.JSONBuilder
chcę usługę zrobić uzyskać dane i konwertować je do formatu JSON:
def tables = DataProfileStats.withCriteria {
projections {
distinct("tableName")
}
};
Sposób pomocnik pisałem budować JSON jest:
public String buildNodeString(String nodeText)
{
def builder = new JSONBuilder();
JSON result = builder.build {
hello = "world"
};
return result.toString();
}
W teście jednostkowym muszę dodać @TestMixin (ControllerUnitTestMixin), aby załadować adapter JSON. Ale muszę też dodać @TestMixin (DomainClassUnitTestMixin), aby móc sfałszować obiekt bazy danych.
Jakieś pomysły na to, jak mieć wiele @TestMixin lub czy jest to problem z projektowaniem, ponieważ mam import grails.web.JSONBuilder w klasie serwisowej? W przeciwnym razie muszę użyć biblioteki JAVA/JSON lub umieścić rzeczy JSON w kontrolerze.
To jest to, co chcę testy wyglądać:
@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 != "";
}
}
dostaję @TestMixin (ControllerUnitTestMixin) @TestMixin (DomainClassUnitTestMixin) klasy JsTreeJSONGeneratorServiceTests {
void testSomething() {
DataProfileStats stats1 = new DataProfileStats();
stats1.tableName = "table";
mockDomain(DataProfileStats, stats1);
JsTreeJSONGeneratorService service = new JsTreeJSONGeneratorService();
String json = service.buildNodeString();
assert json != "";
}
}
dostaję @TestMixin (ControllerUnitTestMixin) @TestMixin (DomainClassUnitTestMixin) klasy JsTreeJSONGeneratorServiceTests {
void testSomething() {
DataProfileStats stats1 = new DataProfileStats();
stats1.tableName = "table";
mockDomain(DataProfileStats, stats1);
JsTreeJSONGeneratorService service = new JsTreeJSONGeneratorService();
String json = service.buildNodeString();
assert json != "";
}
}
dostaję „Nie można określić duplikat adnotacji na tego samego członka: grails.test.mixin. TestMixin "wyjątek.
Dzięki
Gratulujemy rozwiązania. Jeśli jesteś w stanie, zaznacz swoją odpowiedź jako "zaakceptowaną", aby inni mogli uczyć się od ciebie sukcesu. Pozdrowienia ~ –
Dziękuję - to mi pomogło (korzystałem z @Mock()). – duma