private void mockUserGroupInformation(String username) throws IOException { // need to mock out since FileSystem.create calls UGI, which occasionally has issues on some // systems PowerMockito.mockStatic(UserGroupInformation.class); final UserGroupInformation ugi = mock(UserGroupInformation.class); when(UserGroupInformation.getCurrentUser()).thenReturn(ugi); when(ugi.getUserName()).thenReturn(username); when(ugi.getShortUserName()).thenReturn(username.split("@")[0]); }
private void setupShellMocks(String username, List<String> groups) throws IOException { PowerMockito.mockStatic(CommonUtils.class); PowerMockito.when(CommonUtils.getUnixGroups(eq(username))).thenReturn(groups); }
@Before public void setup() { mockStatic(Utility.class); mMockActivity = mock(Activity.class); mMockPackageManager = mock(PackageManager.class); when(mMockActivity.getPackageManager()).thenReturn(mMockPackageManager); }
@Test public void run() throws Exception { JobCoordinator coordinator = PowerMockito.mock(JobCoordinator.class); PowerMockito.mockStatic(JobCoordinator.class); Mockito.when( JobCoordinator.create(Mockito.any(CommandManager.class), Mockito.any(UfsManager.class), Mockito.anyList(), Mockito.anyLong(), Mockito.any(JobConfig.class), Mockito.any(null))) .thenReturn(coordinator); TestJobConfig jobConfig = new TestJobConfig("/test"); for (long i = 0; i < TEST_JOB_MASTER_JOB_CAPACITY; i++) { mJobMaster.run(jobConfig); } Assert.assertEquals(TEST_JOB_MASTER_JOB_CAPACITY, mJobMaster.list().size()); }
@Before public void setUp() { mockStatic(Log.class); service = mock(AdVpnService.class); thread = new AdVpnThread(service, null); builder = mock(VpnService.Builder.class); when(builder.addDnsServer(anyString())).thenAnswer(new Answer<VpnService.Builder>() { @Override public VpnService.Builder answer(InvocationOnMock invocation) throws Throwable { when(builder.addDnsServer(any(InetAddress.class))).thenAnswer(new Answer<VpnService.Builder>() { @Override public VpnService.Builder answer(InvocationOnMock invocation) throws Throwable {
@Test public void testExceptionNoSuchAlgorithmException() throws Exception { final Configuration config = new DefaultConfiguration("myName"); final String filePath = temporaryFolder.newFile().getPath(); final PropertyCacheFile cache = new PropertyCacheFile(config, filePath); cache.put("myFile", 1); mockStatic(MessageDigest.class); when(MessageDigest.getInstance("SHA-1")) .thenThrow(NoSuchAlgorithmException.class); final Class<?>[] param = new Class<?>[1]; param[0] = Serializable.class; final Method method = PropertyCacheFile.class.getDeclaredMethod("getHashCodeBasedOnObjectContent", param); method.setAccessible(true); try { method.invoke(cache, config); fail("InvocationTargetException is expected"); } catch (InvocationTargetException ex) { assertTrue("Invalid exception cause", ex.getCause().getCause() instanceof NoSuchAlgorithmException); assertEquals("Invalid exception message", "Unable to calculate hashcode.", ex.getCause().getMessage()); } }
@Before public void setup() throws Exception { mainService = mock(ExecutorCompletionService.class); bgService = mock(ExecutorCompletionService.class); mockStatic(ThreadUtils.class); mContext = new ComponentContext(new ContextWrapper(RuntimeEnvironment.application));
@Before public void before() { mMockJobMasterContext = Mockito.mock(JobMasterContext.class); mMockFileSystem = Mockito.mock(FileSystem.class); mMockFileSystemContext = PowerMockito.mock(FileSystemContext.class); mMockBlockStore = PowerMockito.mock(AlluxioBlockStore.class); PowerMockito.mockStatic(AlluxioBlockStore.class); PowerMockito.when(AlluxioBlockStore.create(mMockFileSystemContext)).thenReturn(mMockBlockStore); }
private void mockCustomTabRedirectActivity(final boolean hasActivity) { mockStatic(Validate.class); when(Validate.hasCustomTabRedirectActivity(any(Context.class))).thenReturn(hasActivity); }
protected void setupProperties() { connection = mock(Connection.class); dbmd = mock(DatabaseMetaData.class); PowerMockito.mockStatic(SqlUtil.class); when(SqlUtil.getConnection(dbConnConf)).thenReturn(connection); }
@Before public void setUp() { mDiffNode = mock(DiffNode.class); mNode = mock(InternalNode.class); final YogaNode cssNode = new YogaNode(); cssNode.setData(mNode); mNode.mYogaNode = cssNode; mockStatic(ComponentsPools.class); when(mNode.getLastWidthSpec()).thenReturn(-1); when(mNode.getDiffNode()).thenReturn(mDiffNode); when(mDiffNode.getLastMeasuredWidth()).thenReturn(-1f); when(mDiffNode.getLastMeasuredHeight()).thenReturn(-1f); when(ComponentsPools.acquireInternalNode(any(ComponentContext.class))).thenReturn(mNode); when(ComponentsPools.acquireComponentTreeBuilder( any(ComponentContext.class), any(Component.class))) .thenCallRealMethod(); mockStatic(LayoutState.class); StateHandler stateHandler = mock(StateHandler.class); mContext = spy(new ComponentContext(RuntimeEnvironment.application, stateHandler)); mNestedTreeWidthSpec = SizeSpec.makeSizeSpec(400, SizeSpec.EXACTLY); mNestedTreeHeightSpec = SizeSpec.makeSizeSpec(200, SizeSpec.EXACTLY); }
@Test @PrepareForTest(Log.class) public void testCloseOrWarn_closeable() throws Exception { Closeable closeable = mock(Closeable.class); mockStatic(Log.class); when(Log.e(anyString(), anyString(), any(Throwable.class))).then(new CountingAnswer(null)); // Closing null should work just fine testResult = 0; assertNull(FileHelper.closeOrWarn((Closeable) null, "tag", "msg")); assertEquals(0, testResult); // Successfully closing the file should not log. testResult = 0; assertNull(FileHelper.closeOrWarn(closeable, "tag", "msg")); assertEquals(0, testResult); // If closing fails, it should log. when(closeable).thenThrow(new IOException("Foobar")); testResult = 0; assertNull(FileHelper.closeOrWarn(closeable, "tag", "msg")); assertEquals(1, testResult); }
mockStatic(CommonUtil.class); final CheckstyleException mockException = new CheckstyleException("Cannot get URL for cache file " + cacheFile.getAbsolutePath()); when(CommonUtil.getUriByFilename(any(String.class))) .thenThrow(mockException);
@Before public void before() throws Exception { mMockFileSystemContext = PowerMockito.mock(FileSystemContext.class); mMockBlockStore = PowerMockito.mock(AlluxioBlockStore.class); mMockFileSystem = Mockito.mock(FileSystem.class); mMockUfsManager = Mockito.mock(UfsManager.class); PowerMockito.mockStatic(AlluxioBlockStore.class); PowerMockito.when(AlluxioBlockStore.create(mMockFileSystemContext)).thenReturn(mMockBlockStore); when(mMockBlockStore.getAllWorkers()).thenReturn(BLOCK_WORKERS); createDirectory("/"); }
private ReferenceCountHelperImpl prepareInstance() { ReferenceCountHelperImpl rchi = mock(ReferenceCountHelperImpl.class); PowerMockito.mockStatic(ReferenceCountHelper.class, Mockito.CALLS_REAL_METHODS); PowerMockito.when(ReferenceCountHelper.getInstance()).thenReturn(rchi); return rchi; }
@Before public void setup() throws Exception { PowerMockito.mockStatic( SSHData.class ); PowerMockito.mockStatic( KettleVFS.class ); when( SSHData.createConnection( server, port ) ).thenReturn( connection ); when( SSHData.OpenConnection( any(), anyInt(), any(), any(), anyBoolean(), any(), any(), anyInt(), anyObject(), any(), anyInt(), any(), any() ) ).thenCallRealMethod(); when( KettleVFS.getFileObject( keyFilePath ) ).thenReturn( fileObject ); }
@Test public void flowControl() throws Exception { JobCoordinator coordinator = PowerMockito.mock(JobCoordinator.class); PowerMockito.mockStatic(JobCoordinator.class); Mockito.when( JobCoordinator.create(Mockito.any(CommandManager.class), Mockito.any(UfsManager.class), Mockito.anyList(), Mockito.anyLong(), Mockito.any(JobConfig.class), Mockito.any(null))) .thenReturn(coordinator); TestJobConfig jobConfig = new TestJobConfig("/test"); for (long i = 0; i < TEST_JOB_MASTER_JOB_CAPACITY; i++) { mJobMaster.run(jobConfig); } try { mJobMaster.run(jobConfig); Assert.fail("should not be able to run more jobs than job master capacity"); } catch (ResourceExhaustedException e) { Assert.assertEquals(ExceptionMessage.JOB_MASTER_FULL_CAPACITY .getMessage(ServerConfiguration.get(PropertyKey.JOB_MASTER_JOB_CAPACITY)), e.getMessage()); } }
@Test @PrepareForTest({Log.class, Os.class}) public void testCloseOrWarn_fileDescriptor() throws Exception { FileDescriptor fd = mock(FileDescriptor.class); mockStatic(Log.class); mockStatic(Os.class); when(Log.e(anyString(), anyString(), any(Throwable.class))).then(new CountingAnswer(null)); // Closing null should work just fine testResult = 0; assertNull(FileHelper.closeOrWarn((FileDescriptor) null, "tag", "msg")); assertEquals(0, testResult); // Successfully closing the file should not log. testResult = 0; assertNull(FileHelper.closeOrWarn(fd, "tag", "msg")); assertEquals(0, testResult); // If closing fails, it should log. testResult = 0; doThrow(new ErrnoException("close", 0)).when(Os.class, "close", any(FileDescriptor.class)); assertNull(FileHelper.closeOrWarn(fd, "tag", "msg")); assertEquals(1, testResult); }
mockStatic(CommonUtil.class); final CheckstyleException mockException = new CheckstyleException("Exception #" + i); when(CommonUtil.getUriByFilename(any(String.class))) .thenThrow(mockException);
private void setUpPresenter() throws Exception { mockPresenter = mock(TestPresenter.class); 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); }