org.powermock.api.mockito.PowerMockito类的使用及代码示例

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

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

PowerMockito介绍

[英]PowerMockito extends Mockito functionality with several new features such as mocking static and private methods and more. Use PowerMock instead of Mockito where applicable.
[中]PowerMockito通过几个新特性扩展了Mockito功能,比如模拟静态和私有方法等等。如果适用,请使用PowerMock而不是Mockito。

代码示例

代码示例来源: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: pentaho/pentaho-kettle

@Test
@PrepareForTest( { Encode.class } )
public void testCleanupTransServletEscapesHtmlWhenTransNotFound() throws ServletException, IOException {
 HttpServletRequest mockHttpServletRequest = mock( HttpServletRequest.class );
 HttpServletResponse mockHttpServletResponse = mock( HttpServletResponse.class );
 StringWriter out = new StringWriter();
 PrintWriter printWriter = new PrintWriter( out );
 PowerMockito.spy( Encode.class );
 when( mockHttpServletRequest.getContextPath() ).thenReturn( CleanupTransServlet.CONTEXT_PATH );
 when( mockHttpServletRequest.getParameter( anyString() ) ).thenReturn( ServletTestUtils.BAD_STRING_TO_TEST );
 when( mockHttpServletResponse.getWriter() ).thenReturn( printWriter );
 cleanupTransServlet.doGet( mockHttpServletRequest, mockHttpServletResponse );
 assertFalse( ServletTestUtils.hasBadText( ServletTestUtils.getInsideOfTag( "H1", out.toString() ) ) );
 PowerMockito.verifyStatic( atLeastOnce() );
 Encode.forHtml( anyString() );
}

代码示例来源:origin: konmik/nucleus

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

代码示例来源:origin: spring-projects/spring-security

public final void currentSecurityContextPowermockSetup() throws Exception {
  spy(DelegatingSecurityContextCallable.class);
  doReturn(wrappedCallable).when(DelegatingSecurityContextCallable.class, "create",
      callable, null);
  spy(DelegatingSecurityContextRunnable.class);
  doReturn(wrappedRunnable).when(DelegatingSecurityContextRunnable.class, "create",
      runnable, null);
}

代码示例来源:origin: spring-projects/spring-security

public final void explicitSecurityContextPowermockSetup() throws Exception {
  spy(DelegatingSecurityContextCallable.class);
  doReturn(wrappedCallable).when(DelegatingSecurityContextCallable.class, "create",
      eq(callable), securityContextCaptor.capture());
  spy(DelegatingSecurityContextRunnable.class);
  doReturn(wrappedRunnable).when(DelegatingSecurityContextRunnable.class, "create",
      eq(runnable), securityContextCaptor.capture());
}

代码示例来源:origin: pentaho/pentaho-kettle

@Before
public void setup() throws KettlePluginException {
 mockSpace = mock( VariableSpace.class );
 doReturn("N" ).when( mockSpace ).getVariable( any(), anyString() );
 rowMeta = spy( new RowMeta() );
 memoryGroupByMeta = spy( new MemoryGroupByMeta() );
 mockStatic( ValueMetaFactory.class );
 when( ValueMetaFactory.createValueMeta( anyInt() ) ).thenCallRealMethod();
 when( ValueMetaFactory.createValueMeta( anyString(), anyInt() ) ).thenCallRealMethod();
 when( ValueMetaFactory.createValueMeta( "maxDate", 3, -1, -1 ) ).thenReturn( new ValueMetaDate( "maxDate" ) );
 when( ValueMetaFactory.createValueMeta( "minDate", 3, -1, -1 ) ).thenReturn( new ValueMetaDate( "minDate" ) );
 when( ValueMetaFactory.createValueMeta( "countDate", 5, -1, -1 ) ).thenReturn( new ValueMetaInteger( "countDate" ) );
 when( ValueMetaFactory.getValueMetaName( 3 ) ).thenReturn( "Date" );
 when( ValueMetaFactory.getValueMetaName( 5 ) ).thenReturn( "Integer" );
}

代码示例来源: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: pentaho/pentaho-kettle

