1

I am facing a small challenge on which i do not know how to tackle it. In my current application we are using RabbitMQ as an AMQP broker (using ​spring-rabbit​) and we successfully have written integration-tests where the RabbitMQ ConnectionFactory gets autowired etc.

Now we also have some Unit Tests for a small controller which give me an error when I run them using the command-line grails test-app

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'rabbitMQConfig': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.amqp.rabbit.connection.ConnectionFactory rabbitmq.config.RabbitMQConfig.connectionFactory; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.amqp.rabbit.connection.ConnectionFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

When i ​run the Unit Test inside IntelliJ all works fine!

So i am wondering what the difference is between running the tests inside IntelliJ and the Grails command line. I guess it has something todo with the Environments that Grails runs the test in. But we also have wired up a RabbitMQTestConfig so the integration tests work with the test configuration.

I am a bit puzzled on why this works inside IntelliJ and not on the command-line. Any clue?

Marco
  • 15,101
  • 33
  • 107
  • 174

2 Answers2

0

When you say "run the unit test inside IntelliJ", you mean running the Gradle 'test' task (since you're using Grails 3)?

HypeMK
  • 331
  • 3
  • 9
0

You can try to define your mocked injection with doWithSpring in your spec :

// define the mock or the object instance you want
ConnectionFactory connectionFactory = Mock(ConnectionFactory) 
def doWithSpring = {
  connectionFactory(InstanceFactoryBean, connectionFactory, ConnectionFactory)
}

If it's not the solution you can try to run clean before testing again