private void mockDelimitedTextFileWriter(boolean isCompressed) throws Exception { PowerMockito.mockStatic(FileSystem.class); FileSystem fs = Mockito.mock(FileSystem.class); Mockito.when( FileSystem.get(Mockito.any(URI.class), Mockito.any(Configuration.class))).thenReturn(fs); Path fsPath = (!isCompressed) ? new Path(PATH) : new Path(PATH_GZ); GzipCodec codec = PowerMockito.mock(GzipCodec.class); PowerMockito.whenNew(GzipCodec.class).withNoArguments() .thenReturn(codec); FSDataInputStream fileInputStream = Mockito .mock(FSDataInputStream.class); FSDataOutputStream fileOutputStream = Mockito .mock(FSDataOutputStream.class); Mockito.when(fs.open(fsPath)).thenReturn(fileInputStream); Mockito.when(fs.create(fsPath)).thenReturn(fileOutputStream); CompressionInputStream inputStream = Mockito .mock(CompressionInputStream.class); CompressionOutputStream outputStream = Mockito .mock(CompressionOutputStream.class); Mockito.when(codec.createInputStream(Mockito.any(InputStream.class))) .thenReturn(inputStream); Mockito.when(codec.createOutputStream(Mockito.any(OutputStream.class))) .thenReturn(outputStream); }
@Before public void setUp() throws Exception { doReturn( true ).when( repository ).isConnected(); doReturn( null ).when( repository ).getJobEntryAttributeString( any( ObjectId.class ), anyString() ); doReturn( rdi ).when( repository ).loadRepositoryDirectoryTree(); doReturn( directory ).when( rdi ).findDirectory( JOB_ENTRY_FILE_DIRECTORY ); doReturn( directory ).when( rdi ).findDirectory( "/home/admin/folder" ); doReturn( null ).when( space ).environmentSubstitute( anyString() ); doReturn( "" ).when( space ).environmentSubstitute( "" ); doReturn( JOB_ENTRY_FILE_PATH ).when( space ).environmentSubstitute( JOB_ENTRY_FILE_PATH ); doReturn( JOB_ENTRY_FILE_NAME ).when( space ).environmentSubstitute( JOB_ENTRY_FILE_NAME ); doReturn( JOB_ENTRY_FILE_DIRECTORY ).when( space ).environmentSubstitute( JOB_ENTRY_FILE_DIRECTORY ); doReturn( "hdfs://server/path/" ).when( space ).environmentSubstitute( "${hdfs}" ); doReturn( "/home/admin/folder/job.kjb" ).when( space ).environmentSubstitute( "${repositoryfullfilepath}" ); doReturn( "/home/admin/folder/" ).when( space ).environmentSubstitute( "${repositorypath}" ); doReturn( "job.kjb" ).when( space ).environmentSubstitute( "${jobname}" ); doReturn( "job" ).when( space ).environmentSubstitute( "job" ); doCallRealMethod().when( resolver ).normalizeSlashes( anyString() ); doReturn( space ).when( resolver ).resolveCurrentDirectory( any( ObjectLocationSpecificationMethod.class ), any( VariableSpace.class ), any( Repository.class ), any( Job.class ), anyString() ); whenNew( CurrentDirectoryResolver.class ).withNoArguments().thenReturn( resolver ); whenNew( JobMeta.class ).withAnyArguments().thenReturn( mock( JobMeta.class ) ); }
private void setUpPresenter() throws Exception { mockPresenter = mock(TestPresenter.class); PowerMockito.whenNew(Bundle.class).withNoArguments().thenAnswer(new Answer<Bundle>() { @Override public Bundle answer(InvocationOnMock invocation) throws Throwable { return BundleMock.mock(); } }); mockDelegate = mock(PresenterLifecycleDelegate.class); PowerMockito.whenNew(PresenterLifecycleDelegate.class).withAnyArguments().thenReturn(mockDelegate); when(mockDelegate.getPresenter()).thenReturn(mockPresenter); mockFactory = mock(ReflectionPresenterFactory.class); when(mockFactory.createPresenter()).thenReturn(mockPresenter); PowerMockito.mockStatic(ReflectionPresenterFactory.class); when(ReflectionPresenterFactory.fromViewClass(any(Class.class))).thenReturn(mockFactory); }
@Before public void setUp() throws Exception { PowerMockito.whenNew(Bundle.class).withNoArguments().thenAnswer(new Answer<Bundle>() { @Override public Bundle answer(InvocationOnMock invocation) throws Throwable { return BundleMock.mock(); } }); storage = mockStorage(); factory = mock(PresenterFactory.class); when(factory.createPresenter()).thenAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { return mockPresenter(); } }); mockStatic(Parcel.class); Parcel parcel = ParcelMock.mock(); PowerMockito.when(Parcel.obtain()).thenReturn(parcel); }
private void mockTfVersionCommand(final ToolVersion version) throws Exception { final TfVersionCommand command = Mockito.mock(TfVersionCommand.class); PowerMockito.whenNew(TfVersionCommand.class).withNoArguments().thenReturn(command); when(command.runSynchronously()).thenReturn(version); } }
@Test public void handleNotificationWithNoLauncherActivity() throws Exception { /* Override set up mock to return null for launcher activity. */ Intent intent = mock(Intent.class); whenNew(Intent.class).withNoArguments().thenReturn(intent); when(mPackageManagerMock.getLaunchIntentForPackage(anyString())).thenReturn(null); String title = "title"; when(PushIntentUtils.getTitle(any(Intent.class))).thenReturn(title); Map<String, String> customData = new HashMap<>(); customData.put("key", "val"); customData.put("key2", "val2"); when(PushIntentUtils.getCustomData(any(Intent.class))).thenReturn(customData); PushNotifier.handleNotification(mContextMock, new Intent("mock")); verify(mNotificationBuilderMock).setContentTitle(title); verify(mNotificationManagerMock).notify(mDummyGoogleMessageId.hashCode(), mNotificationMock); /* Intent being invalid, we don't put extras in that case, it will never be listened. */ verify(intent, never()).putExtra(anyString(), anyString()); }
@Test public void testConstructor() throws Exception { A a = new A(); PowerMockito.whenNew(A.class).withNoArguments().thenReturn(a); A a2 = A.create(); Assert.assertEquals(a, a2); // PowerMockito.verifyNew(A.class).withNoArguments(); } }
@Test public void testMyMethod() throws Exception { MySut sut = new MySut(); MyCollaborator collaborator = mock(MyCollaborator.class); PowerMockito.whenNew(MyCollaborator.class).withNoArguments().thenReturn(collaborator); when(collaborator.someMethod()).thenReturn(true); assertTrue(sut.myMethod()); } }
@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 coverJSONException() throws Exception { /* Fake JSON exception to cover the checked exception that never happens. */ JSONObject value = mock(JSONObject.class); whenNew(JSONObject.class).withNoArguments().thenReturn(value); when(value.put(anyString(), any())).thenThrow(new JSONException("mock")); CommonSchemaLog commonSchemaLog = new MockCommonSchemaLog(); List<TypedProperty> properties = new ArrayList<>(); StringTypedProperty stringTypedProperty = new StringTypedProperty(); stringTypedProperty.setName("a"); stringTypedProperty.setValue("b"); properties.add(stringTypedProperty); CommonSchemaDataUtils.addCommonSchemaData(properties, commonSchemaLog); assertEquals(0, commonSchemaLog.getData().getProperties().length()); } }
@Test public void handleNotificationWithCustomSound() throws Exception { String customSoundString = "sound"; int resourceId = 2; when(PushIntentUtils.getSound(any(Intent.class))).thenReturn(customSoundString); Resources resourcesMock = mock(Resources.class); when(mContextMock.getResources()).thenReturn(resourcesMock); when(resourcesMock.getIdentifier(eq(customSoundString), eq("raw"), anyString())).thenReturn(resourceId); when(resourcesMock.getResourcePackageName(resourceId)).thenReturn("packageName"); when(resourcesMock.getResourceTypeName(resourceId)).thenReturn("typeName"); when(resourcesMock.getResourceEntryName(resourceId)).thenReturn("entryName"); Uri.Builder uriBuilderMock = mock(Uri.Builder.class); Uri uriMock = mock(Uri.class); whenNew(Uri.Builder.class).withNoArguments().thenReturn(uriBuilderMock); when(uriBuilderMock.scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)).thenReturn(uriBuilderMock); when(uriBuilderMock.authority(resourcesMock.getResourcePackageName(resourceId))).thenReturn(uriBuilderMock); when(uriBuilderMock.appendPath(resourcesMock.getResourceTypeName(resourceId))).thenReturn(uriBuilderMock); when(uriBuilderMock.appendPath(resourcesMock.getResourceEntryName(resourceId))).thenReturn(uriBuilderMock); when(uriBuilderMock.build()).thenReturn(uriMock); PushNotifier.handleNotification(mContextMock, new Intent()); verify(mNotificationBuilderMock).setSound(uriMock); verify(mNotificationManagerMock).notify(mDummyGoogleMessageId.hashCode(), mNotificationMock); }
protected void mockActiveAndroidContext() { PowerMockito.mockStatic(Cache.class); PowerMockito.mockStatic(ContentProvider.class); TableInfo tableInfo = PowerMockito.mock(TableInfo.class); Context context = PowerMockito.mock(Context.class); ContentResolver resolver = PowerMockito.mock(ContentResolver.class); SQLiteDatabase sqliteDb = PowerMockito.mock(SQLiteDatabase.class); ContentValues vals = PowerMockito.mock(ContentValues.class); try { PowerMockito.whenNew(ContentValues.class).withNoArguments().thenReturn(vals); } catch (Exception e) { e.printStackTrace(); } when(Cache.openDatabase()).thenReturn(sqliteDb); when(context.getContentResolver()).thenReturn(resolver); doNothing().when(resolver).notifyChange(any(Uri.class), any(ContentObserver.class)); when(tableInfo.getFields()).thenReturn(new ArrayList<>()); when(tableInfo.getTableName()).thenReturn("TestTable"); when(Cache.getTableInfo(any(Class.class))).thenReturn(tableInfo); when(Cache.getContext()).thenReturn(context); when(ContentProvider.createUri(anyObject(), anyLong())).thenReturn(null); }
services.init(); HCatCredentialHelper hcatCredHelper = Mockito.mock(HCatCredentialHelper.class); PowerMockito.whenNew(HCatCredentialHelper.class).withNoArguments().thenReturn(hcatCredHelper); CredentialsProperties credProps = new CredentialsProperties("", ""); credProps.setProperties(new HashMap<String, String>());
@Test public void testAddToJobConfFromOozieConfig() throws Exception { services.init(); HCatCredentialHelper hcatCredHelper = Mockito.mock(HCatCredentialHelper.class); PowerMockito.whenNew(HCatCredentialHelper.class).withNoArguments().thenReturn(hcatCredHelper); CredentialsProperties credProps = new CredentialsProperties("", ""); HashMap<String, String> prop = new HashMap<String, String>(); prop.put("hcat.metastore.principal", TEST_HIVE_METASTORE_PRINCIPAL2); prop.put("hcat.metastore.uri", TEST_HIVE_METASTORE_URI2); credProps.setProperties(prop); HCatCredentials hcatCred = new HCatCredentials(); final JobConf jobConf = new JobConf(false); Credentials credentials = new Credentials(); PowerMockito.doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { Object[] args = invocation.getArguments(); JobConf jConf = (JobConf) args[1]; jConf.set(HCAT_METASTORE_PRINCIPAL, (String) args[2]); jConf.set(HCAT_METASTORE_URI, (String) args[3]); return null; } }).when(hcatCredHelper).set(credentials, jobConf, TEST_HIVE_METASTORE_PRINCIPAL2, TEST_HIVE_METASTORE_URI2); hcatCred.updateCredentials(credentials, jobConf, credProps, null); assertEquals(TEST_HIVE_METASTORE_PRINCIPAL2, jobConf.get(HCAT_METASTORE_PRINCIPAL)); assertEquals(TEST_HIVE_METASTORE_URI2, jobConf.get(HCAT_METASTORE_URI)); assertNull(jobConf.get(HIVE_METASTORE_PRINCIPAL)); assertNull(jobConf.get(HIVE_METASTORE_URI)); }
whenNew(JSONObject.class).withNoArguments().thenReturn(ticketJson); when(ticketJson.put(anyString(), anyString())).thenThrow(new JSONException("mock"));
@Test public void testAddToJobConfFromHiveConf() throws Exception { services.init(); CredentialsProperties credProps = new CredentialsProperties("", ""); credProps.setProperties(new HashMap<String, String>()); HCatCredentials hcatCred = new HCatCredentials(); final JobConf jobConf = new JobConf(false); Credentials credentials = new Credentials(); HCatCredentialHelper hcatCredHelper = Mockito.mock(HCatCredentialHelper.class); PowerMockito.whenNew(HCatCredentialHelper.class).withNoArguments().thenReturn(hcatCredHelper); PowerMockito.doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { Object[] args = invocation.getArguments(); Configuration jConf = (Configuration) args[1]; jConf.set(HIVE_METASTORE_PRINCIPAL, (String) args[2]); jConf.set(HIVE_METASTORE_URI, (String) args[3]); return null; } }).when(hcatCredHelper).set(credentials, jobConf, TEST_HIVE_METASTORE_PRINCIPAL, TEST_HIVE_METASTORE_URI); hcatCred.updateCredentials(credentials, jobConf, credProps, null); assertEquals(TEST_HIVE_METASTORE_PRINCIPAL, jobConf.get(HIVE_METASTORE_PRINCIPAL)); assertEquals(TEST_HIVE_METASTORE_URI, jobConf.get(HIVE_METASTORE_URI)); assertNull(jobConf.get(HCAT_METASTORE_PRINCIPAL)); assertNull(jobConf.get(HCAT_METASTORE_URI)); }
when(Process.myPid()).thenReturn(123); Date logTimestamp = new Date(1000L); whenNew(Date.class).withNoArguments().thenReturn(logTimestamp); whenNew(Date.class).withArguments(anyLong()).thenAnswer(new Answer<Date>() {
when(Process.myPid()).thenReturn(123); Date logTimestamp = new Date(1000L); whenNew(Date.class).withNoArguments().thenReturn(logTimestamp); whenNew(Date.class).withArguments(anyLong()).thenAnswer(new Answer<Date>() {
whenNew(DisplayMetrics.class).withNoArguments().thenReturn(displayMetricsMock); whenNew(LayoutParams.class).withAnyArguments().thenReturn(paramsMock);
setInternalState(removeViewMock, "button", buttonMock); whenNew(RemoveView.class).withArguments(contextMock).thenReturn(removeViewMock); whenNew(DisplayMetrics.class).withNoArguments().thenReturn(displayMetricsMock); whenNew(WindowManager.LayoutParams.class).withAnyArguments().thenReturn(paramsMock); doReturn(config).when(mockXSpring).getSpringConfig();