@Test
public void testRemoveChannelFromBufferCallInGetLogBufferInFirstJobExecution() {
 StringBuffer sb = new StringBuffer( "" );
 LoggingBuffer lb = mock( LoggingBuffer.class );
 doReturn( sb ).when( lb ).getBuffer( anyString(), anyBoolean() );
 mockStatic( KettleLogStore.class );
 mockStatic( Utils.class );
 mockStatic( Const.class );
 when( KettleLogStore.getAppender() ).thenReturn( lb );
 BaseLogTable baseLogTable = mock( BaseLogTable.class );
 doCallRealMethod().when( baseLogTable ).getLogBuffer( any( VariableSpace.class ), anyString(), any( LogStatus.class ), anyString() );
 VariableSpace vs = mock( VariableSpace.class );
 String s1 = baseLogTable.getLogBuffer( vs, "1", LogStatus.START, null );
 String s2 = baseLogTable.getLogBuffer( vs, "1", LogStatus.END, null );
 assertEquals( Const.CR + "START" + Const.CR, s1 );
 assertEquals( Const.CR + "START" + Const.CR, s1 + Const.CR + "END" + Const.CR, s2 );
 verify( lb, times( 1 ) ).removeChannelFromBuffer( "1" );
}

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

@Before
public void setup() throws SQLException {
  connection = mock(Connection.class);
  dbmd = mock(DatabaseMetaData.class);
  jdbcMetadata = mock(DefaultJdbcMetadata.class);
  PowerMockito.stub(PowerMockito.method(SqlUtil.class, "getConnection")).toReturn(connection);
  PowerMockito.mockStatic(JdbcMetadataFactory.class);
  when(JdbcMetadataFactory.getJdbcMetadata(anyString(), any(DBConnConf.class))).thenReturn(jdbcMetadata);
  when(connection.getMetaData()).thenReturn(dbmd);
  jdbcExplorer = spy(JdbcExplorer.class);
}

代码示例来源: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: 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);
}

代码示例来源:origin: jenkinsci/gerrit-trigger-plugin

/**
 * Tests
 * {@link TriggerMonitor#buildCompleted(com.sonymobile.tools.gerrit.gerritevents.dto.GerritEvent, hudson.model.Run)}.
 * @throws Exception if so.
 */
@Test
public void testBuildCompleted() throws Exception {
  TriggerMonitor monitor = new TriggerMonitor();
  ManualPatchsetCreated patch = Setup.createManualPatchsetCreated();
  monitor.add(patch);
  monitor.triggerScanStarting(patch);
  AbstractProject project = PowerMockito.mock(AbstractProject.class);
  doReturn("projectX").when(project).getFullName();
  AbstractBuild build = mock(AbstractBuild.class);
  when(build.getProject()).thenReturn(project);
  monitor.projectTriggered(patch, project);
  monitor.buildStarted(patch, build);
  monitor.buildCompleted(patch, build);
  TriggerMonitor.EventState state = monitor.getEvents().get(0);
  assertEquals(1, state.getBuilds().size());
  assertSame(build, state.getBuilds().get(0).getBuild());
}

代码示例来源:origin: pentaho/pentaho-kettle

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

代码示例来源:origin: pentaho/pentaho-kettle

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

代码示例来源:origin: pentaho/pentaho-kettle

@Before
public void setup() throws KettlePluginException {
 rowMeta = spy( new RowMeta() );
 groupByMeta = spy( new GroupByMeta() );
 mockStatic( ValueMetaFactory.class );
 when( ValueMetaFactory.createValueMeta( anyInt() ) ).thenCallRealMethod();
 when( ValueMetaFactory.createValueMeta( anyString(), anyInt() ) ).thenCallRealMethod();
 when( ValueMetaFactory.createValueMeta( "maxDate", 3, -1, -1 ) ).thenReturn( new ValueMetaDate( "maxDate" ) );
 when( ValueMetaFactory.createValueMeta( "minDate", 3, -1, -1 ) ).thenReturn( new ValueMetaDate( "minDate" ) );
 when( ValueMetaFactory.createValueMeta( "countDate", 5, -1, -1 ) ).thenReturn( new ValueMetaInteger( "countDate" ) );
 when( ValueMetaFactory.getValueMetaName( 3 ) ).thenReturn( "Date" );
 when( ValueMetaFactory.getValueMetaName( 5 ) ).thenReturn( "Integer" );
}

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

