org.powermock.api.mockito.PowerMockito.mock()方法的使用及代码示例

x33g5p2x  于2022-01-26 转载在 其他  
字(11.3k)|赞(0)|评价(0)|浏览(167)

本文整理了Java中org.powermock.api.mockito.PowerMockito.mock()方法的一些代码示例,展示了PowerMockito.mock()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。PowerMockito.mock()方法的具体详情如下:
包路径:org.powermock.api.mockito.PowerMockito
类名称:PowerMockito
方法名:mock

PowerMockito.mock介绍

[英]Creates a mock object that supports mocking of final and native methods.
[中]创建支持模拟最终方法和本机方法的模拟对象。

代码示例

代码示例来源:origin: facebook/facebook-android-sdk

@Before
public void setup() {
 mockStatic(Utility.class);
 mMockActivity = mock(Activity.class);
 mMockPackageManager = mock(PackageManager.class);
 when(mMockActivity.getPackageManager()).thenReturn(mMockPackageManager);
}

代码示例来源:origin: ankidroid/Anki-Android

private ColorWrap createColorMock(int i) {
  ColorWrap c = mock(ColorWrap.class);
  when(c.getColorValue()).thenReturn(i);
  return c;
}

代码示例来源:origin: Alluxio/alluxio

@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);
}

代码示例来源:origin: checkstyle/checkstyle

@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));
}

代码示例来源:origin: apache/kylin

@Test(expected = CoprocessorException.class)
public void testStart() throws IOException {
  CoprocessorEnvironment env = PowerMockito.mock(RegionServerCoprocessorEnvironment.class);
  CubeVisitService service = new CubeVisitService();
  service.start(env);
}

代码示例来源:origin: checkstyle/checkstyle

@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());
  }
}

代码示例来源:origin: facebook/litho

@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);
}

代码示例来源:origin: julian-klode/dns66

@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 {

代码示例来源:origin: facebook/litho

@Before
public void setup() throws Exception {
 mainService = mock(ExecutorCompletionService.class);
 bgService = mock(ExecutorCompletionService.class);
 mockStatic(ThreadUtils.class);
 mContext = new ComponentContext(new ContextWrapper(RuntimeEnvironment.application));

代码示例来源:origin: Alluxio/alluxio

@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);
}

代码示例来源:origin: checkstyle/checkstyle

@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));
}

代码示例来源:origin: checkstyle/checkstyle

@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());
  }
}

代码示例来源:origin: ankidroid/Anki-Android

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

代码示例来源:origin: Alluxio/alluxio

/**
 * 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);
}

代码示例来源:origin: rackerlabs/blueflood

@BeforeClass
public static void setupClass() {
  // mock logger
  PowerMockito.mockStatic(LoggerFactory.class);
  loggerMock = mock(Logger.class);
  when(LoggerFactory.getLogger(any(Class.class))).thenReturn(loggerMock);
}

代码示例来源:origin: checkstyle/checkstyle

@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());
}

代码示例来源:origin: facebook/litho

@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);
}

代码示例来源:origin: checkstyle/checkstyle

@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();
}

代码示例来源:origin: Alluxio/alluxio

@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);
}

代码示例来源:origin: Alluxio/alluxio

@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);
}

相关文章