@Test public void testReadBytes_whenReturnsLessThanZeroWithInvalidLength_doesNotThrow() throws IOException { InputStream is = ContentLengthInputStream.obtain(wrapped, "invalid_length"); when(wrapped.read(any(byte[].class), anyInt(), anyInt())).thenReturn(-1); //noinspection ResultOfMethodCallIgnored is.read(new byte[10], 0, 0); }
@Test public void load_afterResourceIsLoadedInActiveResources_returnsFromMemoryCache() { when(harness.resource.isCacheable()).thenReturn(true); doAnswer( new Answer<Object>() { @Override public Object answer(InvocationOnMock invocationOnMock) { harness.callOnEngineJobComplete(); return null; } }) .when(harness.job) .start(any(DecodeJob.class)); harness.doLoad(); harness.doLoad(); verify(harness.cb).onResourceReady(any(Resource.class), eq(DataSource.MEMORY_CACHE)); }
@Test public void testDeleteStatusOverride() throws Exception { when(instanceResource.deleteStatusUpdate(anyString(), anyString(), anyString())).thenReturn(Response.ok().build()); ReplicationInstance replicationInstance = newReplicationInstanceOf(Action.DeleteStatusOverride, instanceInfo); Response response = peerReplicationResource.batchReplication(new ReplicationList(replicationInstance)); assertStatusOkReply(response); verify(instanceResource, times(1)).deleteStatusUpdate( "true", replicationInstance.getStatus(), Long.toString(replicationInstance.getLastDirtyTimestamp()) ); }
static Resources mockResources(final String resValueString) { Resources resources = mock(Resources.class); doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); ((TypedValue) args[1]).string = resValueString; return null; } }).when(resources).getValue(anyInt(), any(TypedValue.class), anyBoolean()); return resources; }
@Test public void testMultipleRequestListeners() { getNullModelRequest().addListener(listener1).addListener(listener2).into(target); verify(requestManager).track(any(Target.class), requestCaptor.capture()); requestCaptor.getValue().onResourceReady(new SimpleResource<>(new Object()), DataSource.LOCAL); verify(listener1) .onResourceReady( any(), any(), isA(Target.class), isA(DataSource.class), anyBoolean()); verify(listener2) .onResourceReady( any(), any(), isA(Target.class), isA(DataSource.class), anyBoolean()); }
@Test public void decodeFrame_withTargetSizeOriginalWidthOnly_onApi27_doesNotThrow() throws IOException { Bitmap expected = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); when(retriever.getFrameAtTime(-1, MediaMetadataRetriever.OPTION_CLOSEST_SYNC)) .thenReturn(expected); verify(retriever, never()).getScaledFrameAtTime(anyLong(), anyInt(), anyInt(), anyInt()); assertThat(decoder.decode(resource, Target.SIZE_ORIGINAL, 100, options).get()) .isSameAs(expected); }
@Test public void testOpen() throws Exception { OutputFormat<String, Long> dummyOutputFormat = mock(DummyOutputFormat.class); DummyOutputCommitter outputCommitter = mock(DummyOutputCommitter.class); JobConf jobConf = Mockito.spy(new JobConf()); when(jobConf.getOutputCommitter()).thenReturn(outputCommitter); HadoopOutputFormat<String, Long> outputFormat = new HadoopOutputFormat<>(dummyOutputFormat, jobConf); outputFormat.open(1, 1); verify(jobConf, times(2)).getOutputCommitter(); verify(outputCommitter, times(1)).setupJob(any(JobContext.class)); verify(dummyOutputFormat, times(1)).getRecordWriter(nullable(FileSystem.class), any(JobConf.class), anyString(), any(Progressable.class)); }
@SuppressWarnings("unchecked") private <T> ModelLoader<T, InputStream> mockStreamModelLoader(final Class<T> modelClass) { ModelLoader<T, InputStream> modelLoader = mock(ModelLoader.class); DataFetcher<InputStream> fetcher = mock(DataFetcher.class); try { doAnswer(new Util.CallDataReady<>(new ByteArrayInputStream(new byte[0]))).when(fetcher) .loadData(isA(Priority.class), isA(DataFetcher.DataCallback.class)); } catch (Exception e) { // Do nothing. } when(fetcher.getDataClass()).thenReturn(InputStream.class); when(modelLoader.buildLoadData(isA(modelClass), anyInt(), anyInt(), isA(Options.class))) .thenReturn(new ModelLoader.LoadData<>(mock(Key.class), fetcher)); when(modelLoader.handles(isA(modelClass))).thenReturn(true); return modelLoader; }
private OutputCommitter setupOutputCommitter(boolean needsTaskCommit) throws IOException { OutputCommitter outputCommitter = Mockito.mock(OutputCommitter.class); when(outputCommitter.needsTaskCommit(nullable(TaskAttemptContext.class))).thenReturn(needsTaskCommit); doNothing().when(outputCommitter).commitTask(any(TaskAttemptContext.class)); return outputCommitter; }
@Before public void setUp() throws Exception { reset(requestHandler); when(requestHandler.register(any(InstanceInfo.class))).thenReturn(EurekaHttpResponse.status(204)); when(requestHandler.cancel(anyString(), anyString())).thenReturn(EurekaHttpResponse.status(200)); when(requestHandler.getDelta()).thenReturn( anEurekaHttpResponse(200, new Applications()).type(MediaType.APPLICATION_JSON_TYPE).build() ); }
@Test public void testPreFillHandlerDoesNotPostIfHasBitmapsButIsCancelled() { PreFillType size = new PreFillType.Builder(1).setConfig(Bitmap.Config.ARGB_8888).build(); Map<PreFillType, Integer> allocationOrder = new HashMap<>(); allocationOrder.put(size, 2); BitmapPreFillRunner handler = getHandler(allocationOrder); when(clock.now()).thenReturn(0L).thenReturn(0L).thenReturn(BitmapPreFillRunner.MAX_DURATION_MS); handler.cancel(); handler.run(); verify(mainHandler, never()).postDelayed(any(Runnable.class), anyLong()); }
@Test public void testAppliesTransformationsInOrder() { final int width = 584; final int height = 768; MultiTransformation<Object> transformation = new MultiTransformation<>(first, second); when(first.transform(anyContext(), eq(initial), eq(width), eq(height))) .thenReturn(firstTransformed); when(second.transform(anyContext(), eq(firstTransformed), eq(width), eq(height))) .thenReturn(secondTransformed); assertEquals(secondTransformed, transformation.transform(context, initial, width, height)); }
private RequestBuilder<Object> getNullModelRequest() { when(glideContext.buildImageViewTarget(isA(ImageView.class), isA(Class.class))) .thenReturn(mock(ViewTarget.class)); when(glideContext.getDefaultRequestOptions()).thenReturn(new RequestOptions()); when(requestManager.getDefaultRequestOptions()) .thenReturn(new RequestOptions()); when(requestManager.getDefaultTransitionOptions(any(Class.class))) .thenReturn(new GenericTransitionOptions<>()); return new RequestBuilder<>(glide, requestManager, Object.class, context) .load((Object) null); } }
private Dispatcher createDispatcher(ExecutorService service, boolean scansNetworkChanges) { when(connectivityManager.getActiveNetworkInfo()).thenReturn(scansNetworkChanges ? mock(NetworkInfo.class) : null); when(context.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(connectivityManager); when(context.checkCallingOrSelfPermission(anyString())).thenReturn( scansNetworkChanges ? PERMISSION_GRANTED : PERMISSION_DENIED); return new Dispatcher(context, service, new Handler(Looper.getMainLooper()), cache, stats); }
@SuppressWarnings("unchecked") @Before public void setUp() { MockitoAnnotations.initMocks(this); when(handler.obtainMessage(anyInt(), isA(DelayTarget.class))).thenReturn(mock(Message.class)); firstFrame = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); ByteBuffer byteBuffer = ByteBuffer.allocate(10); when(gifDecoder.getData()).thenReturn(byteBuffer); requestBuilder = mock(RequestBuilder.class, new ReturnsSelfAnswer()); loader = createGifFrameLoader(handler); }
@Test public void testEmptySingleResult() { when(filterServiceMock.singleResult(anyString(), any(Query.class))).thenReturn(null); given() .header(ACCEPT_JSON_HEADER) .pathParam("id", EXAMPLE_FILTER_ID) .then().expect() .statusCode(Status.NO_CONTENT.getStatusCode()) .when() .get(EXECUTE_SINGLE_RESULT_FILTER_URL); verify(filterServiceMock).singleResult(eq(EXAMPLE_FILTER_ID), isNull(Query.class)); }
@Test public void testReturnsNullIfFileDoesNotExist() throws FileNotFoundException { when(harness.service.get(anyString())).thenReturn(harness.file); when(harness.service.exists(eq(harness.file))).thenReturn(false); assertNull(harness.get().open(harness.uri)); }
@Test public void testWriteRecord() throws Exception { OutputFormat<String, Long> dummyOutputFormat = mock(DummyOutputFormat.class); DummyRecordWriter recordWriter = mock(DummyRecordWriter.class); JobConf jobConf = mock(JobConf.class); HadoopOutputFormat<String, Long> outputFormat = new HadoopOutputFormat<>(dummyOutputFormat, jobConf); outputFormat.recordWriter = recordWriter; outputFormat.writeRecord(new Tuple2<>("key", 1L)); verify(recordWriter, times(1)).write(anyString(), anyLong()); }