Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

findAll not supported in this implementation of GORM

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

  • How can I test this ? I am on grails 2.2.4

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.

like image 214
birdy Avatar asked Oct 31 '22 21:10

birdy


1 Answers

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.

like image 192
Jeff Scott Brown Avatar answered Nov 16 '22 13:11

Jeff Scott Brown