Да, если подкласс RobolectricTestRunner, добавьте настраиваемый пакет в конструктор и загрузите классы Shadow в метод bindShadowClasses. Не нужно использовать андроид. * Трюк пакета.
(Примечание: это с robolectric-1.1)
Есть целый ряд крючков, предусмотренных в RobolectricTestRunner # setupApplicationState, что вы можете переопределить.
Вот моя реализация RobolectricTestRunner.
import org.junit.runners.model.InitializationError;
import com.android.testFramework.shadows.ShadowLoggerConfig;
import com.xtremelabs.robolectric.Robolectric;
import com.xtremelabs.robolectric.RobolectricTestRunner;
public class RoboRunner extends RobolectricTestRunner {
public RoboRunner(Class<?> clazz) throws InitializationError {
super(clazz);
addClassOrPackageToInstrument("package.you're.creating.shadows.of");
}
@Override
protected void bindShadowClasses() {
super.bindShadowClasses(); // as you can see below, you really don't need this
Robolectric.bindShadowClass(ShadowClass.class);
}
}
Более методы, которые вы можете создать подкласс (от RobolectricTestRunner.class)
/**
* Override this method to bind your own shadow classes
*/
protected void bindShadowClasses() {
}
/**
* Override this method to reset the state of static members before each test.
*/
protected void resetStaticState() {
}
/**
* Override this method if you want to provide your own implementation of Application.
* <p/>
* This method attempts to instantiate an application instance as specified by the AndroidManifest.xml.
*
* @return An instance of the Application class specified by the ApplicationManifest.xml or an instance of
* Application if not specified.
*/
protected Application createApplication() {
return new ApplicationResolver(robolectricConfig).resolveApplication();
}
Вот где они называются в Robolectric TestRunner:
public void setupApplicationState(final RobolectricConfig robolectricConfig) {
setupLogging();
ResourceLoader resourceLoader = createResourceLoader(robolectricConfig);
Robolectric.bindDefaultShadowClasses();
bindShadowClasses();
resourceLoader.setLayoutQualifierSearchPath();
Robolectric.resetStaticState();
resetStaticState();
DatabaseConfig.setDatabaseMap(this.databaseMap);//Set static DatabaseMap in DBConfig
Robolectric.application = ShadowApplication.bind(createApplication(), resourceLoader);
}
Благодарим вас за разъяснение. Причина, по которой я хочу скрыть свою активность, заключается в том, что она запускается моим приложением с вызовом 'startActivityForResult (..)'. У меня есть этот код: ShadowActivity shadowActivity = shadowOf (activityA); \t \t Intent startedIntent = shadowActivity.getNextStartedActivity(); \t \t ShadowIntent shadowIntent = shadowOf (initialIntent); \t \t assertThat (shadowIntent.getComponent(). GetClassName(), equalTo (activityB.class.getName())); 'Я хочу получить представления из activityB. Используя собственный API, я использовал «ActivityMonitor», но я хочу знать, как это сделать, используя Robolectric. – kaneda