0

My goal is to write test for my own Grails plugin. steps taken so far:

  1. Step - Create separate grails app (in our case, testApp) inside the Grails Plugin 'test' folder
  2. Step - Make the plugin to be inline for the application - testApp by adding following Line in BuildConfig.groovy

    grails.plugin.location.PluginName = '../../../../PluginName'

The issue is that in the Unit test, the Service (from the inline plugin) are not injected like it is for regular app in Unit Test. Here is sample test:

@TestFor(SampleService)
class SampleServiceUnitSpec extends UnitSpec {
    def "Sample Test"(){
        setup:
           def test = 'ok'

        when:
           test = "changed"

        then:
           assert service
           assert "changed" == test
    }
}

The service is injected if this test is run from grails app and service is part of the app. The service is not injected if this test is run from the testApp with inline plugin containing the service. The service is not injected if the test is plugin unit test and the service declared in the plugin.

How to inject service so it can be tested? Any good docs,posts.etc on how best test Grails plugins? Thank You

latvian
  • 3,161
  • 9
  • 33
  • 62

1 Answers1

0

Why you want to create a unit test for your plugin service inside an application. Shouldn't be better your application just worry with his own code?

I suggest you to separate your concerns: plugin classes should be tested in the plugin, and application classes in the application. For the point of view fo the application, assume that your services "should just work", because they are well tested in the plugin.

Also, I advise against creating an application inside your Grails plugin. The test folder is used only for your test classes and nothing more.

If you want to maintain only the last version of your plugin, you can create just one application (in another folder, outside your plugin) and always update there.

If you need to maintain more than one version, or more than one Grails version, then maybe a Groovy script that create your test application with an specified version will be a better approach.

  • Thank You Sergio. This plugin is used for multiple applications. By putting test/project/app1, i can test the dependencies(integration) between plugin and the apps while keep it in one place - at the plugin – latvian May 30 '13 at 13:56