1
我正在使用Robolectric并尝试创建GoogleApiClient的Shadow对象,但未成功。来自Shadow类的方法永远不会被调用。 GoogleApiClient是界面 - 这可能是一个问题吗?有没有办法如何“隐藏”GoogleApiClient界面(或者在测试中模拟GoogleApiClient)?使用Robolectric创建GoogleApiClient阴影
实现:
@Implements(GoogleApiClient.class)
public class ShadowGoogleApiClient {
public void __constructor__(){
System.out.println("__constructor____constructor__");
}
@Implementation
void connect() {
System.out.println("connectconnectconnect");
}
@Implementation
boolean isConnected() {
System.out.println("isConnectedisConnected");
return false;
}
@Implementation
boolean isConnecting() {
System.out.println("isConnectingisConnecting");
return false;
}
}
,我已经在我的测试中定义的暗影类:
@Config(shadows = {ShadowGoogleApiClient.class},
constants = BuildConfig.class)
@RunWith(CustomRobolectricRunner.class)
public class ApiTest {
...
}
您必须注册您的班级以进行遮蔽。请参阅http://stackoverflow.com/questions/29629786/robolectric-shadow-not-working – nenick
实际上,它仍然无法正常工作。我将类(GoogleApiClient)添加到了InstrumentationConfiguration.Builder中,但没有任何更改。 – Zis
GoogleApiClient是一个界面。不知道,但你只能影响具体的课程是有道理的。调试应用程序时应显示真正的实现类。你可以使用@Implements中的className属性来影射无法访问的类 – nenick