@Before
public void setup() throws Exception {
 Cluster cluster = Mockito.spy(Cluster.class);
 repository = Mockito.spy(Repository.class);
 spy(Repository.class);
 when(Repository.class, "get").thenReturn(repository);
 doReturn(cluster).when(repository).getCluster();
 handler = new LogoutHandler("/defaultTargetUrl");
}

代码示例来源:origin: pentaho/pentaho-kettle

@Test
 public void callStopCarteRestService() throws Exception {
  WebResource status = mock( WebResource.class );
  doReturn( "<serverstatus>" ).when( status ).get( String.class );

  WebResource stop = mock( WebResource.class );
  doReturn( "Shutting Down" ).when( stop ).get( String.class );

  Client client = mock( Client.class );
  doCallRealMethod().when( client ).addFilter( any( HTTPBasicAuthFilter.class ) );
  doCallRealMethod().when( client ).getHeadHandler();
  doReturn( status ).when( client ).resource( "http://localhost:8080/kettle/status/?xml=Y" );
  doReturn( stop ).when( client ).resource( "http://localhost:8080/kettle/stopCarte" );

  mockStatic( Client.class );
  when( Client.create( any( ClientConfig.class ) ) ).thenReturn( client );

  Carte.callStopCarteRestService( "localhost", "8080", "admin", "Encrypted 2be98afc86aa7f2e4bb18bd63c99dbdde" );

  // the expected value is: "Basic <base64 encoded username:password>"
  assertEquals( "Basic " + new String( Base64.getEncoder().encode( "admin:password".getBytes( "utf-8" ) ) ),
   getInternalState( client.getHeadHandler(), "authentication" ) );
 }
}

代码示例来源:origin: pentaho/pentaho-kettle

@Test
public void getNativeDataTypeSimpleName_Unknown() throws Exception {
 KettleValueException e = new KettleValueException();
 ValueMetaInterface v = mock( ValueMetaInterface.class );
 doThrow( e ).when( v ).getNativeDataTypeClass();
 LogChannel log = mock( LogChannel.class );
 whenNew( LogChannel.class ).withAnyArguments().thenReturn( log );
 assertEquals( "Object", FieldHelper.getNativeDataTypeSimpleName( v ) );
 verify( log, times( 1 ) ).logDebug( "Unable to get name from data type" );
}

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

@Before
public void before() {
 mMockJobMasterContext = Mockito.mock(JobMasterContext.class);
 mMockFileSystemContext = PowerMockito.mock(FileSystemContext.class);
 mMockBlockStore = PowerMockito.mock(AlluxioBlockStore.class);
}

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

@Test(expected = IOException.class)
 public void shouldThrowExceptionOnDistcpFailure() throws Exception {
  Path destination = mock(Path.class);
  Path source = mock(Path.class);
  FileSystem fs = mock(FileSystem.class);
  List<Path> srcPaths = Arrays.asList(source, source);
  HiveConf conf = mock(HiveConf.class);
  CopyUtils copyUtils = Mockito.spy(new CopyUtils(null, conf));

  mockStatic(FileUtils.class);
  mockStatic(Utils.class);
  when(destination.getFileSystem(same(conf))).thenReturn(fs);
  when(source.getFileSystem(same(conf))).thenReturn(fs);
  when(FileUtils.distCp(same(fs), anyListOf(Path.class), same(destination),
             anyBoolean(), eq(null), same(conf),
             same(ShimLoader.getHadoopShims())))
    .thenReturn(false);
  when(Utils.getUGI()).thenReturn(mock(UserGroupInformation.class));
  doReturn(false).when(copyUtils).regularCopy(same(fs), same(fs), anyListOf(ReplChangeManager.FileInfo.class));

  copyUtils.doCopy(destination, srcPaths);
 }
}

相关文章