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

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

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

Mockito.only介绍

[英]Allows checking if given method was the only one invoked. E.g:

verify(mock, only()).someMethod(); 
//above is a shorthand for following 2 lines of code: 
verify(mock).someMethod(); 
verifyNoMoreInvocations(mock);

See also Mockito#verifyNoMoreInteractions(Object...)

See examples in javadoc for Mockito class
[中]允许检查给定的方法是否是唯一调用的方法。例如:

verify(mock, only()).someMethod(); 
//above is a shorthand for following 2 lines of code: 
verify(mock).someMethod(); 
verifyNoMoreInvocations(mock);

另请参见Mockito#verifyNoMoreInteractions(对象…)
有关Mockito类,请参见javadoc中的示例

代码示例

代码示例来源:origin: reactor/reactor-core

@Test
public void monoLogWithGivenLogger() {
  Level level = Level.WARNING;
  Mono<String> source = Mono.just("foo");
  Logger mockLogger = Mockito.mock(Logger.class);
  source.log(mockLogger, level, false, SignalType.ON_NEXT)
     .subscribe();
  verify(mockLogger, only()).warn(anyString(), eq(SignalType.ON_NEXT),
      eq("foo"));
  verifyNoMoreInteractions(mockLogger);
}

代码示例来源:origin: line/armeria

@Test
public void transferSuccess() {
  @SuppressWarnings("unchecked")
  final AsyncMethodCallback<String> callback = mock(AsyncMethodCallback.class);
  AsyncMethodCallbacks.transfer(completedFuture("foo"), callback);
  verify(callback, only()).onComplete("foo");
}

代码示例来源:origin: nisrulz/sensey

@Test
public void detectOnSwipeLeft() {
  MotionEvent ev1 = MotionEvent.obtain(10, 10, 0, 300, 50, 0);
  MotionEvent ev2 = MotionEvent.obtain(10, 10, 0, 100, 60, 0);
  testTouchTypeDetector.gestureListener.onFling(ev1, ev2, 201, 201);
  verify(mockListener, only()).onSwipe(SWIPE_DIR_LEFT);
}

代码示例来源:origin: reactor/reactor-core

@Test
public void fluxLogWithGivenLogger() {
  Level level = Level.WARNING;
  Flux<String> source = Flux.just("foo");
  Logger mockLogger = Mockito.mock(Logger.class);
  source.log(mockLogger, level, false, SignalType.ON_NEXT)
     .subscribe();
  verify(mockLogger, only()).warn(anyString(), eq(SignalType.ON_NEXT),
      eq("foo"));
  verifyNoMoreInteractions(mockLogger);
}

代码示例来源:origin: line/armeria

@Test
public void transferFailure_Throwable() {
  @SuppressWarnings("unchecked")
  final AsyncMethodCallback<String> callback = mock(AsyncMethodCallback.class);
  AsyncMethodCallbacks.transfer(exceptionallyCompletedFuture(new Throwable("foo")), callback);
  verify(callback, only()).onError(argThat(argument -> {
    return argument instanceof CompletionException &&
        "foo".equals(argument.getCause().getMessage());
  }));
}

代码示例来源:origin: line/armeria

@Test
public void execute() throws Exception {
  final HelloService.Iface client = helloClient(retryOnException, 100);
  when(serviceHandler.hello(anyString())).thenReturn("world");
  assertThat(client.hello("hello")).isEqualTo("world");
  verify(serviceHandler, only()).hello("hello");
}

代码示例来源:origin: line/armeria

@Test
  public void transferCallbackError() {
    @SuppressWarnings("unchecked")
    final AsyncMethodCallback<String> callback = mock(AsyncMethodCallback.class);
    doThrow(new AnticipatedException()).when(callback).onComplete(any());
    AsyncMethodCallbacks.transfer(completedFuture("foo"), callback);
    verify(callback, only()).onComplete("foo");
  }
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void shouldPersist() {
 Notification notification = new Notification("test");
 underTest.scheduleForSending(notification);
 verify(notificationQueueDao, only()).insert(any(List.class));
}

代码示例来源:origin: line/armeria

