I'm trying to test a method in my service but I keep getting error that
String-based queries like [findAll] are currently not supported in this implementation of GORM. Use criteria instead.
My service:
class MyService {
List<Color> colorShade (String shade) {
def shadeId = Shade.findByName(shade).id
return ColorShade.get(shadeId)
}
}
My class that joins Shade
and Color
domains
class ColorShade extends Serializable {
Color color
Shade shade
static ColorShade create (Color color, Shade shade, boolean flush = false) {
if (get(shade.id) == null)
new ColorNetwork(color: color, shade: shade).save(flush: flush)
}
static ColorShade get (long shadeId) {
ColorShade.findAll 'from ColorShade where shade.id = :shadeId',
[shadeId: shadeId]
}
static mapping = {
table 'color_shade'
version false
id composite: ['color', 'shade']
}
}
My Spec: test\unit\MyServiceSpec.groovy
I've also tried adding it to test\integration\MyServiceSpec.groovy
@TestFor(MyServiceSpec)
@Mock([Color, Shade, ColorNetwork])
@TestMixin(DomainClassUnitTestMixin)
class MyServiceSpec extends Specification {
def shade
def color
def setup(){
color = new Color(name: "red")
shade = new Shade(name: "dark")
color.save(flush: true)
shade.save(flush: true)
ColorShade.create(color, shade)
/*
I've also tried:
mockDomain(ColorShade, [[color: color, shade: shade]])
but I get the same error
*/
}
def cleanup () {
}
def "test colorShade" () {
expect:
1 == service.colorShade("dark").size()
}
}
When I run this test I get an error:
String-based queries like [findAll] are currently not supported in this implementation of GORM. Use criteria instead.
Question
Note: This is just a sample test scenario I've created for purpose of the question. My actual tests are different but in them I have the same problem as this question.
If you are using version 4.3.5.4 or later of the hibernate4 plugin then you can use the grails.test.mixin.hibernate.HibernateTestMixin
, which supports a full Hibernate GORM in your unit test environment. You need to add that as a dependency with something like this in grails-app/conf/BuildConfig.groovy
.
dependencies {
test "org.grails:grails-datastore-test-support:1.0-grails-2.4"
}
Then annotate your test with the grails.test.mixin.hibernate.HibernateTestMixin
annotation.
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