Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GrailsUnitTest.defineBeans(new PluginClass()) does not set grailsApplication /applicationContext? #43

Open
boardbloke opened this issue Sep 13, 2018 · 1 comment

Comments

@boardbloke
Copy link

boardbloke commented Sep 13, 2018

I have a custom TagLib that makes use of the asset-pipeline-grails (version 3.0.6) AssetsTagLib to display images. It calls asset.image() to output an image tag when it renders.

In order to test this, I have a CustomTagLibSpec that implements TagLibUnitTest, and in the setup method I was expecting to be able to do the following to bootstrap and mock the the relevant asset dependencies:

setup() {`
  defineBeans(new AssetPipelineGrailsPlugin())
  mockTagLib(AssetsTagLib)
  mockTagLib(AssetMethodTagLib)
}

On running the unit test though, I get a failure because grailsApplication is used in the AssetPipelineGrailsPlugin.doWithSpring Closure, and that is not set.

If I change my unit test code to

setup() {`
  def assetPlugin = new AssetPipelineGrailsPlugin()
  assetPlugin.applicationContext = applicationContext
  assetPlugin.grailsApplicatin = grailsApplication
  defineBeans(assetPlugin)
  mockTagLib(AssetsTagLib)
  mockTagLib(AssetMethodTagLib)
}

Then my unit test works.

Given that all plugins implement Plugin, and therefore ApplicationContextAware, and GrailsApplicationAware, can the defineBeans(Object plugin) method in GrailsUnitTest be changed to set the grailsApplication and applicationContext from the unit test to avoid having to do it manually please ?

@boardbloke boardbloke changed the title defineBeans(new PluginClass()) does not set grailsApplication /applicationContext? GrailsUnitTest.defineBeans(new PluginClass()) does not set grailsApplication /applicationContext? Sep 13, 2018
@musketyr
Copy link

I believe this is the same root cause as for https://github.com/agorapulse/dru/issues/15. applicationContext is null inside doWithSpring closure.

	at whatever.HelloConstollerSpec.basic test(HelloControllerSpec.groovy:17)
Caused by: java.lang.NullPointerException: Cannot get property 'classLoader' on null object
	at grails.plugin.json.view.JsonViewGrailsPlugin.doWithSpring_closure1(JsonViewGrailsPlugin.groovy:53)
	at groovy.lang.Closure.call(Closure.java:405)
	at groovy.lang.Closure.call(Closure.java:399)
	at grails.spring.BeanBuilder.invokeBeanDefiningClosure(BeanBuilder.java:759)
	at grails.spring.BeanBuilder.beans(BeanBuilder.java:588)
	at org.grails.testing.GrailsUnitTest$Trait$Helper.defineBeans(GrailsUnitTest.groovy:97)
	at org.grails.testing.GrailsUnitTest$Trait$Helper.defineBeans(GrailsUnitTest.groovy:108)
	at com.agorapulse.gru.grails.minions.jsonview.JsonViewRendererMinion.setupJsonViewsPlugin(JsonViewRendererMinion.groovy:80)
	at com.agorapulse.gru.grails.minions.jsonview.JsonViewRendererMinion.doBeforeRun(JsonViewRendererMinion.groovy:47)
	at com.agorapulse.gru.minions.AbstractMinion.beforeRun(AbstractMinion.java:24)
	at com.agorapulse.gru.Squad.beforeRun(Squad.java:125)
	at com.agorapulse.gru.Gru.test(Gru.java:135)
	at com.agorapulse.gru.Gru.test(Gru.java:105)
	... 1 more

see https://github.com/mortensoby/gru-grails4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants