私のSpringアプリケーションとjunitの統合テストでは、次のようにテストクラスを配線するためorg.springframework.test.context.ContextLoader
にすでに存在するを使用したいので、サブクラス化しています:XmlWebApplicationContext
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader=MyContextLoader.class)
@Transactional
public class MyTest {
@Autowired
public AccountDao accountDao;
}
私の ContextLoader の実装は次のとおりです。
public class MyContextLoader は ContextLoader を実装します {
@Override
public String[] processLocations(Class<?> clazz, String... locations) {
return locations;
}
@Override
public ApplicationContext loadContext(String... locations) throws Exception {
try {
// Start Embedded Tomcat
EmbeddedTomcat tomcat = new EmbeddedTomcat("mas", 8080);
tomcat.launch();
Context rootContext = tomcat.getRootContext();
ContextLoaderListener contextLoaderListener = (ContextLoaderListener) rootContext.getApplicationLifecycleListeners()[0];
XmlWebApplicationContext context = (XmlWebApplicationContext) contextLoaderListener.getContext();
GenericApplicationContext c = new GenericApplicationContext(context);
AnnotationConfigUtils.registerAnnotationConfigProcessors(c);
//context.refresh();
//context.registerShutdownHook();
return context;
}
catch(Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
メソッドにブレークポイントを設定すると、loadContext(...)
getBean(AccountDao.class) を呼び出すことができ、すべて正常に動作します。ただし、私のテストクラスは実際には自動配線されていないようです。私は少しデバッグし、春のコードをステップ実行しました.AbstractAutowireCapableBeanFactory.populateBean(String beanName, AbstractBeanDefinition mbd, BeanWrapper bw) メソッドで、PropertyValues がクラス Test に設定されていないようです。
たぶん、注釈処理の設定が間違っていますか?
コードへの情報: ご想像のとおり、私は統合テストを行っているため、RESTful Web サービスをテストするために組み込みの tomcat サーバーを開始しています。埋め込み tomcat から「ハック」を使用してアプリケーション コンテキストを取得する方法は、私の投稿に示されています: Getting Access to Spring with Embedded Tomcat 6
返信をお待ちしております。エリック