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

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

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

Mockito.times介绍

[英]Allows verifying exact number of invocations. E.g:

verify(mock, times(2)).someMethod("some arg");

See examples in javadoc for Mockito class
[中]允许验证调用的确切次数。例如:

verify(mock, times(2)).someMethod("some arg");

参见javadoc中Mockito类的示例

代码示例

代码示例来源: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: ReactiveX/RxJava

/**
 * This is testing a no-op path since it uses Schedulers.immediate() which will not do scheduling.
 */
@Test
public void testObserveOn() {
  Observer<Integer> observer = TestHelper.mockObserver();
  Observable.just(1, 2, 3).observeOn(ImmediateThinScheduler.INSTANCE).subscribe(observer);
  verify(observer, times(1)).onNext(1);
  verify(observer, times(1)).onNext(2);
  verify(observer, times(1)).onNext(3);
  verify(observer, times(1)).onComplete();
}

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

@Test  // SPR-13225
public void headerWithNullValue() {
  HttpURLConnection urlConnection = mock(HttpURLConnection.class);
  HttpHeaders headers = new HttpHeaders();
  headers.set("foo", null);
  SimpleBufferingClientHttpRequest.addHeaders(urlConnection, headers);
  verify(urlConnection, times(1)).addRequestProperty("foo", "");
}

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

@Test
public void testListIterable() {
  Observable<String> o = Observable.fromIterable(Arrays.<String> asList("one", "two", "three"));
  Observer<String> observer = TestHelper.mockObserver();
  o.subscribe(observer);
  verify(observer, times(1)).onNext("one");
  verify(observer, times(1)).onNext("two");
  verify(observer, times(1)).onNext("three");
  verify(observer, Mockito.never()).onError(any(Throwable.class));
  verify(observer, times(1)).onComplete();
}

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

public void testShutdownAndAwaitTermination_forcedShutDownInternal() throws Exception {
 ExecutorService service = mock(ExecutorService.class);
 when(service.awaitTermination(HALF_SECOND_NANOS, NANOSECONDS))
   .thenReturn(false)
   .thenReturn(true);
 when(service.isTerminated()).thenReturn(true);
 assertTrue(shutdownAndAwaitTermination(service, 1L, SECONDS));
 verify(service).shutdown();
 verify(service, times(2)).awaitTermination(HALF_SECOND_NANOS, NANOSECONDS);
 verify(service).shutdownNow();
}

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

@Test
public void testCast() {
  Observable<?> source = Observable.just(1, 2);
  Observable<Integer> observable = source.cast(Integer.class);
  Observer<Integer> observer = TestHelper.mockObserver();
  observable.subscribe(observer);
  verify(observer, times(1)).onNext(1);
  verify(observer, times(1)).onNext(1);
  verify(observer, never()).onError(
      any(Throwable.class));
  verify(observer, times(1)).onComplete();
}

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

public void testShutdownAndAwaitTermination_nonTerminationInternal() throws Exception {
 ExecutorService service = mock(ExecutorService.class);
 when(service.awaitTermination(HALF_SECOND_NANOS, NANOSECONDS))
   .thenReturn(false)
   .thenReturn(false);
 assertFalse(shutdownAndAwaitTermination(service, 1L, SECONDS));
 verify(service).shutdown();
 verify(service, times(2)).awaitTermination(HALF_SECOND_NANOS, NANOSECONDS);
 verify(service).shutdownNow();
}

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

@Test
public void testToMultimap() {
  Flowable<String> source = Flowable.just("a", "b", "cc", "dd");
  Single<Map<Integer, Collection<String>>> mapped = source.toMultimap(lengthFunc);
  Map<Integer, Collection<String>> expected = new HashMap<Integer, Collection<String>>();
  expected.put(1, Arrays.asList("a", "b"));
  expected.put(2, Arrays.asList("cc", "dd"));
  mapped.subscribe(singleObserver);
  verify(singleObserver, never()).onError(any(Throwable.class));
  verify(singleObserver, times(1)).onSuccess(expected);
}

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

@Test
public void sessionCompleted() {
  Runnable callback1 = Mockito.mock(Runnable.class);
  Runnable callback2 = Mockito.mock(Runnable.class);
  this.simpAttributes.registerDestructionCallback("name1", callback1);
  this.simpAttributes.registerDestructionCallback("name2", callback2);
  this.simpAttributes.sessionCompleted();
  verify(callback1, times(1)).run();
  verify(callback2, times(1)).run();
}

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

@Test
public void testFirstOrElseWithPredicateOfNoneMatchingThePredicate() {
  Observable<String> src = Observable.just("a", "b", "c");
  src.filter(IS_D).first("default").subscribe(wo);
  verify(wo, times(1)).onSuccess(anyString());
  verify(wo, times(1)).onSuccess("default");
  verify(wo, never()).onError(any(Throwable.class));
}

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

@Test
public void testHookCreate() throws Exception {
  CompletableSource subscriber = mock(CompletableSource.class);
  Completable create = Completable.unsafeCreate(subscriber);
  verify(onCreate, times(1)).apply(create);
}

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