@Test
public void propagateLastResponseWhenNextRetryIsAfterTimeout() throws Exception {
  final RetryStrategyWithContent<RpcResponse> strategy =
      (ctx, response) -> CompletableFuture.completedFuture(Backoff.fixed(10000000));
  final HelloService.Iface client = helloClient(strategy, 100);
  when(serviceHandler.hello(anyString())).thenThrow(new IllegalArgumentException());
  final Throwable thrown = catchThrowable(() -> client.hello("hello"));
  assertThat(thrown).isInstanceOf(TApplicationException.class);
  assertThat(((TApplicationException) thrown).getType()).isEqualTo(TApplicationException.INTERNAL_ERROR);
  verify(serviceHandler, only()).hello("hello");
}

代码示例来源:origin: nisrulz/sensey

@Test
public void detectOnLongPress() {
  testTouchTypeDetector.gestureListener.onLongPress(null);
  verify(mockListener, only()).onLongPress();
}

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

@Test
public void shouldInvokeConsumerForEachKeyWithHighestPrecedenceOnce() throws URISyntaxException
{
  DiscoverableURIs discoverables =
      new DiscoverableURIs.Builder()
          .add( "a", "/test1", LOWEST )
          .add( "a", "/test2", LOW )
          .add( "a", "/data3", NORMAL )
          .add( "a", "/test4", HIGH )
          .add( "a", "/test5", HIGHEST )
          .build();
  discoverables.forEach( consumer );
  verify( consumer, only() ).accept( "a", new URI( "/test5" ) );
}

代码示例来源:origin: line/armeria

@Test
public void transferFailure_Exception() {
  @SuppressWarnings("unchecked")
  final AsyncMethodCallback<String> callback = mock(AsyncMethodCallback.class);
  AsyncMethodCallbacks.transfer(exceptionallyCompletedFuture(new AnticipatedException()), callback);
  verify(callback, only()).onError(isA(AnticipatedException.class));
}

代码示例来源:origin: nisrulz/sensey

@Test
public void detectShakeWithDoubleGravityForCustomThreshold() {
  testDetector(9, 1000).onSensorChanged(testAccelerometerEvent(new float[]{0, 0, 2 * 9.81f}));
  verify(mockListener, only()).onShakeDetected();
}

代码示例来源:origin: nisrulz/sensey

@Test
public void detectOnDarkWithLuxLessThanCustomThreshold() {
  testDetector(9).onSensorChanged(testLightEvent(new float[]{3}));
  verify(mockListener, only()).onDark();
}

代码示例来源:origin: nisrulz/sensey

@Test
public void detectOnLightWithExtraValues() {
  testDetector().onSensorChanged(testLightEvent(new float[]{10, 0, 43, 3, -423}));
  verify(mockListener, only()).onLight();
}

代码示例来源:origin: nisrulz/sensey

@Test
public void detectShakeWithDoubleGravity() {
  testDetector().onSensorChanged(testAccelerometerEvent(new float[]{0, 0, 2 * 9.81f}));
  verify(mockListener, only()).onShakeDetected();
}

代码示例来源:origin: nisrulz/sensey

@Test
public void detectOnLightWithLuxEqualsToCustomThreshold() {
  testDetector(9).onSensorChanged(testLightEvent(new float[]{9}));
  verify(mockListener, only()).onLight();
}

代码示例来源:origin: nisrulz/sensey

@Test
public void detectLeftSideUp() {
  testOrientationDetector.onSensorChanged(
      testAccelerometerEvent(new float[]{-9.81f, 0, -9.81f}));
  testOrientationDetector.onSensorChanged(testMagneticEvent(new float[]{0, 0, 1}));
  verify(mockListener, only()).onLeftSideUp();
}

代码示例来源:origin: nisrulz/sensey

@Test
public void detectBottomSideUp() {
  testOrientationDetector.onSensorChanged(
      testAccelerometerEvent(new float[]{-9.81f, -9.81f, -9.81f}));
  testOrientationDetector.onSensorChanged(testMagneticEvent(new float[]{0, 0, 1}));
  verify(mockListener, only()).onBottomSideUp();
}

代码示例来源:origin: nisrulz/sensey

@Test
public void detectTopSideUp() {
  testOrientationDetector.onSensorChanged(
      testAccelerometerEvent(new float[]{9.81f, 9.81f, 9.81f}));
  testOrientationDetector.onSensorChanged(testMagneticEvent(new float[]{0, 0, 1}));
  verify(mockListener, only()).onTopSideUp();
}

相关文章