@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" ); }
@Test public void testRemoteExecutionWithSavepoint() throws Exception { SavepointRestoreSettings restoreSettings = SavepointRestoreSettings.forPath("fakePath"); RemoteStreamEnvironment env = new RemoteStreamEnvironment("fakeHost", 1, null, new String[]{}, null, restoreSettings); env.fromElements(1).map(x -> x * 2); RestClusterClient mockedClient = Mockito.mock(RestClusterClient.class); JobExecutionResult expectedResult = new JobExecutionResult(null, 0, null); PowerMockito.whenNew(RestClusterClient.class).withAnyArguments().thenReturn(mockedClient); when(mockedClient.run(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.eq(restoreSettings))) .thenReturn(expectedResult); JobExecutionResult actualResult = env.execute("fakeJobName"); Assert.assertEquals(expectedResult, actualResult); } }
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); }
@Test public void testOnItems() throws Exception { MainActivity activity = spy(MainActivity.class); PowerMockito.whenNew(ArrayAdapter.class).withArguments(activity, R.layout.item).thenReturn(arrayAdapter); activity.onCreate(null); ServerAPI.Item[] items = {new ServerAPI.Item(TEXT)}; activity.onItems(items, ""); InOrder inOrder = inOrder(arrayAdapter); inOrder.verify(arrayAdapter, times(1)).clear(); inOrder.verify(arrayAdapter, times(1)).addAll(items); }
@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); 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); }
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); }
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); }
.mock(SequenceFile.Reader.class); PowerMockito .whenNew(SequenceFile.Reader.class) .withParameterTypes(FileSystem.class, Path.class, Configuration.class)
/** * Verifies that the port passed to the RemoteStreamEnvironment is used for connecting to the cluster. */ @Test public void testPortForwarding() throws Exception { String host = "fakeHost"; int port = 99; JobExecutionResult expectedResult = new JobExecutionResult(null, 0, null); RestClusterClient mockedClient = Mockito.mock(RestClusterClient.class); when(mockedClient.run(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())) .thenReturn(expectedResult); PowerMockito.whenNew(RestClusterClient.class).withAnyArguments().thenAnswer((invocation) -> { Object[] args = invocation.getArguments(); Configuration config = (Configuration) args[0]; Assert.assertEquals(host, config.getString(RestOptions.ADDRESS)); Assert.assertEquals(port, config.getInteger(RestOptions.PORT)); return mockedClient; } ); final Configuration clientConfiguration = new Configuration(); final StreamExecutionEnvironment env = StreamExecutionEnvironment.createRemoteEnvironment( host, port, clientConfiguration); env.fromElements(1).map(x -> x * 2); JobExecutionResult actualResult = env.execute("fakeJobName"); Assert.assertEquals(expectedResult, actualResult); }
@Test public void testConfigWithIgnoreExceptionalAttributes() throws Exception { // emulate exception from unrelated code, but that is same try-catch final DefaultConfiguration tested = PowerMockito.mock(DefaultConfiguration.class); when(tested.getAttributeNames()).thenReturn(new String[] {"severity"}); when(tested.getName()).thenReturn("MemberName"); when(tested.getAttribute("severity")).thenThrow(CheckstyleException.class); // to void creation of 2 other mocks for now reason, only one moc is used for all cases PowerMockito.whenNew(DefaultConfiguration.class) .withArguments("MemberName", ThreadModeSettings.SINGLE_THREAD_MODE_INSTANCE) .thenReturn(tested); PowerMockito.whenNew(DefaultConfiguration.class) .withArguments("Checker", ThreadModeSettings.SINGLE_THREAD_MODE_INSTANCE) .thenReturn(tested); PowerMockito.whenNew(DefaultConfiguration.class) .withArguments("TreeWalker", ThreadModeSettings.SINGLE_THREAD_MODE_INSTANCE) .thenReturn(tested); try { ConfigurationLoader.loadConfiguration( getPath("InputConfigurationLoaderModuleIgnoreSeverity.xml"), new PropertiesExpander(new Properties()), true); fail("Exception is expected"); } catch (CheckstyleException expected) { assertEquals("Invalid exception cause message", "Problem during accessing 'severity' attribute for MemberName", expected.getCause().getMessage()); } }
@Test public void getGetSignature_Serializable() throws Exception { LogChannel log = mock( LogChannel.class ); whenNew( LogChannel.class ).withAnyArguments().thenReturn( log ); ValueMetaSerializable v = new ValueMetaSerializable( "Data" ); String accessor = FieldHelper.getAccessor( true, "Data" ); assertEquals( "Object Data = get(Fields.In, \"Data\").getObject(r);", FieldHelper.getGetSignature( accessor, v ) ); assertNotNull( getMethod( FieldHelper.class, "getObject", Object[].class ) ); }
whenNew(StateSnapshotContextSynchronousImpl.class).withAnyArguments().thenReturn(context);
@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); }
whenNew(OperatorSnapshotFinalizer.class). withAnyArguments(). thenAnswer((Answer<OperatorSnapshotFinalizer>) invocation -> {
/** * Checks that the state snapshot context is closed after a successful snapshot operation. */ @Test public void testSnapshotMethod() throws Exception { final long checkpointId = 42L; final long timestamp = 1L; final CloseableRegistry closeableRegistry = new CloseableRegistry(); StateSnapshotContextSynchronousImpl context = spy(new StateSnapshotContextSynchronousImpl(0L, 0L)); whenNew(StateSnapshotContextSynchronousImpl.class).withAnyArguments().thenReturn(context); StreamTask<Void, AbstractStreamOperator<Void>> containingTask = mock(StreamTask.class); when(containingTask.getCancelables()).thenReturn(closeableRegistry); AbstractStreamOperator<Void> operator = mock(AbstractStreamOperator.class); when(operator.snapshotState(anyLong(), anyLong(), any(CheckpointOptions.class), any(CheckpointStreamFactory.class))).thenCallRealMethod(); doReturn(containingTask).when(operator).getContainingTask(); operator.snapshotState( checkpointId, timestamp, CheckpointOptions.forCheckpointWithDefaultLocation(), new MemCheckpointStreamFactory(Integer.MAX_VALUE)); verify(context).close(); }
@Before public void before() throws Exception { suppress(methods(BASE_VIEW_CLASS, "onCreate")); suppress(methods(BASE_VIEW_CLASS, "setContentView")); stub(method(BASE_VIEW_CLASS, "getPresenter")).toReturn(mainPresenter); replace(method(BASE_VIEW_CLASS, "findViewById")).with(new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { switch ((int)args[0]) { case R.id.listView: return listView; case R.id.check1: return check1; case R.id.check2: return check2; } return null; } }); activity = spy(MainActivity.class); PowerMockito.whenNew(ArrayAdapter.class).withArguments(activity, R.layout.item).thenReturn(arrayAdapter); }
whenNew(LocalDataOutputStream.class).withAnyArguments().thenAnswer(new Answer<LocalDataOutputStream>() {