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

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

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

Mockito.inOrder介绍

[英]Creates org.mockito.InOrder object that allows verifying mocks in order.

InOrder inOrder = inOrder(firstMock, secondMock); 
inOrder.verify(firstMock).add("was called first"); 
inOrder.verify(secondMock).add("was called second");

Verification in order is flexible - you don't have to verify all interactions one-by-one but only those that you are interested in testing in order.

Also, you can create InOrder object passing only mocks that are relevant for in-order verification.

InOrder verification is 'greedy'. You will hardly every notice it but if you want to find out more search for 'greedy' on the Mockito wiki pages.

As of Mockito 1.8.4 you can verifyNoMoreInvocations() in order-sensitive way. Read more: InOrder#verifyNoMoreInteractions()

See examples in javadoc for Mockito class
[中]创建组织。莫基托。允许按顺序验证模拟的InOrder对象

InOrder inOrder = inOrder(firstMock, secondMock); 
inOrder.verify(firstMock).add("was called first"); 
inOrder.verify(secondMock).add("was called second");

按顺序验证是灵活的-您不必逐个验证所有交互,只需按顺序验证您感兴趣的交互。
此外,您还可以创建只传递与顺序验证相关的模拟的顺序对象。
InOrder验证为“贪婪”。你几乎不会注意到这一点,但是如果你想在Mockitowiki pages上找到更多的“贪婪”搜索。
从Mockito 1.8.4开始,您可以以顺序敏感的方式验证NomoreInvocations()。阅读更多:InOrder#verifyNoMoreInteractions()
有关Mockito类,请参见javadoc中的示例

代码示例

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

@Test
public void testFirstOrDefault() {
  Single<Integer> o = Observable.just(1, 2, 3)
      .first(4);
  o.subscribe(wo);
  InOrder inOrder = inOrder(wo);
  inOrder.verify(wo, times(1)).onSuccess(1);
  inOrder.verifyNoMoreInteractions();
}

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

public void testAddDelayedShutdownHook_success() throws InterruptedException {
 TestApplication application = new TestApplication();
 ExecutorService service = mock(ExecutorService.class);
 application.addDelayedShutdownHook(service, 2, TimeUnit.SECONDS);
 verify(service, Mockito.never()).shutdown();
 application.shutdown();
 InOrder shutdownFirst = Mockito.inOrder(service);
 shutdownFirst.verify(service).shutdown();
 shutdownFirst.verify(service).awaitTermination(2, TimeUnit.SECONDS);
}

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

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

代码示例来源:origin: neo4j/neo4j

@Test
public void shouldNotOpenExplicitTransactionForPeriodicCommitQuery() throws Exception
{
  KernelTransaction transaction = newTransaction();
  TransactionStateMachineV1SPI stateMachineSPI = newTransactionStateMachineSPI( transaction );
  when( stateMachineSPI.isPeriodicCommit( PERIODIC_COMMIT_QUERY ) ).thenReturn( true );
  TransactionStateMachine stateMachine = newTransactionStateMachine( stateMachineSPI );
  stateMachine.run( PERIODIC_COMMIT_QUERY, EMPTY_MAP );
  // transaction was created only to stream back result of the periodic commit query
  assertEquals( transaction, stateMachine.ctx.currentTransaction );
  InOrder inOrder = inOrder( stateMachineSPI );
  inOrder.verify( stateMachineSPI ).isPeriodicCommit( PERIODIC_COMMIT_QUERY );
  // periodic commit query was executed without starting an explicit transaction
  inOrder.verify( stateMachineSPI ).executeQuery( any( LoginContext.class ), eq( PERIODIC_COMMIT_QUERY ), eq( EMPTY_MAP ), any(), any() );
  // explicit transaction was started only after query execution to stream the result
  inOrder.verify( stateMachineSPI ).beginTransaction( any( LoginContext.class ), any(), any() );
}

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

