gpt4 book ai didi

java - 使用 Robolectric 创建 GoogleApiClient 影子

转载 作者:太空宇宙 更新时间:2023-11-04 13:26:00 29 4
gpt4 key购买 nike

我正在使用 Robolectric 并尝试创建 GoogleApiClient 的 Shadow 对象,但没有成功。 Shadow 类中的方法永远不会被调用。 GoogleApiClient 是接口(interface) - 这会是一个问题吗?有什么方法可以“隐藏”GoogleApiClient 接口(interface)(或在测试中模拟 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;
}

}

我在测试中定义了 Shadow 类:

@Config(shadows = {ShadowGoogleApiClient.class},
constants = BuildConfig.class)
@RunWith(CustomRobolectricRunner.class)
public class ApiTest {
...
}

最佳答案

您是否尝试过使用 Mockito 来模拟 GoogleApiClient 的实现?

doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
myCallback.onConnected();
return null;
}
}).when(mGoogleApiClient).connect();

关于java - 使用 Robolectric 创建 GoogleApiClient 影子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32657197/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com