@Test
public void testCombineLatest3TypesB() {
  Function3<String, Integer, int[], String> combineLatestFunction = getConcatStringIntegerIntArrayCombineLatestFunction();
  /* define an Observer to receive aggregated events */
  Observer<String> observer = TestHelper.mockObserver();
  Observable<String> w = Observable.combineLatest(Observable.just("one"), Observable.just(2), Observable.just(new int[] { 4, 5, 6 }, new int[] { 7, 8 }), combineLatestFunction);
  w.subscribe(observer);
  verify(observer, never()).onError(any(Throwable.class));
  verify(observer, times(1)).onComplete();
  verify(observer, times(1)).onNext("one2[4, 5, 6]");
  verify(observer, times(1)).onNext("one2[7, 8]");
}

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

@Test
@SuppressWarnings({"rawtypes", "unchecked"})
public void cancelInactivityTasks() throws Exception {
  TcpConnection<byte[]> tcpConnection = getTcpConnection();
  ScheduledFuture future = mock(ScheduledFuture.class);
  when(this.taskScheduler.scheduleWithFixedDelay(any(), eq(1L))).thenReturn(future);
  tcpConnection.onReadInactivity(mock(Runnable.class), 2L);
  tcpConnection.onWriteInactivity(mock(Runnable.class), 2L);
  this.webSocketHandlerCaptor.getValue().afterConnectionClosed(this.webSocketSession, CloseStatus.NORMAL);
  verify(future, times(2)).cancel(true);
  verifyNoMoreInteractions(future);
}

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

@Test
public void testSkipEverything() {
  Observable<Integer> src = Observable.just(1, 2, 3, 4, 3, 2, 1);
  src.skipWhile(LESS_THAN_FIVE).subscribe(w);
  verify(w, never()).onNext(anyInt());
  verify(w, never()).onError(any(Throwable.class));
  verify(w, times(1)).onComplete();
}

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

@Test
public void testSuccess() throws Exception {
  @SuppressWarnings("unchecked")
  Future<Object> future = mock(Future.class);
  Object value = new Object();
  when(future.get()).thenReturn(value);
  Observer<Object> o = TestHelper.mockObserver();
  TestObserver<Object> to = new TestObserver<Object>(o);
  Observable.fromFuture(future).subscribe(to);
  to.dispose();
  verify(o, times(1)).onNext(value);
  verify(o, times(1)).onComplete();
  verify(o, never()).onError(any(Throwable.class));
  verify(future, never()).cancel(true);
}

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

@Test
public void testDematerialize1() {
  Observable<Notification<Integer>> notifications = Observable.just(1, 2).materialize();
  Observable<Integer> dematerialize = notifications.dematerialize();
  Observer<Integer> observer = TestHelper.mockObserver();
  dematerialize.subscribe(observer);
  verify(observer, times(1)).onNext(1);
  verify(observer, times(1)).onNext(2);
  verify(observer, times(1)).onComplete();
  verify(observer, never()).onError(any(Throwable.class));
}

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

@Test
public void beanInstanceRetrievedAtEveryInvocation() {
  Method method = ReflectionUtils.findMethod(
      SampleEvents.class, "handleGenericString", GenericTestEvent.class);
  when(this.context.getBean("testBean")).thenReturn(this.sampleEvents);
  ApplicationListenerMethodAdapter listener = new ApplicationListenerMethodAdapter(
      "testBean", GenericTestEvent.class, method);
  listener.init(this.context, new EventExpressionEvaluator());
  GenericTestEvent<String> event = createGenericTestEvent("test");
  listener.onApplicationEvent(event);
  verify(this.sampleEvents, times(1)).handleGenericString(event);
  verify(this.context, times(1)).getBean("testBean");
  listener.onApplicationEvent(event);
  verify(this.sampleEvents, times(2)).handleGenericString(event);
  verify(this.context, times(2)).getBean("testBean");
}

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

@Test
public void testSkipTwoElements() {
  Observable<String> skip = Observable.just("one", "two", "three").skip(2);
  Observer<String> observer = TestHelper.mockObserver();
  skip.subscribe(observer);
  verify(observer, never()).onNext("one");
  verify(observer, never()).onNext("two");
  verify(observer, times(1)).onNext("three");
  verify(observer, never()).onError(any(Throwable.class));
  verify(observer, times(1)).onComplete();
}

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

@Test
public void testSuccess() throws Exception {
  @SuppressWarnings("unchecked")
  Future<Object> future = mock(Future.class);
  Object value = new Object();
  when(future.get()).thenReturn(value);
  Subscriber<Object> subscriber = TestHelper.mockSubscriber();
  TestSubscriber<Object> ts = new TestSubscriber<Object>(subscriber);
  Flowable.fromFuture(future).subscribe(ts);
  ts.dispose();
  verify(subscriber, times(1)).onNext(value);
  verify(subscriber, times(1)).onComplete();
  verify(subscriber, never()).onError(any(Throwable.class));
  verify(future, never()).cancel(anyBoolean());
}

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

@Test
public void testCountError() {
  Flowable<String> f = Flowable.error(new Callable<Throwable>() {
    @Override
    public Throwable call() {
      return new RuntimeException();
    }
  });
  f.count().subscribe(wo);
  verify(wo, never()).onSuccess(anyInt());
  verify(wo, times(1)).onError(any(RuntimeException.class));
}

相关文章