Search in sources :

Example 56 with InvocationOnMock

use of org.mockito.invocation.InvocationOnMock in project fresco by facebook.

the class JobSchedulerTest method setUp.

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    mFakeClockForTime = new FakeClock();
    mFakeClockForWorker = new FakeClock();
    mFakeClockForScheduled = new FakeClock();
    mFakeClockForTime.incrementBy(1000);
    mFakeClockForWorker.incrementBy(1000);
    mFakeClockForScheduled.incrementBy(1000);
    PowerMockito.mockStatic(SystemClock.class);
    when(SystemClock.uptimeMillis()).thenAnswer(new Answer<Long>() {

        @Override
        public Long answer(InvocationOnMock invocation) throws Throwable {
            return mFakeClockForTime.now();
        }
    });
    mTestExecutorService = new TestExecutorService(mFakeClockForWorker);
    mTestScheduledExecutorService = new TestScheduledExecutorService(mFakeClockForScheduled);
    PowerMockito.mockStatic(JobScheduler.JobStartExecutorSupplier.class);
    when(JobScheduler.JobStartExecutorSupplier.get()).thenReturn(mTestScheduledExecutorService);
    mTestJobRunnable = new TestJobRunnable();
    mJobScheduler = new JobScheduler(mTestExecutorService, mTestJobRunnable, INTERVAL);
}
Also used : FakeClock(com.facebook.imagepipeline.testing.FakeClock) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TestExecutorService(com.facebook.imagepipeline.testing.TestExecutorService) TestScheduledExecutorService(com.facebook.imagepipeline.testing.TestScheduledExecutorService) Before(org.junit.Before)

Example 57 with InvocationOnMock

use of org.mockito.invocation.InvocationOnMock in project HikariCP by brettwooldridge.

the class MockDataSource method createMockConnection.

public static Connection createMockConnection() throws SQLException {
    // Setup mock connection
    final Connection mockConnection = mock(Connection.class);
    // Autocommit is always true by default
    when(mockConnection.getAutoCommit()).thenReturn(true);
    // Handle Connection.createStatement()
    Statement statement = mock(Statement.class);
    when(mockConnection.createStatement()).thenReturn(statement);
    when(mockConnection.createStatement(anyInt(), anyInt())).thenReturn(statement);
    when(mockConnection.createStatement(anyInt(), anyInt(), anyInt())).thenReturn(statement);
    when(mockConnection.isValid(anyInt())).thenReturn(true);
    // Handle Connection.prepareStatement()
    PreparedStatement mockPreparedStatement = mock(PreparedStatement.class);
    when(mockConnection.prepareStatement(anyString())).thenReturn(mockPreparedStatement);
    when(mockConnection.prepareStatement(anyString(), anyInt())).thenReturn(mockPreparedStatement);
    when(mockConnection.prepareStatement(anyString(), any(int[].class))).thenReturn(mockPreparedStatement);
    when(mockConnection.prepareStatement(anyString(), any(String[].class))).thenReturn(mockPreparedStatement);
    when(mockConnection.prepareStatement(anyString(), anyInt(), anyInt())).thenReturn(mockPreparedStatement);
    when(mockConnection.prepareStatement(anyString(), anyInt(), anyInt(), anyInt())).thenReturn(mockPreparedStatement);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            return null;
        }
    }).doNothing().when(mockPreparedStatement).setInt(anyInt(), anyInt());
    ResultSet mockResultSet = mock(ResultSet.class);
    when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet);
    when(mockResultSet.getString(anyInt())).thenReturn("aString");
    when(mockResultSet.next()).thenReturn(true);
    // Handle Connection.prepareCall()
    CallableStatement mockCallableStatement = mock(CallableStatement.class);
    when(mockConnection.prepareCall(anyString())).thenReturn(mockCallableStatement);
    when(mockConnection.prepareCall(anyString(), anyInt(), anyInt())).thenReturn(mockCallableStatement);
    when(mockConnection.prepareCall(anyString(), anyInt(), anyInt(), anyInt())).thenReturn(mockCallableStatement);
    return mockConnection;
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) InvocationOnMock(org.mockito.invocation.InvocationOnMock) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 58 with InvocationOnMock

use of org.mockito.invocation.InvocationOnMock in project glide by bumptech.

the class RequestManagerTest method setUp.

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    connectivityMonitor = mock(ConnectivityMonitor.class);
    ConnectivityMonitorFactory factory = mock(ConnectivityMonitorFactory.class);
    when(factory.build(isA(Context.class), isA(ConnectivityMonitor.ConnectivityListener.class))).thenAnswer(new Answer<ConnectivityMonitor>() {

        @Override
        public ConnectivityMonitor answer(InvocationOnMock invocation) throws Throwable {
            connectivityListener = (ConnectivityListener) invocation.getArguments()[1];
            return connectivityMonitor;
        }
    });
    requestTracker = mock(RequestTracker.class);
    manager = new RequestManager(Glide.get(RuntimeEnvironment.application), lifecycle, treeNode, requestTracker, factory);
}
Also used : Context(android.content.Context) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ConnectivityListener(com.bumptech.glide.manager.ConnectivityMonitor.ConnectivityListener) ConnectivityMonitor(com.bumptech.glide.manager.ConnectivityMonitor) ConnectivityMonitorFactory(com.bumptech.glide.manager.ConnectivityMonitorFactory) RequestTracker(com.bumptech.glide.manager.RequestTracker) Before(org.junit.Before)

Example 59 with InvocationOnMock

use of org.mockito.invocation.InvocationOnMock in project glide by bumptech.

the class EngineKeyTest method testDiffersIfSignatureDiffers.

@Test
public void testDiffersIfSignatureDiffers() throws UnsupportedEncodingException, NoSuchAlgorithmException {
    EngineKey first = harness.build();
    Key signature = mock(Key.class);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
            MessageDigest digest = (MessageDigest) invocationOnMock.getArguments()[0];
            digest.update("signature".getBytes("UTF-8"));
            return null;
        }
    }).when(signature).updateDiskCacheKey(any(MessageDigest.class));
    harness.signature = signature;
    EngineKey second = harness.build();
    KeyAssertions.assertDifferent(first, second, false);
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) MessageDigest(java.security.MessageDigest) Key(com.bumptech.glide.load.Key) Test(org.junit.Test)

Example 60 with InvocationOnMock

use of org.mockito.invocation.InvocationOnMock in project glide by bumptech.

the class EngineTest method testHandlesNonEngineResourcesFromCacheIfPresent.

@Test
public void testHandlesNonEngineResourcesFromCacheIfPresent() {
    final Object expected = new Object();
    @SuppressWarnings("rawtypes") Resource fromCache = mockResource();
    when(fromCache.get()).thenReturn(expected);
    when(harness.cache.remove(eq(harness.cacheKey))).thenReturn(fromCache);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
            Resource<?> resource = (Resource<?>) invocationOnMock.getArguments()[0];
            assertEquals(expected, resource.get());
            return null;
        }
    }).when(harness.cb).onResourceReady(anyResource(), isADataSource());
    harness.doLoad();
    verify(harness.cb).onResourceReady(anyResource(), isADataSource());
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) Util.mockResource(com.bumptech.glide.tests.Util.mockResource) Util.anyResource(com.bumptech.glide.tests.Util.anyResource) Test(org.junit.Test)

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