Refine search
public Object answer(final InvocationOnMock invocation) throws Throwable { Object defaultReturnValue = delegate.answer(invocation); if (defaultReturnValue != null) { return defaultReturnValue; } Class<?> type = invocation.getMethod().getReturnType(); if (!type.isPrimitive() && !Modifier.isFinal(type.getModifiers())) { final Location location = new LocationImpl(); return Mockito.mock(type, new ThrowsSmartNullPointer(invocation, location)); } return null; }
protected void blockDataContainerIteration(final Cache<?, ?> cache, final CyclicBarrier barrier) { InternalDataContainer dataContainer = TestingUtil.extractComponent(cache, InternalDataContainer.class); final Answer<Object> forwardedAnswer = AdditionalAnswers.delegatesTo(dataContainer); InternalDataContainer mockContainer = mock(InternalDataContainer.class, withSettings().defaultAnswer(forwardedAnswer)); doAnswer(invocation -> { // Wait for main thread to sync up barrier.await(10, TimeUnit.SECONDS); // Now wait until main thread lets us through barrier.await(10, TimeUnit.SECONDS); return forwardedAnswer.answer(invocation); }).when(mockContainer).removeSegments(any()); TestingUtil.replaceComponent(cache, InternalDataContainer.class, mockContainer, true); } }
@Test public void testConstructor_avoidsEclipseWriterBug() throws IOException { // Due to a bug in Eclipse, we *must* call close on the object returned from openWriter(). // Eclipse proxies a Writer but does not implement the fluent API correctly. // Here, we implement the fluent Writer API with the same bug: Writer mockWriter = Mockito.mock(Writer.class, (Answer<?>) invocation -> { if (Writer.class.isAssignableFrom(invocation.getMethod().getReturnType())) { // Erroneously return the delegate writer (matching the Eclipse bug!) return source; } else { return Answers.RETURNS_SMART_NULLS.get().answer(invocation); } }); when(sourceFile.openWriter()).thenReturn(mockWriter); FilerUtils.writeCompilationUnit(filer, unit, originatingElement); verify(mockWriter).close(); }
@Override public QueueingSegmentListener putIfAbsent(UUID key, QueueingSegmentListener value) { log.tracef("Adding segment listener %s : %s", key, value); final Answer<Object> listenerAnswer = AdditionalAnswers.delegatesTo(value); QueueingSegmentListener mockListener = mock(QueueingSegmentListener.class, withSettings().defaultAnswer(listenerAnswer)); doAnswer(i -> { Supplier<PrimitiveIterator.OfInt> segments = i.getArgument(0); if (log.isTraceEnabled()) { Set<Integer> segmentsCompleted = new HashSet<>(); return listenerAnswer.answer(i); }).when(mockListener).accept(Mockito.any(Supplier.class)); doAnswer(i -> { Object k = i.getArgument(0); log.tracef("Notified for key %s", k); if (keyToWaitFor.equals(k)) { return listenerAnswer.answer(i); }).when(mockListener).notifiedKey(any());
/** * Replaces L1Manager in cache with a proxy one that will block on * {#link L1Manager#registerL1WriteSynchronizer} until the checkpoint is triggered * @param cache The cache to replace the L1Manager on * @param checkPoint The checkpoint to use to trigger blocking * @return The original real L1Manager */ protected L1Manager waitUntilL1Registration(final Cache<?, ?> cache, final CheckPoint checkPoint) { L1Manager l1Manager = TestingUtil.extractComponent(cache, L1Manager.class); final Answer<Object> forwardedAnswer = AdditionalAnswers.delegatesTo(l1Manager); L1Manager mockL1 = mock(L1Manager.class, withSettings().defaultAnswer(forwardedAnswer).extraInterfaces(RemoteValueRetrievedListener.class)); doAnswer(invocation -> { // Wait for main thread to sync up checkPoint.trigger("pre_acquire_shared_topology_lock_invoked"); // Now wait until main thread lets us through checkPoint.awaitStrict("pre_acquire_shared_topology_lock_released", 10, TimeUnit.SECONDS); return forwardedAnswer.answer(invocation); }).when(mockL1).registerL1WriteSynchronizer(Mockito.notNull(), Mockito.any(L1WriteSynchronizer.class)); TestingUtil.replaceComponent(cache, L1Manager.class, mockL1, true); return l1Manager; } }
public static void stubRegistryKeys(MuleContext muleContext, final String... keys) { when(((MuleContextWithRegistry) muleContext).getRegistry().get(anyString())).thenAnswer(invocation -> { String name = (String) invocation.getArguments()[0]; if (name != null) { for (String key : keys) { if (name.contains(key)) { return null; } } } return RETURNS_DEEP_STUBS.get().answer(invocation); }); }
protected void waitUntilNotificationRaised(final Cache<?, ?> cache, final CheckPoint checkPoint) { CacheNotifier cn = TestingUtil.extractComponent(cache, CacheNotifier.class); final Answer<Object> forwardedAnswer = AdditionalAnswers.delegatesTo(cn); CacheNotifier mockNotifier = mock(CacheNotifier.class, withSettings().extraInterfaces(ClusterCacheNotifier.class) .defaultAnswer(forwardedAnswer)); Answer answer = invocation -> { // Wait for main thread to sync up checkPoint.trigger("pre_raise_notification_invoked"); // Now wait until main thread lets us through checkPoint.awaitStrict("pre_raise_notification_release", 10, TimeUnit.SECONDS); try { return forwardedAnswer.answer(invocation); } finally { // Wait for main thread to sync up checkPoint.trigger("post_raise_notification_invoked"); // Now wait until main thread lets us through checkPoint.awaitStrict("post_raise_notification_release", 10, TimeUnit.SECONDS); } }; doAnswer(answer).when(mockNotifier).notifyCacheEntryCreated(any(), any(), any(Metadata.class), eq(false), any(InvocationContext.class), any(FlagAffectedCommand.class)); doAnswer(answer).when(mockNotifier).notifyCacheEntryModified(any(), any(), any(Metadata.class), any(), any(Metadata.class), anyBoolean(), any(InvocationContext.class), any(FlagAffectedCommand.class)); doAnswer(answer).when(mockNotifier).notifyCacheEntryRemoved(any(), any(), any(Metadata.class), eq(false), any(InvocationContext.class), any(FlagAffectedCommand.class)); TestingUtil.replaceComponent(cache, CacheNotifier.class, mockNotifier, true); }
public Object answer(InvocationOnMock invocation) throws Throwable { return implementation.answer(invocation); } }
protected void waitUntilToplogyInstalled(final Cache<?, ?> cache, final CheckPoint checkPoint) { StateTransferLock sc = TestingUtil.extractComponent(cache, StateTransferLock.class); final Answer<Object> forwardedAnswer = AdditionalAnswers.delegatesTo(sc); StateTransferLock mockConsumer = mock(StateTransferLock.class, withSettings().defaultAnswer(forwardedAnswer)); doAnswer(invocation -> { Object answer = forwardedAnswer.answer(invocation); // Wait for main thread to sync up checkPoint.trigger("post_topology_installed_invoked_" + cache); // Now wait until main thread lets us through checkPoint.awaitStrict("post_topology_installed_released_" + cache, 10, TimeUnit.SECONDS); return answer; }).when(mockConsumer).notifyTopologyInstalled(anyInt()); TestingUtil.replaceComponent(cache, StateTransferLock.class, mockConsumer, true); } }
@Override public Object answer(final InvocationOnMock invocation) throws Throwable { TimeUnit.MILLISECONDS.sleep(sleepyTime); return answer.answer(invocation); }
private void blockRebalanceConfirmation(final EmbeddedCacheManager manager, final CheckPoint checkPoint, int rebalanceTopologyId) throws Exception { ClusterTopologyManager ctm = TestingUtil.extractGlobalComponent(manager, ClusterTopologyManager.class); final Answer<Object> forwardedAnswer = AdditionalAnswers.delegatesTo(ctm); ClusterTopologyManager mockManager = mock(ClusterTopologyManager.class, withSettings().defaultAnswer(forwardedAnswer)); doAnswer(invocation -> { Object[] arguments = invocation.getArguments(); Address source = (Address) arguments[1]; int topologyId = (Integer) arguments[2]; if (topologyId == rebalanceTopologyId) { checkPoint.trigger("pre_rebalance_confirmation_" + topologyId + "_from_" + source); checkPoint.awaitStrict("resume_rebalance_confirmation_" + topologyId + "_from_" + source, 20, SECONDS); } return forwardedAnswer.answer(invocation); }).when(mockManager).handleRebalancePhaseConfirm(anyString(), any(Address.class), anyInt(), isNull(), anyInt()); TestingUtil.replaceComponent(manager, ClusterTopologyManager.class, mockManager, true); }
public Object answer(final InvocationOnMock invocation) throws Throwable { Object defaultReturnValue = delegate.answer(invocation); if (defaultReturnValue != null) { return defaultReturnValue; } Class<?> type = invocation.getMethod().getReturnType(); final Type returnType = invocation.getMethod().getGenericReturnType(); if (returnType instanceof TypeVariable) { type = findTypeFromGeneric(invocation, (TypeVariable) returnType); if (type != null) { defaultReturnValue = delegateChains(type); } } if (defaultReturnValue != null) { return defaultReturnValue; } if (type != null && !type.isPrimitive() && !Modifier.isFinal(type.getModifiers())) { final Location location = new LocationImpl(); return Mockito.mock(type, new ThrowsSmartNullPointer(invocation, location)); } return null; }