Search in sources :

Example 51 with InvocationOnMock

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();
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) RobolectricPackageManager(org.robolectric.res.builder.RobolectricPackageManager) ShadowProvider(org.robolectric.internal.ShadowProvider) Test(org.junit.Test)

Example 52 with InvocationOnMock

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();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) Test(org.junit.Test)

Example 53 with InvocationOnMock

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);
}
Also used : RequestMatcher(org.springframework.security.web.util.matcher.RequestMatcher) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) Filter(javax.servlet.Filter) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Before(org.junit.Before)

Example 54 with InvocationOnMock

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);
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Config(com.hazelcast.config.Config) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 55 with InvocationOnMock

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);
}
Also used : BitmapPool(com.facebook.imagepipeline.memory.BitmapPool) EncodedImage(com.facebook.imagepipeline.image.EncodedImage) TrivialPooledByteBuffer(com.facebook.imagepipeline.testing.TrivialPooledByteBuffer) ByteBuffer(java.nio.ByteBuffer) PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer) Bitmap(android.graphics.Bitmap) Random(java.util.Random) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TrivialPooledByteBuffer(com.facebook.imagepipeline.testing.TrivialPooledByteBuffer) MockBitmapFactory(com.facebook.imagepipeline.testing.MockBitmapFactory) BitmapFactory(android.graphics.BitmapFactory) Before(org.junit.Before)

Aggregations

InvocationOnMock (org.mockito.invocation.InvocationOnMock)1088 Test (org.junit.Test)655 Answer (org.mockito.stubbing.Answer)287 Matchers.anyString (org.mockito.Matchers.anyString)145 HashMap (java.util.HashMap)124 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)113 Before (org.junit.Before)110 Mockito.doAnswer (org.mockito.Mockito.doAnswer)110 ArrayList (java.util.ArrayList)109 IOException (java.io.IOException)92 Context (android.content.Context)68 List (java.util.List)62 AtomicReference (java.util.concurrent.atomic.AtomicReference)61 CountDownLatch (java.util.concurrent.CountDownLatch)59 Test (org.testng.annotations.Test)59 File (java.io.File)55 UUID (java.util.UUID)46 Configuration (org.apache.hadoop.conf.Configuration)46 Activity (android.app.Activity)40 Semaphore (java.util.concurrent.Semaphore)38