Androidtesting:用Mockito进行改造

在我的MainActivity我有一个名为getAPI的方法,它返回一个OTBServiceWrapper 。 这用于设置调用API的改造。

在我的MainActivityTest文件中,我试图将new OTBService().getService()调用getApi方法正在做,所以我可以返回一个MockedOTBService ,将客户端更改为自定义返回json

现在的实现是MockedOTBService如果我不得不在MockedOTBService放置一个MockedOTBServicelogging器,而且还会通过并调用真正的api,这不是我想要的testing。

我正在尝试使用Mockito来存储Retrofit API调用并返回json 。 我似乎无法理解为什么这个存根被调用,但不是存疑的方法。

笔记:

  1. 我正在使用ActivityInstrumentationTestCase2
  2. 我只运行一个testing
  3. 如果我添加一个verify(mockedOTBService, atLeastOnce()).getService(); 是说从来没有被称为。
  4. 如果我更改when...thenReturn返回使用mMainActivity = spy(getActivity()) ,则不会更改,并调用真实的API。

Logcat输出

 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 

真正的stream量

MainActivity.onCreate()> OTBService.getService()> OTBServiceWrapper.createSearch(…)

试图在testing中达到目的

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' } 

Solutions Collecting From Web of "Androidtesting:用Mockito进行改造"

通过允许“活动”设置服务,消除嘲笑您的请求的需要。

在你的MainActivity为服务创build一个类variables和一个类设置器。 它需要在类的范围内,以防止OnCreate方法被调用之前,你已经把服务设置为你想要的。 如果你还没有创build一个实例getter来设置服务。

在您调用getActivity()之前的testing中,将服务设置为您的模拟服务。 (也许想想把它移出到支持对象)。

MainActivity.java

 public class MainActivity extends Activity { private static OTBServiceWrapper serviceWrapper; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getServiceWrapper.createSearch(...) } public OTBServiceWrapper getServiceWrapper() { if (serviceWrapper == null) { MainActivity.setServiceWrapper(new OTBService().getService()); } return serviceWrapper; } public static void setServiceWrapper(OTBServiceWrapper serviceWrapper) { MainActivity.serviceWrapper = serviceWrapper; } } 

MainActivityTest.java

 public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> { private MainActivity mMainActivity; public MainActivityTest() { super(MainActivity.class); } @Override protected void setUp() throws Exception { super.setUp(); setActivityInitialTouchMode(true); MainActivity.setServiceWrapper( new MockedOTBService(getInstrumentation().getContext()).getService() ); mMainActivity = getActivity(); } }