0

I'm creating a plugin in Grails 5 and Java 11 to perform processes in a static class, but when running the test described below, the following error message appears:

  • Error:
java.lang.IllegalArgumentException: GrailsApplication not found
    at org.springframework.util.Assert.notNull(Assert.java:201)
    at grails.util.Holders.getGrailsApplication(Holders.java:129)
    at grails.util.Holders$getGrailsApplication.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:130)
  • Example scenario:
---
configuration.value: "ABC"

import grails.util.Holders

class SimpleClass {

    static String simpleMethod() {
        return Holders.getGrailsApplication().getConfig().getProperty("configuration.value")
    }

}
import spock.lang.Specification

class SomeTest extends Specification {

    void "example"() {
        expect:
        SimpleClass.simpleMethod() == "ABC"
    }

}
  • There is no good reason to do `Holders.getGrailsApplication().getConfig().getProperty("configuration.value")`. Knowing the context from where that is happening would be helpful. Is that in a Spring managed bean? – Jeff Scott Brown Sep 29 '22 at 19:41
  • I'm creating a plugin to contain the final project's encryption rules. And the idea is that the encryption key is in the application.yml or even a path to it. That's the reason for getProperty("configuration.value"). This works if I just run it, however if I run the unit test it doesn't. – João G. Hartmann Sep 29 '22 at 20:04
  • If you put that logic in a container managed bean you can simply inject the GrallsApplication into that bean. – Jeff Scott Brown Sep 29 '22 at 21:11
  • One of the requirements is not to use a bean for this, but a class with a static method. – João G. Hartmann Sep 30 '22 at 12:26
  • "One of the requirements is not to use a bean for this, but a class with a static method." - Requiring a static method is a peculiar requirement but can definitely be done. Will this static method be invoked during request processing? – Jeff Scott Brown Sep 30 '22 at 13:23
  • Yes, the method must be accessible from anywhere. – João G. Hartmann Sep 30 '22 at 13:32
  • "Yes, the method must be accessible from anywhere." - If you care to create a small project with a representative failing test, I would be happy to send a PR to address the test. – Jeff Scott Brown Sep 30 '22 at 22:10
  • Sorry for the delay, here's the repository link: https://github.com/JoaoGH/simple-plugin – João G. Hartmann Oct 05 '22 at 20:56
  • are you wanting to test the logic in the `RSA` class and mock its dependencies? – Jeff Scott Brown Oct 06 '22 at 04:38
  • Your test is a unit test. There is nothing in the environment that is creating an instance of GrailsApplication, but your test expects one to be there. It isn't clear what the unit test intends to cover. If you want to register a `GrailsApplication` with `Holders.setGrailsApplication(anInstanceOfDefaultGrailsApplicationYouCreateAndInitialize)`. You could also refactor `RSA` to make it easier to test the logic in it. – Jeff Scott Brown Oct 06 '22 at 04:45
  • The reason for testing is just to make sure there are no problems encrypting and decrypting. – João G. Hartmann Oct 06 '22 at 11:31
  • If it's easier to use the integration test I'll change it no problem. Could you do a PR with your point? – João G. Hartmann Oct 06 '22 at 11:33
  • "If it's easier to use the integration test I'll change it no problem. Could you do a PR with your point?" - It is easier, but not what I would recommend. I would refactor the RSA class. The PR https://github.com/JoaoGH/simple-plugin/pull/1 does show that it is easy to use an integration test. – Jeff Scott Brown Oct 06 '22 at 13:14
  • thanks, it worked. I didn't know I needed that dependency. – João G. Hartmann Oct 06 '22 at 16:14
  • "I didn't know I needed that dependency." - Strictly speaking you don't, but adding it just like that is a simple way to make the test you wrote work just by moving the test and adding an annotation to it. – Jeff Scott Brown Oct 06 '22 at 16:19
  • https://github.com/JoaoGH/simple-plugin/pull/2 represents a way to approach it as a unit test. – Jeff Scott Brown Oct 06 '22 at 21:25

0 Answers0