I'm using Spring 3.1.0.RELEASE and JUnit 4.8.1. I'm having trouble figuring out why a class' member field isn't getting autowired in a JUnit test. My test looks like ...
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "file:src/test/resources/testApplicationContext.xml" })
@TransactionConfiguration(defaultRollback=true)
@Transactional
public abstract class NowYouKnowEventsParserTest {
private EventFeed eventFeed;
@Before
public void setUp() {
eventFeed = getEventFeed(16);
} // setUp
@Test
public void testParser() {
Assert.assertNotSame(0, eventFeed.getEvents().size());
} // testParser
...
@Autowired
private EventFeedsDao eventFeedsDao;
protected EventFeed getEventFeed(final Integer id) {
return eventFeedsDao.findById(id);
} // getEventFeed
}
The class "EventFeed" invokes an instance of the below class ...
package com.myco.myproject.parsers;
...
public abstract class AbstractEventParser {
@Autowired
protected NetUtilsService netUtilsService;
...
}
but when it comes time, the AbstractEventParser's "netUtilsService" member field is null. This is strange because in my "testApplicationContext.xml" file, I have this, which I thought would take care of the autowiring ...
<mvc:annotation-driven />
<context:component-scan base-package="com.myco.myproject" />
How do I force autowiring in my JUnit test? I would prefer not to add and invoke a setter method for the member field but if that is the only way, so be it.