public static PowerMockitoStubber doReturn(Object toBeReturned, Object... othersToBeReturned) { if (othersToBeReturned != null && othersToBeReturned.length == 0) { return doReturn(toBeReturned); } return POWERMOCKITO_CORE.doAnswer(new ChainReturns(toBeReturned, othersToBeReturned)); } }
private void setTimeOnNanos(long nanoTime) throws Exception { PowerMockito.doReturn(nanoTime) .when(rateLimiter, "currentNanoTime"); }
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()); }
private static SuppressWarningsHolder createHolder(String checkName, int firstLine, int firstColumn, int lastLine, int lastColumn) throws Exception { final Class<?> entry = Class .forName("com.puppycrawl.tools.checkstyle.checks.SuppressWarningsHolder$Entry"); final Constructor<?> entryConstr = entry.getDeclaredConstructor(String.class, int.class, int.class, int.class, int.class); entryConstr.setAccessible(true); final Object entryInstance = entryConstr.newInstance(checkName, firstLine, firstColumn, lastLine, lastColumn); final List<Object> entriesList = new ArrayList<>(); entriesList.add(entryInstance); final ThreadLocal<?> threadLocal = mock(ThreadLocal.class); PowerMockito.doReturn(entriesList).when(threadLocal, "get"); final SuppressWarningsHolder holder = new SuppressWarningsHolder(); final Field entries = holder.getClass().getDeclaredField("ENTRIES"); entries.setAccessible(true); entries.set(holder, threadLocal); return holder; }
@Test public void noWarnIfSpringVersionLarger() throws Exception { spy(SpringSecurityCoreVersion.class); spy(SpringVersion.class); doReturn("4.0.0.RELEASE").when(SpringSecurityCoreVersion.class, "getVersion"); doReturn("4.0.0.RELEASE").when(SpringVersion.class, "getVersion"); performChecks(); verify(logger, never()).warn(any()); }
@Test public void noWarnIfSpringPatchVersionDoubleDigits() throws Exception { String minSpringVersion = "3.2.8.RELEASE"; spy(SpringSecurityCoreVersion.class); spy(SpringVersion.class); doReturn("3.2.0.RELEASE").when(SpringSecurityCoreVersion.class, "getVersion"); doReturn("3.2.10.RELEASE").when(SpringVersion.class, "getVersion"); performChecks(minSpringVersion); verify(logger, never()).warn(any()); }
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); }
@Test public void warnIfSpringVersionTooSmall() throws Exception { spy(SpringSecurityCoreVersion.class); spy(SpringVersion.class); doReturn("3").when(SpringSecurityCoreVersion.class, "getVersion"); doReturn("2").when(SpringVersion.class, "getVersion"); performChecks(); verify(logger, times(1)).warn(any()); }
@Test public void noLoggingIfPropertySet() throws Exception { spy(SpringSecurityCoreVersion.class); spy(SpringVersion.class); doReturn("3").when(SpringSecurityCoreVersion.class, "getVersion"); doReturn("2").when(SpringVersion.class, "getVersion"); System.setProperty(getDisableChecksProperty(), Boolean.TRUE.toString()); performChecks(); verifyZeroInteractions(logger); }
@Test public void noLoggingIfSpringVersionNull() throws Exception { spy(SpringSecurityCoreVersion.class); spy(SpringVersion.class); doReturn("1").when(SpringSecurityCoreVersion.class, "getVersion"); doReturn(null).when(SpringVersion.class, "getVersion"); performChecks(); verifyZeroInteractions(logger); }
@Test public void noLoggingIfVersionsAreEqual() throws Exception { String version = "1"; spy(SpringSecurityCoreVersion.class); spy(SpringVersion.class); doReturn(version).when(SpringSecurityCoreVersion.class, "getVersion"); doReturn(version).when(SpringVersion.class, "getVersion"); performChecks(); verifyZeroInteractions(logger); }
@Test public void testResolveQualifiedNameFails() throws Exception { final Set<String> imports = new HashSet<>(); imports.add("java.applet.someClass"); final ClassResolver classResolver = PowerMockito.spy(new ClassResolver(Thread .currentThread().getContextClassLoader(), "", imports)); PowerMockito.doThrow(new ClassNotFoundException("expected exception")) .when(classResolver, "safeLoad", any()); PowerMockito.doReturn(true).when(classResolver, "isLoadable", any()); try { classResolver.resolve("someClass", ""); fail("IllegalStateException is expected"); } catch (IllegalStateException ex) { // exception is expected final String expected = "expected exception"; assertTrue("Invalid exception cause, should be: ClassNotFoundException", ex.getCause() instanceof ClassNotFoundException); assertTrue("Invalid exception message, should end with: " + expected, ex.getMessage().endsWith(expected)); } }
doReturn(containingTask).when(operator).getContainingTask();
/** * 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(); }
doCallRealMethod().when(operator).dispose(); doReturn(containingTask).when(operator).getContainingTask();
doReturn(false).when(component).canUsePreviousLayout(any(ComponentContext.class));
doReturn(true).when(component).canUsePreviousLayout(any(ComponentContext.class));
@Test @PrepareForTest({Configuration.class}) public void testLoadDefaultSettings() throws Exception { InputStream mockInStream = mock(InputStream.class); Configuration mockConfig = mock(Configuration.class); when(mockAssets.open(anyString())).thenReturn(mockInStream); when(mockContext.getAssets()).thenReturn(mockAssets); mockStatic(Configuration.class); doReturn(mockConfig).when(Configuration.class, "read", any(Reader.class)); assertSame(mockConfig, FileHelper.loadDefaultSettings(mockContext)); Mockito.verify(mockAssets.open(anyString())); }
@Test public void canEndTheSshConnectionTest() throws Exception { PowerMockito.spy(SshHelper.class); Session mockedSession = Mockito.mock(Session.class); PowerMockito.doReturn(true).when(SshHelper.class, "isChannelConditionEof", Mockito.anyInt()); Mockito.when(mockedSession.waitForCondition(ChannelCondition.EXIT_STATUS, 1l)).thenReturn(0); PowerMockito.doNothing().when(SshHelper.class, "throwSshExceptionIfConditionsTimeout", Mockito.anyInt()); SshHelper.canEndTheSshConnection(1, mockedSession, 0); PowerMockito.verifyStatic(); SshHelper.isChannelConditionEof(Mockito.anyInt()); SshHelper.throwSshExceptionIfConditionsTimeout(Mockito.anyInt()); Mockito.verify(mockedSession).waitForCondition(ChannelCondition.EXIT_STATUS, 1l); }
@Before public void setUp() throws Exception { view = mock(View.class); WeakReference viewWeakReference = mock(WeakReference.class); whenNew(WeakReference.class).withArguments(view).thenReturn(viewWeakReference); doReturn(view).when(viewWeakReference).get(); simpleAnimator = new SimpleAnimator(view, 1); mockStatic(AnimationUtils.class); animation = mock(Animation.class); doReturn(animation).when(AnimationUtils.class, "loadAnimation", null, 1); }