use of org.mockito.invocation.InvocationOnMock in project robolectric by robolectric.
the class RobolectricTest method reset_shouldResetShadows_beforeClearingPackageManager.
@Test
public void reset_shouldResetShadows_beforeClearingPackageManager() {
Iterable<ShadowProvider> oldProviders = ReflectionHelpers.getStaticField(Robolectric.class, "providers");
;
ShadowProvider mockProvider = new MockProvider();
List<ShadowProvider> mockProviders = Collections.singletonList(mockProvider);
ReflectionHelpers.setStaticField(Robolectric.class, "providers", mockProviders);
RobolectricPackageManager mockManager = mock(RobolectricPackageManager.class);
doAnswer(new Answer<Void>() {
public Void answer(InvocationOnMock invocation) {
order.add("packageManager");
return null;
}
}).when(mockManager).reset();
RuntimeEnvironment.setRobolectricPackageManager(mockManager);
try {
Robolectric.reset();
} finally {
// Make sure we clean up after ourselves
ReflectionHelpers.setStaticField(Robolectric.class, "providers", oldProviders);
}
assertThat(order).as("reset order").containsExactly("shadowProvider", "packageManager");
assertThat(RuntimeEnvironment.application).as("app after reset").isNull();
assertThat(RuntimeEnvironment.getRobolectricPackageManager()).as("packageManager after reset").isNull();
assertThat(RuntimeEnvironment.getActivityThread()).as("activityThread after reset").isNull();
}
use of org.mockito.invocation.InvocationOnMock in project spring-security by spring-projects.
the class FilterChainProxyTests method doFilterClearsSecurityContextHolderOnceOnForwards.
// SEC-2027
@Test
public void doFilterClearsSecurityContextHolderOnceOnForwards() throws Exception {
final FilterChain innerChain = mock(FilterChain.class);
when(matcher.matches(any(HttpServletRequest.class))).thenReturn(true);
doAnswer(new Answer<Object>() {
public Object answer(InvocationOnMock inv) throws Throwable {
TestingAuthenticationToken expected = new TestingAuthenticationToken("username", "password");
SecurityContextHolder.getContext().setAuthentication(expected);
doAnswer(new Answer<Object>() {
public Object answer(InvocationOnMock inv) throws Throwable {
innerChain.doFilter(request, response);
return null;
}
}).when(filter).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class), any(FilterChain.class));
;
fcp.doFilter(request, response, innerChain);
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(expected);
return null;
}
}).when(filter).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class), any(FilterChain.class));
fcp.doFilter(request, response, chain);
verify(innerChain).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull();
}
use of org.mockito.invocation.InvocationOnMock in project spring-security by spring-projects.
the class FilterChainProxyTests method setup.
@Before
public void setup() throws Exception {
matcher = mock(RequestMatcher.class);
filter = mock(Filter.class);
doAnswer(new Answer<Object>() {
public Object answer(InvocationOnMock inv) throws Throwable {
Object[] args = inv.getArguments();
FilterChain fc = (FilterChain) args[2];
HttpServletRequestWrapper extraWrapper = new HttpServletRequestWrapper((HttpServletRequest) args[0]);
fc.doFilter(extraWrapper, (HttpServletResponse) args[1]);
return null;
}
}).when(filter).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class), any(FilterChain.class));
fcp = new FilterChainProxy(new DefaultSecurityFilterChain(matcher, Arrays.asList(filter)));
fcp.setFilterChainValidator(mock(FilterChainProxy.FilterChainValidator.class));
request = new MockHttpServletRequest();
request.setServletPath("/path");
response = new MockHttpServletResponse();
chain = mock(FilterChain.class);
}
use of org.mockito.invocation.InvocationOnMock in project hazelcast by hazelcast.
the class HazelcastInstanceFactoryTest method test_NewInstance_terminateInstance_afterNodeStart.
@Test(expected = IllegalStateException.class)
public void test_NewInstance_terminateInstance_afterNodeStart() throws Exception {
NodeContext context = new TestNodeContext() {
@Override
public NodeExtension createNodeExtension(final Node node) {
NodeExtension nodeExtension = super.createNodeExtension(node);
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
node.hazelcastInstance.shutdown();
return null;
}
}).when(nodeExtension).afterStart();
return nodeExtension;
}
};
Config config = new Config();
config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
hazelcastInstance = HazelcastInstanceFactory.newHazelcastInstance(config, randomString(), context);
}
use of org.mockito.invocation.InvocationOnMock in project fresco by facebook.
the class ArtDecoderTest method setUp.
@Before
public void setUp() throws Exception {
final Random random = new Random();
random.setSeed(RANDOM_SEED);
mEncodedBytes = new byte[ENCODED_BYTES_LENGTH];
random.nextBytes(mEncodedBytes);
mPooledByteBuffer = new TrivialPooledByteBuffer(mEncodedBytes);
mBitmapPool = mock(BitmapPool.class);
mArtDecoder = new ArtDecoder(mBitmapPool, 1, new Pools.SynchronizedPool(1));
mByteBufferRef = CloseableReference.of(mPooledByteBuffer);
mEncodedImage = new EncodedImage(mByteBufferRef);
mEncodedImage.setImageFormat(DefaultImageFormats.JPEG);
mBitmap = MockBitmapFactory.create();
doReturn(mBitmap).when(mBitmapPool).get(MockBitmapFactory.DEFAULT_BITMAP_SIZE);
mBitmapFactoryDefaultAnswer = new Answer<Bitmap>() {
@Override
public Bitmap answer(InvocationOnMock invocation) throws Throwable {
final BitmapFactory.Options options = (BitmapFactory.Options) invocation.getArguments()[2];
options.outWidth = MockBitmapFactory.DEFAULT_BITMAP_WIDTH;
options.outHeight = MockBitmapFactory.DEFAULT_BITMAP_HEIGHT;
verifyBitmapFactoryOptions(options);
return options.inJustDecodeBounds ? null : mBitmap;
}
};
whenBitmapFactoryDecodeStream().thenAnswer(mBitmapFactoryDefaultAnswer);
ByteBuffer buf = mArtDecoder.mDecodeBuffers.acquire();
mTempStorage = buf.array();
mArtDecoder.mDecodeBuffers.release(buf);
}
Aggregations