org.mockito.Mockito.mock()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(7.5k)|赞(0)|评价(0)|浏览(316)

本文整理了Java中org.mockito.Mockito.mock()方法的一些代码示例,展示了Mockito.mock()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Mockito.mock()方法的具体详情如下:
包路径:org.mockito.Mockito
类名称:Mockito
方法名:mock

Mockito.mock介绍

[英]Creates mock object of given class or interface.

See examples in javadoc for Mockito class
[中]创建给定类或接口的模拟对象。
有关Mockito类,请参见javadoc中的示例

代码示例

代码示例来源:origin: google/guava

@Override
protected void setUp() throws Exception {
 super.setUp();
 hasher = mock(Hasher.class);
 hashFunction = mock(HashFunction.class);
 when(hashFunction.newHasher()).thenReturn(hasher);
}

代码示例来源:origin: ReactiveX/RxJava

@SuppressWarnings("unchecked")
@Test
public void shouldNotInvokeFuncUntilSubscription() throws Exception {
  Callable<Object> func = mock(Callable.class);
  when(func.call()).thenReturn(new Object());
  Single<Object> fromCallableSingle = Single.fromCallable(func);
  verifyZeroInteractions(func);
  fromCallableSingle.subscribe();
  verify(func).call();
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void testUnsubscribeOnlyOnce() {
  Runnable dispose = mock(Runnable.class);
  Disposable subscription = Disposables.fromRunnable(dispose);
  subscription.dispose();
  subscription.dispose();
  verify(dispose, times(1)).run();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void entryEqualsSameInstance() {
  AnnotationMetadata metadata = mock(AnnotationMetadata.class);
  Group.Entry entry = new Group.Entry(metadata, "com.example.Test");
  assertEquals(entry, entry);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void interceptShouldAddHeader() throws Exception {
  SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
  ClientHttpRequest request = requestFactory.createRequest(new URI("http://example.com"), HttpMethod.GET);
  ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class);
  byte[] body = new byte[] {};
  new BasicAuthorizationInterceptor("spring", "boot").intercept(request, body,
      execution);
  verify(execution).execute(request, body);
  assertEquals("Basic c3ByaW5nOmJvb3Q=", request.getHeaders().getFirst("Authorization"));
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void testCancelledBeforeSubscribe() throws Exception {
  @SuppressWarnings("unchecked")
  Future<Object> future = mock(Future.class);
  CancellationException e = new CancellationException("unit test synthetic cancellation");
  when(future.get()).thenThrow(e);
  Observer<Object> o = TestHelper.mockObserver();
  TestObserver<Object> to = new TestObserver<Object>(o);
  to.dispose();
  Observable.fromFuture(future).subscribe(to);
  to.assertNoErrors();
  to.assertNotComplete();
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void settingSameDisposableTwiceDoesUnsubscribeIt() {
  Disposable underlying = mock(Disposable.class);
  serialDisposable.update(underlying);
  verifyZeroInteractions(underlying);
  serialDisposable.update(underlying);
  verify(underlying).dispose();
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void unsubscribingTwiceDoesUnsubscribeOnce() {
  Disposable underlying = mock(Disposable.class);
  serialDisposable.set(underlying);
  serialDisposable.dispose();
  verify(underlying).dispose();
  serialDisposable.dispose();
  verifyNoMoreInteractions(underlying);
}

代码示例来源:origin: spring-projects/spring-framework

private void supportsEventType(
    boolean match, Class<? extends ApplicationListener> listenerType, ResolvableType eventType) {
  ApplicationListener<?> listener = mock(listenerType);
  GenericApplicationListenerAdapter adapter = new GenericApplicationListenerAdapter(listener);
  assertEquals("Wrong match for event '" + eventType + "' on " + listenerType.getClass().getName(),
      match, adapter.supportsEventType(eventType));
}

代码示例来源:origin: google/guava

public void testAddDelayedShutdownHook_interrupted() throws InterruptedException {
 TestApplication application = new TestApplication();
 ExecutorService service = mock(ExecutorService.class);
 application.addDelayedShutdownHook(service, 2, TimeUnit.SECONDS);
 when(service.awaitTermination(2, TimeUnit.SECONDS)).thenThrow(new InterruptedException());
 application.shutdown();
 verify(service).shutdown();
}

代码示例来源:origin: ReactiveX/RxJava

@SuppressWarnings("unchecked")
@Test
public void shouldNotInvokeFuncUntilSubscription() throws Exception {
  Callable<Object> func = mock(Callable.class);
  when(func.call()).thenReturn(new Object());
  Observable<Object> fromCallableObservable = Observable.fromCallable(func);
  verifyZeroInteractions(func);
  fromCallableObservable.subscribe();
  verify(func).call();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void getCronTasks() {
  CronTask mockCronTask = mock(CronTask.class);
  List<CronTask> cronTaskList = Collections.singletonList(mockCronTask);
  this.taskRegistrar.setCronTasksList(cronTaskList);
  List<CronTask> retrievedList = this.taskRegistrar.getCronTaskList();
  assertEquals(1, retrievedList.size());
  assertEquals(mockCronTask, retrievedList.get(0));
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void replacingFirstUnderlyingCausesUnsubscription() {
  Disposable first = mock(Disposable.class);
  serialDisposable.set(first);
  Disposable second = mock(Disposable.class);
  serialDisposable.set(second);
  verify(first).dispose();
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void testCancelledBeforeSubscribe() throws Exception {
  @SuppressWarnings("unchecked")
  Future<Object> future = mock(Future.class);
  CancellationException e = new CancellationException("unit test synthetic cancellation");
  when(future.get()).thenThrow(e);
  Subscriber<Object> subscriber = TestHelper.mockSubscriber();
  TestSubscriber<Object> ts = new TestSubscriber<Object>(subscriber);
  ts.dispose();
  Flowable.fromFuture(future).subscribe(ts);
  ts.assertNoErrors();
  ts.assertNotComplete();
}

代码示例来源:origin: spring-projects/spring-framework

private void testSupports(MethodParameter returnType, boolean supports) {
  ViewResolutionResultHandler resultHandler = resultHandler(mock(ViewResolver.class));
  HandlerResult handlerResult = new HandlerResult(new Object(), null, returnType, this.bindingContext);
  assertEquals(supports, resultHandler.supports(handlerResult));
}

代码示例来源:origin: google/guava

@Override
protected void setUp() throws Exception {
 super.setUp();
 hasher = mock(Hasher.class);
 hashFunction = mock(HashFunction.class);
 buffer = new ByteArrayInputStream(testBytes);
 when(hashFunction.newHasher()).thenReturn(hasher);
}

代码示例来源:origin: ReactiveX/RxJava

@SuppressWarnings("unchecked")
@Test
public void shouldNotInvokeFuncUntilSubscription() throws Exception {
  Callable<Object> func = mock(Callable.class);
  when(func.call()).thenReturn(new Object());
  Flowable<Object> fromCallableFlowable = Flowable.fromCallable(func);
  verifyZeroInteractions(func);
  fromCallableFlowable.subscribe();
  verify(func).call();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void getFixedRateTasks() {
  IntervalTask mockFixedRateTask = mock(IntervalTask.class);
  List<IntervalTask> fixedRateTaskList = Collections.singletonList(mockFixedRateTask);
  this.taskRegistrar.setFixedRateTasksList(fixedRateTaskList);
  List<IntervalTask> retrievedList = this.taskRegistrar.getFixedRateTaskList();
  assertEquals(1, retrievedList.size());
  assertEquals(mockFixedRateTask, retrievedList.get(0));
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void unsubscribingWithSingleUnderlyingUnsubscribes() {
  Disposable underlying = mock(Disposable.class);
  serialDisposable.update(underlying);
  underlying.dispose();
  verify(underlying).dispose();
}

代码示例来源:origin: google/guava

@SuppressWarnings("unchecked")
@Override
protected void setUp() {
 backingMap = mock(ConcurrentMap.class);
 when(backingMap.isEmpty()).thenReturn(true);
 multiset = new ConcurrentHashMultiset<>(backingMap);
}

相关文章

微信公众号

最新文章

更多