Spock/Grails - Groovy:unable to resolve class grails.test.mixin.TestFor and unable to resolve class spock.lang.Specification
Asked Answered
M

3

9

I am creating a Spock test for my plugin project-plugin and my main project name is main-project which is using project-plugin as a plugin. So when I am creating a Spock test for my plugin its throwing above error means its warning project-plugin has error because of below class.

Is creating Spock test for plugin will differ to creating Spock test for main project.

For example I have created a new sample project and created spock test for service which is working without any error.

import grails.test.mixin.TestFor
import spock.lang.Specification
@TestFor(DataImportService){

}

I have also tried refreshing dependency with command grails> refresh-dependencies -Dplugins.useInline=true Dependencies refreshed. but still same error.

Should I run my unit test cases with main-project or plugin-project.

How to sort this issue.

Thank you.

Mayo answered 10/12, 2015 at 14:8 Comment(0)
U
3

add to gradle :

testCompile "org.grails:grails-test-mixins:3.3.0"

Source: https://grails-plugins.github.io/grails-test-mixin-plugin/latest/guide/index.html

Untread answered 6/6, 2018 at 9:35 Comment(0)
S
2

I've created numerous Spock tests (unit and integration) for inline-plugins in the applications I work on. Basically the application is a shell and some number of inplace-plugins -- the applications themselves are dirt simple.

Organization is pretty straightforward. BuildConfig.groovy in each app picks the plugins that it is composed of using grails.plugin.location.'plugin-name' = '../../plugins/plugin-name'

+- applications
|     |
|     +- app1
|     +- app2
|     +- app3
|
+- plugins
      |
      +- plugin1 
      +- plugin2 (there are > 30 plugins in total)

Nothing special about creating the tests, either manually, or through IntelliJ IDE. The tests all live in application > test > [integration | unit] folders (yeah, I should refactor in order to get code-coverage to work properly). Works for Domain classes, Controllers, or Services. (Grails 2.4.3 / 2.4.5)

import grails.test.mixin.TestFor
import grails.test.mixin.Mock
import spock.lang.specification
import spock.lang.Unroll

@TestFor(SomeService)
@Mock([OtherService])
class SomeService extends Specification {
    def "some method"() {
        given:
            // setup
        when:
            // action
        then:
            // test outcomes
        cleanup:
            // undo any metaClassing, etc
    }
}
Scratch answered 11/12, 2015 at 3:49 Comment(4)
You mean to say there is no difference in writing Spock test for project or pluginsMayo
I am not getting point when i created a new sample project and wrote spock test which is working fine but my official existing project its giving class resolution errorMayo
What grails version, and what does your BuildConfig look like for the main project? Might be something there.Scratch
I am using 2.4.4 and its same as default with additional plugins requiredMayo
K
1

I had the same error. My Spock unit tests were working until I tried rolling back an update for an unrelated plugin. I'm not sure how, but IntelliJ stopped recognizing any of my project's plugins.

Here is what I think fixed it:

  • In explorer, backup and delete the folder C:\\[my project root]\\work\\projects\\[project name]
  • In IntelliJ IDEA (15.0.4), Build->Rebuild Project
  • Then File->Close Project and reopen the project.

That last step finally got it working. Here is everything else I tried in case that doesn't work by itself:

  • grails clean
  • grails refresh-dependencies
  • Forced svn update to HEAD
  • In IntelliJ, File->Invalidate Caches / Restart...
Koheleth answered 4/3, 2016 at 17:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.