JSF 2.2アプリケーションでは、Seleniumでテストするためのwarファイルを作成したいと考えています。そのwebtest.warでは、NodeCacheと呼ばれる中央クラスを、WebtestNodeCacheというモックバージョンで置き換えて、データベースや他の外部依存関係をテストから守りたいと考えています。テストのためのServletContextリスナーを含むJSF更新管理Bean
NodeCacheが管理Beanです:
@javax.faces.bean.ManagedBean(name = NodeCache.INSTANCE)
@javax.faces.bean.ApplicationScoped
public class NodeCache {
public static final String INSTANE = "nodecache";
// ...
}
WebtestNodeCacheにこっそり、私はこのようなのServletContextListenerを使用します。通常で
public class WebtestContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent event) {
WebtestNodeCache nodeCache = new WebtestNodeCache();
ServletContext context = event.getServletContext();
context.setAttribute(NodeCache.INSTANCE, nodeCache);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {}
}
ビルド、WebtestContextListenerとWebtestNodeCacheは、warファイルから除外されていますテストビルドでは、それらが含まれています。
これはうまくいくようです。ログインすると、WebtestNodeCacheからダミーノードが取得されます。
これは、アプリケーションのコンテキストでBeanを置き換える信頼できる方法ですか、それとも私はただラッキーですか?
テストダミーに潜入するには、より良い方法がありますか?