I'm using Spring 3.1.0.M2. I'm writing some JUnit 4 tests to test some database functionality, but I'm having trouble wiring up my datasource. In my JUnit class, I have …
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader=AnnotationConfigContextLoader.class,
classes={DataSource.class, WebLeadsDAOImpl.class})
public class TestDB {
...
@Qualifier("mycoSessionFactory")
private SessionFactory sessionFactory;
/**
* Using a single lead lookup should speed up our testing queries.
* <p>
* The lead is is 8104051
* </p>
*/
@Before
public void prep() {
assertNotNull(sessionFactory);
...
}
but the "sessionFactory" object is repeatedly null. Here is how I'm trying to configure it …
@Lazy
@Component
@PropertySource("classpath:oracle.properties")
public class DataSource {
…
@Bean(name="mycoSessionFactory")
public SessionFactory sessionFactory() throws Exception {
final SessionFactory sessionFactory = new AnnotationSessionFactoryBuilder()
.setDataSource(dataSource()开发者_运维问答)
.setHibernateProperties(databaseProperties())
.setPackagesToScan("com.criticalmass.systems.leadsmonitor.domain")
.setSchemaUpdate(false)
.buildSessionFactory();
return sessionFactory;
}
The reason I don't just use "@Autowired" is because I have two SessionFactory beans. Any ideas why my datasource isn't wiring up correctly? Thanks, - Dave
Thanks for your updated suggestion, but the answer turns out to be I had forgotten to add
@Autowired
before the member field. When I did that, everything worked great. -
Looks like you missed to annotate your test class by @ContextConfiguration.
I guess I have it: you missed the annotation that triggers Spring to inject something. Just add:
@Inject
or,@Resource
or,@Autowired
to the variable declaration of sessionFactory in your test case.
精彩评论