@Test
  public void testLastOrDefaultWithOneElement() {
    Single<Integer> o = Observable.just(1).last(2);

    SingleObserver<Integer> observer = TestHelper.mockSingleObserver();
    o.subscribe(observer);

    InOrder inOrder = inOrder(observer);
    inOrder.verify(observer, times(1)).onSuccess(1);
//        inOrder.verify(observer, times(1)).onComplete();
    inOrder.verifyNoMoreInteractions();
  }

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

@Test
@Ignore("Null values no longer allowed")
public void testDistinctOfSourceWithExceptionsFromKeySelector() {
  Observable<String> src = Observable.just("a", "b", null, "c");
  src.distinct(TO_UPPER_WITH_EXCEPTION).subscribe(w);
  InOrder inOrder = inOrder(w);
  inOrder.verify(w, times(1)).onNext("a");
  inOrder.verify(w, times(1)).onNext("b");
  inOrder.verify(w, times(1)).onError(any(NullPointerException.class));
  inOrder.verify(w, never()).onNext(anyString());
  inOrder.verify(w, never()).onComplete();
}

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

public void testSequential() {
 @SuppressWarnings("unchecked")
 Funnel<Object> elementFunnel = mock(Funnel.class);
 PrimitiveSink primitiveSink = mock(PrimitiveSink.class);
 Funnel<Iterable<?>> sequential = Funnels.sequentialFunnel(elementFunnel);
 sequential.funnel(Arrays.asList("foo", "bar", "baz", "quux"), primitiveSink);
 InOrder inOrder = inOrder(elementFunnel);
 inOrder.verify(elementFunnel).funnel("foo", primitiveSink);
 inOrder.verify(elementFunnel).funnel("bar", primitiveSink);
 inOrder.verify(elementFunnel).funnel("baz", primitiveSink);
 inOrder.verify(elementFunnel).funnel("quux", primitiveSink);
}

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

