約:
たとえば、ほぼすべての Activity クラスの onCreate メソッドに Crashlytics と Google Analytics のコードがあります。これらの行に遭遇すると、Robolectric はエラーをスローします。ShadowActivity はこの種の問題を解決できますか?
ある種の AnalyticsFacade を実装する Facade 実装内にライブラリの呼び出しをラップし、すべての実装をカプセル化して共通の動作をそれらに委譲するだけのクラスを作成できます。
public interface Analytics{
void sendEvent(String category, String action, Map<String,String> params);
void log(String msg);
}
public class AnalyticsFacade implements Analytics{
private List<Analytics> analyticsServices;
public AnalyticsFacade()
{
this.analyticsServices = new ArrayList<Analytics>();
}
public void addAnalyticsService(Analytics analytics){
analyticsServices.add(analytics);
}
@Override
public void sendEvent(String category, String action, Map<String, String> params)
{
for(Analytics service: analyticsServices){
service.sendEvent(category,action,params);
}
}
@Override
public void log(String msg)
{
for(Analytics service: analyticsServices){
service.log(msg);
}
}
}
public class GoogleAnalytics implements Analytics{
private Tracker tracker;
public GoogleAnalytics(Tracker tracker)
{
this.tracker = tracker;
}
@Override
public void sendEvent(String category, String action, Map<String, String> params)
{
//Note that this method signature has changed since GA v2 , now you pass a Map and
//it has a builder in order to create it
tracker.send(category,action,params);
}
@Override
public void log(String msg)
{
throw new RuntimeException("Not implemented");
}
}