pivovarit pivovarit - 1 year ago 107
Java Question

How to make Spring ignore an @autowired jmsTemplate field for test purposes?

I created a class that puts mail requests into JMS queue using JMSTemplate.

The problem is that when I run unit tests it throws an exception because it cannot create Sender bean because of the problem with autowiring JMSTemplate bean which is being provided by JBoss Application Server. Of course, it can't autowire it. JBoss is not running when doing unit testing.

So I have created 2 spring context configurations: one for production scope and one for test scope. In test context configuration I have removed the component scan from a package that holds the Sender class. It didn't work because Sender class is being used in some other places so I actually got rid of the JMSTemplate problem and substituted it with another bean creation error.

The Sender bean must be created.
Is there any way Spring could ignore a single autowired field for test purposes?
I'm planning on testing Sender features during integration testing.

I don't think that any code is necessary in this case.
Thank You.

Answer Source

It looks like I managed to solve it myself by using:


on conflicting autowired fields. I hope it helps someone.

However, it might not the be the best idea ever to allow unit testing influence changes on production code so mocking JmsTemplate during testing is probably the desired solution.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download