@Test
public void testSingleOrDefault() {
  Single<Integer> o = Observable.just(1).single(2);
  SingleObserver<Integer> observer = TestHelper.mockSingleObserver();
  o.subscribe(observer);
  InOrder inOrder = inOrder(observer);
  inOrder.verify(observer, times(1)).onSuccess(1);
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void testSkipError() {
  Flowable<Integer> src = Flowable.just(1, 2, 42, 5, 3, 1);
  src.skipWhile(LESS_THAN_FIVE).subscribe(w);
  InOrder inOrder = inOrder(w);
  inOrder.verify(w, never()).onNext(anyInt());
  inOrder.verify(w, never()).onComplete();
  inOrder.verify(w, times(1)).onError(any(RuntimeException.class));
}

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

@Test
public void testFirstOrDefaultWithEmpty() {
  Single<Integer> single = Flowable.<Integer> empty()
      .first(1);
  single.subscribe(wo);
  InOrder inOrder = inOrder(wo);
  inOrder.verify(wo, times(1)).onSuccess(1);
  inOrder.verifyNoMoreInteractions();
}

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

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

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

@Test
public void testSingleObservable() {
  Observable<Integer> o = Observable.just(1).singleElement().toObservable();
  Observer<Integer> observer = TestHelper.mockObserver();
  o.subscribe(observer);
  InOrder inOrder = inOrder(observer);
  inOrder.verify(observer, times(1)).onNext(1);
  inOrder.verify(observer, times(1)).onComplete();
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void testSkipAndCountBuffersWithGaps() {
  Observable<String> source = Observable.just("one", "two", "three", "four", "five");
  Observable<List<String>> buffered = source.buffer(2, 3);
  buffered.subscribe(observer);
  InOrder inOrder = Mockito.inOrder(observer);
  inOrder.verify(observer, Mockito.times(1)).onNext(list("one", "two"));
  inOrder.verify(observer, Mockito.times(1)).onNext(list("four", "five"));
  inOrder.verify(observer, Mockito.never()).onNext(Mockito.<String>anyList());
  inOrder.verify(observer, Mockito.never()).onError(Mockito.any(Throwable.class));
  inOrder.verify(observer, Mockito.times(1)).onComplete();
}

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

@Test
public void testSingleOrDefaultWithEmpty() {
  Single<Integer> o = Observable.<Integer> empty()
      .single(1);
  SingleObserver<Integer> observer = TestHelper.mockSingleObserver();
  o.subscribe(observer);
  InOrder inOrder = inOrder(observer);
  inOrder.verify(observer, times(1)).onSuccess(1);
  inOrder.verifyNoMoreInteractions();
}

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

@Test
public void testSkipWithIndex() {
  Flowable<Integer> src = Flowable.just(1, 2, 3, 4, 5);
  src.skipWhile(INDEX_LESS_THAN_THREE).subscribe(w);
  InOrder inOrder = inOrder(w);
  inOrder.verify(w, times(1)).onNext(4);
  inOrder.verify(w, times(1)).onNext(5);
  inOrder.verify(w, times(1)).onComplete();
  inOrder.verify(w, never()).onError(any(Throwable.class));
}

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

@Test
  public void testLastOrDefault() {
    Single<Integer> single = Flowable.just(1, 2, 3)
        .last(4);

    SingleObserver<Integer> observer = TestHelper.mockSingleObserver();
    single.subscribe(observer);

    InOrder inOrder = inOrder(observer);
    inOrder.verify(observer, times(1)).onSuccess(3);
//        inOrder.verify(observer, times(1)).onComplete();
    inOrder.verifyNoMoreInteractions();
  }

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

@Test
public void testSkipAndCountGaplessBuffers() {
  Observable<String> source = Observable.just("one", "two", "three", "four", "five");
  Observable<List<String>> buffered = source.buffer(3, 3);
  buffered.subscribe(observer);
  InOrder inOrder = Mockito.inOrder(observer);
  inOrder.verify(observer, Mockito.times(1)).onNext(list("one", "two", "three"));
  inOrder.verify(observer, Mockito.times(1)).onNext(list("four", "five"));
  inOrder.verify(observer, Mockito.never()).onNext(Mockito.<String>anyList());
  inOrder.verify(observer, Mockito.never()).onError(Mockito.any(Throwable.class));
  inOrder.verify(observer, Mockito.times(1)).onComplete();
}

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

@Test
  public void testLastOrDefaultWithEmpty() {
    Single<Integer> single = Flowable.<Integer> empty()
        .last(1);

    SingleObserver<Integer> observer = TestHelper.mockSingleObserver();
    single.subscribe(observer);

    InOrder inOrder = inOrder(observer);
    inOrder.verify(observer, times(1)).onSuccess(1);
//        inOrder.verify(observer, times(1)).onComplete();
    inOrder.verifyNoMoreInteractions();
  }

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

@Test
public void testSkipLast1() {
  Flowable<String> flowable = Flowable.fromIterable(Arrays.asList("one", "two", "three")).skipLast(2);
  Subscriber<String> subscriber = TestHelper.mockSubscriber();
  InOrder inOrder = inOrder(subscriber);
  flowable.subscribe(subscriber);
  inOrder.verify(subscriber, never()).onNext("two");
  inOrder.verify(subscriber, never()).onNext("three");
  verify(subscriber, times(1)).onNext("one");
  verify(subscriber, never()).onError(any(Throwable.class));
  verify(subscriber, times(1)).onComplete();
}

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

@Test
  public void testLastWithOneElement() {
    Maybe<Integer> o = Observable.just(1).lastElement();

    MaybeObserver<Integer> observer = TestHelper.mockMaybeObserver();
    o.subscribe(observer);

    InOrder inOrder = inOrder(observer);
    inOrder.verify(observer, times(1)).onSuccess(1);
//        inOrder.verify(observer, times(1)).onComplete();
    inOrder.verifyNoMoreInteractions();
  }

相关文章