@Before public void setup() { mockStatic(Utility.class); mMockActivity = mock(Activity.class); mMockPackageManager = mock(PackageManager.class); when(mMockActivity.getPackageManager()).thenReturn(mMockPackageManager); }
private ColorWrap createColorMock(int i) { ColorWrap c = mock(ColorWrap.class); when(c.getColorValue()).thenReturn(i); return c; }
@Before public void before() { mMockJobMasterContext = mock(JobMasterContext.class); mMockFileSystemContext = PowerMockito.mock(FileSystemContext.class); when(mMockFileSystemContext.getClientContext()) .thenReturn(ClientContext.create(ServerConfiguration.global())); mMockBlockStore = PowerMockito.mock(AlluxioBlockStore.class); mMockFileSystem = mock(FileSystem.class); mMockUfsManager = mock(UfsManager.class); }
@Test public void testFormatModuleNameContainsCheckSuffix() { final AuditEvent mock = PowerMockito.mock(AuditEvent.class); when(mock.getSourceName()).thenReturn("TestModuleCheck"); when(mock.getSeverityLevel()).thenReturn(SeverityLevel.WARNING); when(mock.getLine()).thenReturn(1); when(mock.getColumn()).thenReturn(1); when(mock.getMessage()).thenReturn("Mocked message."); when(mock.getFileName()).thenReturn("InputMockFile.java"); final AuditEventFormatter formatter = new AuditEventDefaultFormatter(); final String expected = "[WARN] InputMockFile.java:1:1: Mocked message. [TestModule]"; assertEquals("Invalid format", expected, formatter.format(mock)); }
@Test public void testPackagesWithIoExceptionGetResources() throws Exception { final ClassLoader classLoader = mock(ClassLoader.class); when(classLoader.getResources("checkstyle_packages.xml")).thenThrow(IOException.class); try { PackageNamesLoader.getPackageNames(classLoader); fail("CheckstyleException is expected"); } catch (CheckstyleException ex) { assertTrue("Invalid exception cause class", ex.getCause() instanceof IOException); assertEquals("Invalid exception message", "unable to get package file resources", ex.getMessage()); } }
@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); }
@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 {
@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); }
@Test public void testFormatModuleNameDoesNotContainCheckSuffix() { final AuditEvent mock = PowerMockito.mock(AuditEvent.class); when(mock.getSourceName()).thenReturn("TestModule"); when(mock.getSeverityLevel()).thenReturn(SeverityLevel.WARNING); when(mock.getLine()).thenReturn(1); when(mock.getColumn()).thenReturn(1); when(mock.getMessage()).thenReturn("Mocked message."); when(mock.getFileName()).thenReturn("InputMockFile.java"); final AuditEventFormatter formatter = new AuditEventDefaultFormatter(); final String expected = "[WARN] InputMockFile.java:1:1: Mocked message. [TestModule]"; assertEquals("Invalid format", expected, formatter.format(mock)); }
@Test public void testInputStreamFailsOnRead() throws Exception { final InputStream inputStream = PowerMockito.mock(InputStream.class); final int available = Mockito.doThrow(IOException.class).when(inputStream).available(); final URL url = PowerMockito.mock(URL.class); BDDMockito.given(url.openStream()).willReturn(inputStream); final URI uri = PowerMockito.mock(URI.class); BDDMockito.given(uri.toURL()).willReturn(url); try { ImportControlLoader.load(uri); //Using available to bypass 'ignored result' warning fail("exception expected " + available); } catch (CheckstyleException ex) { assertSame("Invalid exception class", SAXParseException.class, ex.getCause().getClass()); } }
public static RectangleWrap createRectangleMock(int width, int height) { RectangleWrap r = mock(RectangleWrap.class); r.width = width; r.height = height; when(r.width()).thenReturn(width); when(r.height()).thenReturn(height); return r; }
/** * Sets up the file system and the context before a test runs. */ @Before public void before() { mClientContext = ClientContext.create(mConf); mFileContext = PowerMockito.mock(FileSystemContext.class); mFileSystem = new DummyAlluxioFileSystem(mFileContext); mFileSystemMasterClient = PowerMockito.mock(FileSystemMasterClient.class); when(mFileContext.acquireMasterClient()).thenReturn(mFileSystemMasterClient); when(mFileContext.getClientContext()).thenReturn(mClientContext); when(mFileContext.getConf()).thenReturn(mConf); }
@BeforeClass public static void setupClass() { // mock logger PowerMockito.mockStatic(LoggerFactory.class); loggerMock = mock(Logger.class); when(LoggerFactory.getLogger(any(Class.class))).thenReturn(loggerMock); }
@Test @SuppressWarnings("unchecked") public void testListFilesNotFile() throws Exception { final Class<?> optionsClass = Class.forName(Main.class.getName()); final Method method = optionsClass.getDeclaredMethod("listFiles", File.class, List.class); method.setAccessible(true); final File fileMock = mock(File.class); when(fileMock.canRead()).thenReturn(true); when(fileMock.isDirectory()).thenReturn(false); when(fileMock.isFile()).thenReturn(false); final List<File> result = (List<File>) method.invoke(null, fileMock, null); assertEquals("Invalid result size", 0, result.size()); }
@Test public void testLayoutSpecMeasureResolveNestedTree() { Component component = setUpSpyComponentForCreateLayout( false /* isMountSpec */, true /* canMeasure */); YogaMeasureFunction measureFunction = getMeasureFunction(component); final int nestedTreeWidth = 20; final int nestedTreeHeight = 25; InternalNode nestedTree = mock(InternalNode.class); when(nestedTree.getWidth()).thenReturn(nestedTreeWidth); when(nestedTree.getHeight()).thenReturn(nestedTreeHeight); when(LayoutState.resolveNestedTree(eq(mContext), eq(mNode), anyInt(), anyInt())) .thenReturn(nestedTree); when(mNode.getContext()).thenReturn(mContext); long output = measureFunction.measure(mNode.mYogaNode, 0, EXACTLY, 0, EXACTLY); PowerMockito.verifyStatic(); LayoutState.resolveNestedTree(eq(mContext), eq(mNode), anyInt(), anyInt()); assertThat(YogaMeasureOutput.getWidth(output)).isEqualTo(nestedTreeWidth); assertThat(YogaMeasureOutput.getHeight(output)).isEqualTo(nestedTreeHeight); }
@Test public void testInputStreamThatFailsOnClose() throws Exception { final InputStream inputStream = PowerMockito.mock(InputStream.class); Mockito.doThrow(IOException.class).when(inputStream).close(); final int available = Mockito.doThrow(IOException.class).when(inputStream).available(); final URL url = PowerMockito.mock(URL.class); BDDMockito.given(url.openStream()).willReturn(inputStream); final URI uri = PowerMockito.mock(URI.class); BDDMockito.given(uri.toURL()).willReturn(url); try { ImportControlLoader.load(uri); //Using available to bypass 'ignored result' warning fail("exception expected " + available); } catch (CheckstyleException ex) { final Throwable[] suppressed = ex.getSuppressed(); assertEquals("Expected one suppressed exception", 1, suppressed.length); assertSame("Invalid exception class", IOException.class, suppressed[0].getClass()); } Mockito.verify(inputStream).close(); }
@Before public void before() throws Exception { mClientContext = ClientContext.create(mConf); mContext = PowerMockito.mock(FileSystemContext.class); mAddress = Mockito.mock(WorkerNetAddress.class); mClient = mock(BlockWorkerClient.class); mRequestObserver = mock(ClientCallStreamObserver.class); PowerMockito.when(mContext.getClientContext()).thenReturn(mClientContext); PowerMockito.when(mContext.getConf()).thenReturn(mConf); PowerMockito.when(mContext.acquireBlockWorkerClient(mAddress)).thenReturn(mClient); PowerMockito.doNothing().when(mContext).releaseBlockWorkerClient(mAddress, mClient); PowerMockito.when(mClient.writeBlock(any(StreamObserver.class))).thenReturn(mRequestObserver); PowerMockito.when(mRequestObserver.isReady()).thenReturn(true); }
@Before public void before() throws Exception { AlluxioConfiguration conf = ConfigurationTestUtils.defaults(); mMockFileSystem = Mockito.mock(FileSystem.class); mMockFileSystemContext = PowerMockito.mock(FileSystemContext.class); when(mMockFileSystemContext.getClientContext()) .thenReturn(ClientContext.create(conf)); when(mMockFileSystemContext.getConf()) .thenReturn(conf); mMockInStream = new MockFileInStream(mMockFileSystemContext, TEST_SOURCE_CONTENTS, conf); when(mMockFileSystem.openFile(new AlluxioURI(TEST_SOURCE))).thenReturn(mMockInStream); mMockOutStream = new MockFileOutStream(mMockFileSystemContext); when(mMockFileSystem.createFile(eq(new AlluxioURI(TEST_DESTINATION)), any(CreateFilePOptions.class))).thenReturn(mMockOutStream); mMockUfsManager = Mockito.mock(UfsManager.class); }