@Test public void testOnItems() throws Exception { MainActivity activity = spy(MainActivity.class); PowerMockito.whenNew(ArrayAdapter.class).withArguments(activity, R.layout.item).thenReturn(arrayAdapter); activity.onCreate(null); ServerAPI.Item[] items = {new ServerAPI.Item(TEXT)}; activity.onItems(items, ""); InOrder inOrder = inOrder(arrayAdapter); inOrder.verify(arrayAdapter, times(1)).clear(); inOrder.verify(arrayAdapter, times(1)).addAll(items); }
@Test public void testConfigWithIgnoreExceptionalAttributes() throws Exception { // emulate exception from unrelated code, but that is same try-catch final DefaultConfiguration tested = PowerMockito.mock(DefaultConfiguration.class); when(tested.getAttributeNames()).thenReturn(new String[] {"severity"}); when(tested.getName()).thenReturn("MemberName"); when(tested.getAttribute("severity")).thenThrow(CheckstyleException.class); // to void creation of 2 other mocks for now reason, only one moc is used for all cases PowerMockito.whenNew(DefaultConfiguration.class) .withArguments("MemberName", ThreadModeSettings.SINGLE_THREAD_MODE_INSTANCE) .thenReturn(tested); PowerMockito.whenNew(DefaultConfiguration.class) .withArguments("Checker", ThreadModeSettings.SINGLE_THREAD_MODE_INSTANCE) .thenReturn(tested); PowerMockito.whenNew(DefaultConfiguration.class) .withArguments("TreeWalker", ThreadModeSettings.SINGLE_THREAD_MODE_INSTANCE) .thenReturn(tested); try { ConfigurationLoader.loadConfiguration( getPath("InputConfigurationLoaderModuleIgnoreSeverity.xml"), new PropertiesExpander(new Properties()), true); fail("Exception is expected"); } catch (CheckstyleException expected) { assertEquals("Invalid exception cause message", "Problem during accessing 'severity' attribute for MemberName", expected.getCause().getMessage()); } }
.withArguments( anyLong(), anyLong(),
@Before public void before() throws Exception { suppress(methods(BASE_VIEW_CLASS, "onCreate")); suppress(methods(BASE_VIEW_CLASS, "setContentView")); stub(method(BASE_VIEW_CLASS, "getPresenter")).toReturn(mainPresenter); replace(method(BASE_VIEW_CLASS, "findViewById")).with(new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { switch ((int)args[0]) { case R.id.listView: return listView; case R.id.check1: return check1; case R.id.check2: return check2; } return null; } }); activity = spy(MainActivity.class); PowerMockito.whenNew(ArrayAdapter.class).withArguments(activity, R.layout.item).thenReturn(arrayAdapter); }
@Before public void setup() throws Exception{ supplier = Mockito.mock(WXSQLiteOpenHelper.class); listener = Mockito.mock(IWXStorageAdapter.OnResultReceivedListener.class); storage = new DefaultWXStorage(RuntimeEnvironment.application); mockStatic(WXSQLiteOpenHelper.class); PowerMockito.whenNew(WXSQLiteOpenHelper.class) .withArguments(RuntimeEnvironment.application) .thenReturn(supplier); }
private DeepRDD createDeepRDD(SparkContext sc, ExtractorConfig deepJobConfig) throws Exception { DeepRDD deepRDD = mock(DeepRDD.class); whenNew(DeepRDD.class).withArguments(sc, deepJobConfig).thenReturn(deepRDD); return deepRDD; }
private DeepJavaRDD createDeepJAvaRDD(DeepRDD deepRDD) throws Exception { DeepJavaRDD javaRdd = mock(DeepJavaRDD.class); whenNew(DeepJavaRDD.class).withArguments(deepRDD).thenReturn(javaRdd); return javaRdd; }
@NonNull Intent mockInstallIntent() throws Exception { Intent installIntent = mock(Intent.class); whenNew(Intent.class).withArguments(Intent.ACTION_INSTALL_PACKAGE).thenReturn(installIntent); when(installIntent.resolveActivity(any(PackageManager.class))).thenReturn(mock(ComponentName.class)); return installIntent; }
@Test public void resumeAppBeforeStart() throws Exception { Intent clickIntent = mock(Intent.class); when(clickIntent.getAction()).thenReturn(ACTION_NOTIFICATION_CLICKED); Intent startIntent = mock(Intent.class); whenNew(Intent.class).withArguments(mContext, DeepLinkActivity.class).thenReturn(startIntent); new DownloadManagerReceiver().onReceive(mContext, clickIntent); verify(startIntent).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); verify(mContext).startActivity(startIntent); }
@Test public void resumeAfterBeforeStartButBackground() throws Exception { Intent clickIntent = mock(Intent.class); when(clickIntent.getAction()).thenReturn(ACTION_NOTIFICATION_CLICKED); start(); Intent startIntent = mock(Intent.class); whenNew(Intent.class).withArguments(mContext, DeepLinkActivity.class).thenReturn(startIntent); new DownloadManagerReceiver().onReceive(mContext, clickIntent); verify(startIntent).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); verify(mContext).startActivity(startIntent); }
@Before public void setUp() throws Exception { PowerMockito.whenNew(VcsWorkItemsForm.class).withArguments(mockProject).thenReturn(mockForm); PowerMockito.whenNew(VcsWorkItemsModel.class).withArguments(mockProject).thenReturn(modelMock); PowerMockito.whenNew(TabImpl.class).withAnyArguments().thenReturn(mockTab); underTest = new VcsWorkItemsController(mockProject); reset(modelMock); }
@Test public void clickSettingsFailsToNavigate() throws Exception { /* Click settings. */ Intent intent = mock(Intent.class); whenNew(Intent.class).withArguments(Settings.ACTION_SECURITY_SETTINGS).thenReturn(intent); doThrow(new ActivityNotFoundException()).when(mFirstActivity).startActivity(intent); ArgumentCaptor<DialogInterface.OnClickListener> clickListener = ArgumentCaptor.forClass(DialogInterface.OnClickListener.class); verify(mDialogBuilder).setPositiveButton(eq(R.string.appcenter_distribute_unknown_sources_dialog_settings), clickListener.capture()); clickListener.getValue().onClick(mUnknownSourcesDialog, DialogInterface.BUTTON_POSITIVE); when(mUnknownSourcesDialog.isShowing()).thenReturn(false); /* Verify navigation attempted. */ verify(mFirstActivity).startActivity(intent); /* Verify failure is treated as a cancel dialog. */ verifyStatic(); SharedPreferencesManager.remove(PREFERENCE_KEY_DOWNLOAD_STATE); /* Verify no more calls, e.g. happened only once. */ Distribute.getInstance().onActivityPaused(mock(Activity.class)); Distribute.getInstance().onActivityResumed(mock(Activity.class)); verify(mDialog).show(); verify(mUnknownSourcesDialog).show(); }
@Test public void resumeForegroundThenPause() throws Exception { when(SharedPreferencesManager.getString(eq(PREFERENCE_KEY_UPDATE_TOKEN))).thenReturn("mock"); Intent clickIntent = mock(Intent.class); when(clickIntent.getAction()).thenReturn(ACTION_NOTIFICATION_CLICKED); start(); Intent startIntent = mock(Intent.class); whenNew(Intent.class).withArguments(mContext, DeepLinkActivity.class).thenReturn(startIntent); Distribute.getInstance().onActivityResumed(mock(Activity.class)); new DownloadManagerReceiver().onReceive(mContext, clickIntent); verify(mContext, never()).startActivity(startIntent); /* Then pause and test again. */ Distribute.getInstance().onActivityPaused(mock(Activity.class)); new DownloadManagerReceiver().onReceive(mContext, clickIntent); verify(startIntent).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); verify(mContext).startActivity(startIntent); } }
@Test public void successDownloadInstallerNotFoundEvenWithLocalFile() throws Exception { /* Simulate async task. */ waitDownloadTask(); /* Process download completion. */ completeDownload(); Cursor cursor = mockSuccessCursor(); whenNew(Intent.class).withArguments(Intent.ACTION_INSTALL_PACKAGE).thenReturn(mock(Intent.class)); /* Simulate task. */ waitCheckDownloadTask(); /* Check we completed workflow without starting activity because installer not found. */ verify(cursor).close(); verify(mContext, never()).startActivity(any(Intent.class)); verifyStatic(); SharedPreferencesManager.remove(PREFERENCE_KEY_DOWNLOAD_STATE); }
private void testTls1_2Setting(int apiLevel, int tlsSetExpectedCalls) throws Exception { String urlString = "http://mock/logs?api-version=1.0.0"; URL url = mock(URL.class); whenNew(URL.class).withArguments(urlString).thenReturn(url); HttpsURLConnection urlConnection = mock(HttpsURLConnection.class); when(url.openConnection()).thenReturn(urlConnection); DefaultHttpClient httpClient = new DefaultHttpClient(); TestUtils.setInternalState(Build.VERSION.class, "SDK_INT", apiLevel); httpClient.callAsync(urlString, METHOD_POST, new HashMap<String, String>(), null, mock(ServiceCallback.class)); verify(urlConnection, times(tlsSetExpectedCalls)).setSSLSocketFactory(argThat(new ArgumentMatcher<SSLSocketFactory>() { @Override public boolean matches(Object argument) { return argument instanceof TLS1_2SocketFactory; } })); }
@Test public void successDownloadInstallerNotFoundAfterNougat() throws Exception { /* Simulate async task. */ waitDownloadTask(); /* Process download completion. */ completeDownload(); Cursor cursor = mockSuccessCursor(); whenNew(Intent.class).withArguments(Intent.ACTION_INSTALL_PACKAGE).thenReturn(mock(Intent.class)); TestUtils.setInternalState(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.N); /* Simulate task. */ waitCheckDownloadTask(); /* Check we completed workflow without starting activity because installer not found. */ verify(cursor).close(); verify(mContext, never()).startActivity(any(Intent.class)); verifyStatic(); SharedPreferencesManager.remove(PREFERENCE_KEY_DOWNLOAD_STATE); }
@Before public void setUp() throws Exception { PowerMockito.whenNew(VcsPullRequestsForm.class).withNoArguments().thenReturn(mockForm); PowerMockito.whenNew(VcsPullRequestsModel.class).withArguments(mockProject).thenReturn(modelMock); PowerMockito.whenNew(TabImpl.class).withAnyArguments().thenReturn(mockTab); underTest = new VcsPullRequestsController(mockProject); }
@Test public void testEditProxy_Canceled() throws Exception { final MockObserver observer = new MockObserver(manageWorkspacesModel); when(WorkspaceHelper.getProxyServer(server.getName())).thenReturn(StringUtils.EMPTY); when(mockProxySettingsDialog.showAndGet()).thenReturn(false); whenNew(ProxySettingsDialog.class).withArguments(mockProject, server.getName(), StringUtils.EMPTY).thenReturn(mockProxySettingsDialog); manageWorkspacesModel.editProxy(server); observer.assertUpdateNeverOccurred(ManageWorkspacesModel.REFRESH_SERVER); verify(mockProxySettingsDialog, never()).getProxyUri(); verifyStatic(never()); WorkspaceHelper.setProxyServer(server.getName(), "http://testUri.com"); }
@Test public void testEditProxy_Changed() throws Exception { final MockObserver observer = new MockObserver(manageWorkspacesModel); when(WorkspaceHelper.getProxyServer(server.getName())).thenReturn(StringUtils.EMPTY); doNothing().when(WorkspaceHelper.class, "setProxyServer", anyString(), anyString()); when(mockProxySettingsDialog.showAndGet()).thenReturn(true); when(mockProxySettingsDialog.getProxyUri()).thenReturn("http://testUri.com"); whenNew(ProxySettingsDialog.class).withArguments(mockProject, server.getName(), StringUtils.EMPTY).thenReturn(mockProxySettingsDialog); manageWorkspacesModel.editProxy(server); observer.assertAndClearLastUpdate(manageWorkspacesModel, ManageWorkspacesModel.REFRESH_SERVER); verifyStatic(times(1)); WorkspaceHelper.setProxyServer(server.getName(), "http://testUri.com"); }
@Before public void setUp() throws Exception { view = mock(View.class); WeakReference viewWeakReference = mock(WeakReference.class); whenNew(WeakReference.class).withArguments(view).thenReturn(viewWeakReference); doReturn(view).when(viewWeakReference).get(); simpleAnimator = new SimpleAnimator(view, 1); mockStatic(AnimationUtils.class); animation = mock(Animation.class); doReturn(animation).when(AnimationUtils.class, "loadAnimation", null, 1); }