在我的
在我的
当前实现会命中
我正在尝试使用Mockito存根Retrofit API调用并返回
注意:
MainActivity
中,我有一个名为getAPI
的方法,它返回一个OTBServiceWrapper
。这用于设置Retrofit以调用API。在我的
MainActivityTest
文件中,我试图存根getApi
方法正在进行的new OTBService().getService()
调用,以便我可以返回一个MockedOTBService
,该服务将客户端更改为返回json
的自定义客户端。当前实现会命中
MockedOTBService
,如果我在MockedOTBService
中放置了记录器,但也会继续调用真实的API,这不是我在测试中想要的。我正在尝试使用Mockito存根Retrofit API调用并返回
json
。我似乎无法理解为什么调用存根却没有存根所涉及的方法。注意:
- 我正在使用
ActivityInstrumentationTestCase2
- 我只运行一个测试
- 如果我添加
verify(mockedOTBService, atLeastOnce()).getService();
,则会显示从未调用。 - 如果我更改
when...thenReturn
以使用mMainActivity = spy(getActivity())
,则没有任何更改,并且将调用真实的API。
Logger﹕ MockedOTBService was called // Mock is called
Logger﹕ Real OTBService was called // Real API is called
Logger﹕ MainActivity getAPI method class is "$Proxy1" // Mock is shown in MainActivity
Logger﹕ RealAPIResponse JSON Parsed ID: 266 // Real API response returned
Real Flow
在Real Flow中:
MainActivity.onCreate() > OTBService.getService() > OTBServiceWrapper.createSearch(...)
尝试在测试中实现的目标
在测试中尝试实现的目标是:
MainActivity.onCreate() > MockedOTBService.getService() > OTBServiceWrapper.createSearch(...)
MainActivity.java
public class MainActivity extends Activity {
private OTBServiceWrapper serviceWrapper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getApi().createSearch(...)
}
public OTBServiceWrapper getApi() {
return new OTBService().getService();
}
}
OTBService.java
public class OTBService {
public OTBServiceWrapper getService() {
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(Constants.API_URL)
.build();
return restAdapter.create(OTBServiceWrapper.class);
}
}
OTBServiceWrapper.java
public interface OTBServiceWrapper {
@POST(Constants.API_SEARCHES_POST_URL)
void createSearch(@Body Request request, Callback<Request.Response> callback);
}
MainActivityTest.java
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
private OTBService mMockedOTBService;
private MainActivity mMainActivity;
private View mSearchButton;
public MainActivityTest() { super(MainActivity.class); }
@Override
protected void setUp() throws Exception {
super.setUp();
setActivityInitialTouchMode(true);
System.setProperty("dexmaker.dexcache", getInstrumentation().getTargetContext().getCacheDir().getPath());
mMockedOTBService = mock(OTBService.class);
when(mMockedOTBService.getService()).thenReturn(new MockedOTBService(getInstrumentation().getContext()).getService());
mMainActivity = getActivity();
mSearchButton = mMainActivity.findViewById(R.id.AbSearchButton);
mYourHolidayButton = mMainActivity.findViewById(R.id.AbYourHolidayButton);
}
public void testButtonActions() {
TouchUtils.clickView(this, mSearchButton);
...
}
}
MockedOTBService.java
public class MockedOTBService {
private Context context;
public MockedOTBService(Context context) { this.context = context; }
public OTBServiceWrapper getService() {
RestAdapter restAdapter;
restAdapter = new RestAdapter.Builder()
.setClient(new LocalJsonClient(context))
.setEndpoint(Constants.API_TEST_URL)
.build();
return restAdapter.create(OTBServiceWrapper.class);
}
}
LocalJsonClient.java
@SuppressLint("DefaultLocale")
public class LocalJsonClient implements Client { ... }
build.gradle
dependencies {
androidTestCompile 'com.google.dexmaker:dexmaker:1.0'
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.0'
}