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

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

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

Mockito.verify介绍

[英]Verifies certain behavior happened once.

Alias to verify(mock, times(1)) E.g:

verify(mock).someMethod("some arg");

Above is equivalent to:

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

Arguments passed are compared using equals() method. Read about ArgumentCaptor or ArgumentMatcher to find out other ways of matching / asserting arguments passed.

Although it is possible to verify a stubbed invocation, usually it's just redundant. Let's say you've stubbed foo.bar(). If your code cares what foo.bar() returns then something else breaks(often before even verify() gets executed). If your code doesn't care what get(0) returns then it should not be stubbed. Not convinced? See here.

See examples in javadoc for Mockito class
[中]确认某些行为发生过一次。
verify(mock, times(1))的别名,例如:

verify(mock).someMethod("some arg");

以上等同于:

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

使用equals()方法比较传递的参数。阅读ArgumentCaptor或ArgumentMatcher,了解匹配/断言传递参数的其他方法。
虽然可以验证存根调用,但通常它只是冗余的。假设你已经存根foo.bar()。如果您的代码关心foo.bar()返回的内容,那么其他内容就会中断(通常在verify()执行之前)。如果您的代码不关心get(0)返回的内容,则不应将其存根。不相信?见here
有关Mockito类,请参见javadoc中的示例

代码示例

代码示例来源: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 unsubscribingWithSingleUnderlyingUnsubscribes() {
  Disposable underlying = mock(Disposable.class);
  serialDisposable.update(underlying);
  underlying.dispose();
  verify(underlying).dispose();
}

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

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

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

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

代码示例来源: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: 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 unsubscribingWithSingleUnderlyingUnsubscribes() {
  Disposable underlying = mock(Disposable.class);
  serialDisposable.set(underlying);
  underlying.dispose();
  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: 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

@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: 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 replacingFirstUnderlyingCausesUnsubscription() {
  Disposable first = mock(Disposable.class);
  serialDisposable.set(first);
  Disposable second = mock(Disposable.class);
  serialDisposable.set(second);
  verify(first).dispose();
}

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

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

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

@Test
public void testInitialResource() {
  Disposable r = mock(Disposable.class);
  AsyncSubscription as = new AsyncSubscription(r);
  as.cancel();
  verify(r).dispose();
}

相关文章