public static List<List<String>> createMockGetAllTopicsSequenceFromFixedReturn(final List<String> fixed) { @SuppressWarnings("unchecked") List<List<String>> mockSequence = mock(List.class); when(mockSequence.get(anyInt())).thenAnswer(new Answer<List<String>>() { @Override public List<String> answer(InvocationOnMock invocationOnMock) throws Throwable { return new ArrayList<>(fixed); } }); return mockSequence; }
private Response.ResponseBuilder mockResponseBuilder( Response response, final AtomicReference<StreamingOutput> ref ) { final Response.ResponseBuilder responseBuilder = mock( Response.ResponseBuilder.class ); when( responseBuilder.entity( ArgumentMatchers.isA( StreamingOutput.class ) ) ).thenAnswer( invocationOnMock -> { ref.set( invocationOnMock.getArgument( 0 ) ); return responseBuilder; } ); when( responseBuilder.type( ArgumentMatchers.<MediaType>any() ) ).thenReturn( responseBuilder ); when( responseBuilder.build() ).thenReturn( response ); return responseBuilder; }
@Before public void setUp() throws Exception { when(servletContext.getContextPath()).thenReturn(TEST_CONTEXT); when(servletContext.getResourceAsStream("/index.html")).thenAnswer( (Answer<InputStream>) invocationOnMock -> toInputStream("Content of default index.html with context [%WEB_CONTEXT%], status [%SERVER_STATUS%], instance [%INSTANCE%]", UTF_8)); when(servletContext.getResourceAsStream("/integration/vsts/index.html")) .thenAnswer((Answer<InputStream>) invocationOnMock -> toInputStream("Content of vsts index.html with context [%WEB_CONTEXT%]", UTF_8)); }
@Test public void call_sets_and_restores_thread_name_with_information_of_worker_when_there_is_no_task_to_process() throws Exception { String threadName = randomAlphabetic(3); when(queue.peek(anyString())).thenAnswer(invocation -> { assertThat(Thread.currentThread().getName()) .isEqualTo("Worker " + randomOrdinal + " (UUID=" + workerUuid + ") on " + threadName); return Optional.empty(); }); Thread newThread = createThreadNameVerifyingThread(threadName); newThread.start(); newThread.join(); }
@Before public void setUp() { MockitoAnnotations.initMocks(this); when(arrayPool.get(anyInt(), eq(byte[].class))) .thenAnswer(new Answer<byte[]>() { @Override public byte[] answer(InvocationOnMock invocation) throws Throwable { int size = (Integer) invocation.getArguments()[0]; return new byte[size]; } }); }
private static <T extends PropertyContainer> T mockPropertyContainer( Class<T> type, Properties properties ) { T container = mock( type ); when( container.getProperty( anyString() ) ).thenAnswer( properties ); when( container.getProperty( anyString(), any() ) ).thenAnswer( properties ); when( container.getPropertyKeys() ).thenReturn( properties ); when( container.getAllProperties() ).thenReturn( properties.getProperties() ); return container; } }
@Test public void transform_withBitmapDrawable_andUnitBitmapTransformation_doesNotRecycle() { when( bitmapTransformation .transform( any(Context.class), anyBitmapResource(), anyInt(), anyInt())) .thenAnswer(new ReturnGivenResource()); Bitmap bitmap = Bitmap.createBitmap(100, 200, Bitmap.Config.ARGB_8888); BitmapDrawable drawable = new BitmapDrawable(context.getResources(), bitmap); @SuppressWarnings("unchecked") Resource<Drawable> input = (Resource<Drawable>) (Resource<?>) new BitmapDrawableResource(drawable, bitmapPool); transformation.transform(context, input, /*outWidth=*/ 100, /*outHeight=*/ 200); assertThat(bitmap.isRecycled()).isFalse(); }
public static List<List<KafkaTopicPartition>> createMockGetAllPartitionsFromTopicsSequenceFromFixedReturn(final List<KafkaTopicPartition> fixed) { @SuppressWarnings("unchecked") List<List<KafkaTopicPartition>> mockSequence = mock(List.class); when(mockSequence.get(anyInt())).thenAnswer(new Answer<List<KafkaTopicPartition>>() { @Override public List<KafkaTopicPartition> answer(InvocationOnMock invocationOnMock) throws Throwable { return new ArrayList<>(fixed); } }); return mockSequence; } }
@Before public void setUp() { MockitoAnnotations.initMocks(this); when(pool.getMaxSize()).thenReturn(poolSize); when(pool.getDirty(anyInt(), anyInt(), any(Bitmap.Config.class))) .thenAnswer(new CreateBitmap()); when(cache.getMaxSize()).thenReturn(cacheSize); bitmapPreFiller = new BitmapPreFiller(cache, pool, DecodeFormat.DEFAULT); }
@Before public void setUp() { MockitoAnnotations.initMocks(this); when(bitmapPool.get(anyInt(), anyInt(), any(Bitmap.Config.class))) .thenAnswer(new Util.CreateBitmap()); }
@Test public void readByteChannelError() throws Exception { ReadableByteChannel channel = mock(ReadableByteChannel.class); when(channel.read(any())) .thenAnswer(invocation -> { ByteBuffer buffer = invocation.getArgument(0); buffer.put("foo".getBytes(StandardCharsets.UTF_8)); buffer.flip(); return 3; }) .thenThrow(new IOException()); Flux<DataBuffer> result = DataBufferUtils.readByteChannel(() -> channel, this.bufferFactory, 3); StepVerifier.create(result) .consumeNextWith(stringConsumer("foo")) .expectError(IOException.class) .verify(Duration.ofSeconds(3)); }
@Test public void transform_withBitmapDrawable_andFunctionalBitmapTransformation_doesNotRecycle() { when(bitmapTransformation.transform( any(Context.class), anyBitmapResource(), anyInt(), anyInt())) .thenAnswer(new Answer<Resource<Bitmap>>() { @Override public Resource<Bitmap> answer(InvocationOnMock invocationOnMock) throws Throwable { return BitmapResource.obtain( Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888), bitmapPool); } }); Bitmap bitmap = Bitmap.createBitmap(100, 200, Bitmap.Config.ARGB_8888); BitmapDrawable drawable = new BitmapDrawable(context.getResources(), bitmap); @SuppressWarnings("unchecked") Resource<Drawable> input = (Resource<Drawable>) (Resource<?>) new BitmapDrawableResource(drawable, bitmapPool); transformation.transform(context, input, /*outWidth=*/ 100, /*outHeight=*/ 200); assertThat(bitmap.isRecycled()).isFalse(); }
private ClassLoader newClassLoader() { ClassLoader loader = mock(ClassLoader.class); when(loader.getResourceAsStream(anyString())).thenAnswer(new Answer<InputStream>() { public InputStream answer(InvocationOnMock invocation) throws Throwable { return new FileInputStream(Resources.getResource((String) invocation.getArguments()[0]).getPath()); } }); return loader; }
@Before public void setup() { when( transactionToApply.transactionId() ).thenReturn( 1L ); when( indexingService.convertToIndexUpdates( any(), eq( EntityType.NODE ) ) ).thenAnswer( o -> Iterables.empty() ); }
@Before public void shutUp() { initMocks(this); when(picasso.transformRequest(any(Request.class))).thenAnswer(TRANSFORM_REQUEST_ANSWER); }
private MockServletContext mockServletContext(String servletPath) { MockServletContext servletContext = spy(new MockServletContext()); final ServletRegistration registration = mock(ServletRegistration.class); when(registration.getMappings()).thenReturn(Collections.singleton(servletPath)); Answer<Map<String, ? extends ServletRegistration>> answer = invocation -> Collections.singletonMap("spring", registration); when(servletContext.getServletRegistrations()).thenAnswer(answer); return servletContext; }
@Test public void call_sets_and_restores_thread_name_with_information_of_worker_when_a_task_is_processed() throws Exception { String threadName = randomAlphabetic(3); when(queue.peek(anyString())).thenAnswer(invocation -> { assertThat(Thread.currentThread().getName()) .isEqualTo("Worker " + randomOrdinal + " (UUID=" + workerUuid + ") on " + threadName); return Optional.of(createCeTask(submitter)); }); taskProcessorRepository.setProcessorForTask(CeTaskTypes.REPORT, taskProcessor); Thread newThread = createThreadNameVerifyingThread(threadName); newThread.start(); newThread.join(); }
/** MX bean which is always failing to respond with metrics */ @NotNull private MemoryMXBean createAlwaysFailingMxBean() { final Answer<MemoryUsage> failingAnswer = new Answer<MemoryUsage>() { @Override public MemoryUsage answer(InvocationOnMock invocationOnMock) throws Throwable { throw new IllegalArgumentException("java.lang.IllegalArgumentException: committed = 5274103808 should be < max = 5274095616"); } }; final MemoryMXBean memoryMXBean = Mockito.mock(MemoryMXBean.class); when(memoryMXBean.getHeapMemoryUsage()).thenAnswer(failingAnswer); when(memoryMXBean.getNonHeapMemoryUsage()).thenAnswer(failingAnswer); return memoryMXBean; }
@Before public void setUp() { MockitoAnnotations.initMocks(this); bitmapWidth = 100; bitmapHeight = 100; bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888); when(resource.get()).thenReturn(bitmap); when(pool.get(anyInt(), anyInt(), any(Bitmap.Config.class))) .thenAnswer(new Util.CreateBitmap()); context = RuntimeEnvironment.application; Glide.init(context, new GlideBuilder().setBitmapPool(pool)); centerCrop = new CenterCrop(); }
@Before public void setUp() { MockitoAnnotations.initMocks(this); doAnswer(new AddBitmapPoolAnswer(addedBitmaps)).when(pool).put(any(Bitmap.class)); when(pool.getDirty(anyInt(), anyInt(), any(Bitmap.Config.class))) .thenAnswer(new CreateBitmap()); when(cache.put(any(Key.class), anyResource())) .thenAnswer(new AddBitmapCacheAnswer(addedBitmaps)); }