@Before public void before() throws Exception { MockitoAnnotations.initMocks(this); loader = spy(new FilterLoader(registry, compiler, filterFactory)); doReturn(TestZuulFilter.class).when(compiler).compile(file); when(file.getAbsolutePath()).thenReturn("/filters/in/SomeFilter.groovy"); }
@Test public void clearRequests_withRequestThatClearsAnother_avoidsConcurrentModifications() { Request first = mock(Request.class); Request second = mock(Request.class); doAnswer(new ClearAndRemoveRequest(second)).when(first).clear(); tracker.addRequest(mock(Request.class)); tracker.addRequest(first); tracker.addRequest(second); tracker.clearRequests(); }
@Test public void testGetFilterFromString() throws Exception { String string = ""; doReturn(TestZuulFilter.class).when(compiler).compile(string, string); ZuulFilter filter = loader.getFilter(string, string); assertNotNull(filter); assertTrue(filter.getClass() == TestZuulFilter.class); // assertTrue(loader.filterInstanceMapSize() == 1); }
private static LeaderContender mockContender(final LeaderElectionService service, final String address) { LeaderContender mockContender = mock(LeaderContender.class); when(mockContender.getAddress()).thenReturn(address); doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { final UUID uuid = (UUID) invocation.getArguments()[0]; service.confirmLeaderSessionID(uuid); return null; } }).when(mockContender).grantLeadership(any(UUID.class)); return mockContender; } }
/** * Mocks a subscriber and prepares it to request Long.MAX_VALUE. * @param <T> the value type * @return the mocked subscriber */ @SuppressWarnings("unchecked") public static <T> FlowableSubscriber<T> mockSubscriber() { FlowableSubscriber<T> w = mock(FlowableSubscriber.class); Mockito.doAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock a) throws Throwable { Subscription s = a.getArgument(0); s.request(Long.MAX_VALUE); return null; } }).when(w).onSubscribe((Subscription)any()); return w; }
@Test public void restartRequests_withRequestThatClearsAnother_avoidsConcurrentModifications() { Request first = mock(Request.class); Request second = mock(Request.class); doAnswer(new ClearAndRemoveRequest(second)).when(first).clear(); tracker.addRequest(mock(Request.class)); tracker.addRequest(first); tracker.addRequest(second); tracker.restartRequests(); }
@Test(expected = KeyNotFoundException.class) public void testKeyNotFoundException() throws Exception { Map<String, Object> conf = Utils.readStormConfig(); String key1 = "key1"; conf.put(Config.STORM_LOCAL_DIR, "target"); LocalFsBlobStore bs = new LocalFsBlobStore(); LocalFsBlobStore spy = spy(bs); Mockito.doReturn(true).when(spy).checkForBlobOrDownload(key1); Mockito.doNothing().when(spy).checkForBlobUpdate(key1); spy.prepare(conf, null, null, null); spy.getBlob(key1, null); }
@Test public void closeAllAndRethrowException() throws Exception { doThrow( new IOException( "Faulty closable" ) ).when( faultyClosable ).close(); expectedException.expect( IOException.class ); expectedException.expectMessage( "Exception closing multiple resources" ); expectedException.expect( new NestedThrowableMatcher( IOException.class ) ); IOUtils.closeAll( goodClosable1, faultyClosable, goodClosable2 ); }
private IndexPopulation addPopulator( IndexPopulator indexPopulator, int id ) throws FlipFailedKernelException { FlippableIndexProxy indexProxy = mock( FlippableIndexProxy.class ); when( indexProxy.getState() ).thenReturn( InternalIndexState.ONLINE ); doAnswer( invocation -> { Callable argument = invocation.getArgument( 0 ); return argument.call(); } ).when( indexProxy ).flip( any( Callable.class ), any( FailedIndexProxyFactory.class ) ); return addPopulator( indexPopulator, id, indexProxy, mock( FailedIndexProxyFactory.class ) ); } }
static Context mockPackageResourceContext() { Context context = mock(Context.class); PackageManager pm = mock(PackageManager.class); Resources res = mock(Resources.class); doReturn(pm).when(context).getPackageManager(); try { doReturn(res).when(pm).getResourcesForApplication(RESOURCE_PACKAGE); } catch (PackageManager.NameNotFoundException e) { throw new RuntimeException(e); } doReturn(RESOURCE_ID_1).when(res).getIdentifier(RESOURCE_NAME, RESOURCE_TYPE, RESOURCE_PACKAGE); return context; }