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

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

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

Mockito.after介绍

[英]Verification will be triggered after given amount of millis, allowing testing of async code. Useful when interactions with the mock object did not happened yet. Extensive use of after() method can be a code smell - there are better ways of testing concurrent code.

Not yet implemented to work with InOrder verification.

See also #timeout(long) method for testing async code. Differences between timeout() and after() are explained below.

//passes after 100ms, if someMethod() has only been called once at that time. 
verify(mock, after(100)).someMethod(); 
//above is an alias to: 
verify(mock, after(100).times(1)).someMethod(); 
//passes if someMethod() is called *exactly* 2 times, as tested after 100 millis 
verify(mock, after(100).times(2)).someMethod(); 
//passes if someMethod() has not been called, as tested after 100 millis 
verify(mock, after(100).never()).someMethod(); 
//verifies someMethod() after a given time span using given verification mode 
//useful only if you have your own custom verification modes. 
verify(mock, new After(100, yourOwnVerificationMode)).someMethod();

timeout() vs. after()

  • timeout() exits immediately with success when verification passes
  • after() awaits full duration to check if verification passes
    Examples:
//1. 
mock.foo(); 
verify(mock, after(1000)).foo(); 
//waits 1000 millis and succeeds 
//2. 
mock.foo(); 
verify(mock, timeout(1000)).foo(); 
//succeeds immediately

See examples in javadoc for Mockito class
[中]验证将在给定的毫秒数后触发,允许测试异步代码。当与模拟对象的交互尚未发生时非常有用。广泛使用after()方法可能会让人感觉到代码的味道——有更好的方法来测试并发代码。
尚未实现以使用顺序验证。
另请参见测试异步代码的#超时(long)方法。timeout()和after()之间的区别解释如下。

//passes after 100ms, if someMethod() has only been called once at that time. 
verify(mock, after(100)).someMethod(); 
//above is an alias to: 
verify(mock, after(100).times(1)).someMethod(); 
//passes if someMethod() is called *exactly* 2 times, as tested after 100 millis 
verify(mock, after(100).times(2)).someMethod(); 
//passes if someMethod() has not been called, as tested after 100 millis 
verify(mock, after(100).never()).someMethod(); 
//verifies someMethod() after a given time span using given verification mode 
//useful only if you have your own custom verification modes. 
verify(mock, new After(100, yourOwnVerificationMode)).someMethod();

超时()与之后()
*验证通过后,timeout()立即退出并成功
*after()等待整个过程以检查验证是否通过
示例:

//1. 
mock.foo(); 
verify(mock, after(1000)).foo(); 
//waits 1000 millis and succeeds 
//2. 
mock.foo(); 
verify(mock, timeout(1000)).foo(); 
//succeeds immediately

请参阅javadoc中Mockito类的示例

代码示例

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

/**
 * Ensures that executing a query causing a schema change with a Cluster that has schema metadata
 * disabled will still wait on schema agreement, but not refresh the schema.
 *
 * @jira_ticket JAVA-858
 * @since 2.0.11
 */
@Test(groups = "short", dataProvider = "existingKeyspaceName")
public void should_not_refresh_schema_on_schema_change_response(String keyspace)
  throws InterruptedException {
 ResultSet rs = schemaDisabledSession.execute(String.format(CREATE_TABLE, keyspace));
 // Should still wait on schema agreement.
 assertThat(rs.getExecutionInfo().isSchemaInAgreement()).isTrue();
 assertThat(schemaDisabledCluster.getMetadata().checkSchemaAgreement()).isTrue();
 // Wait up to 1 second (since refreshSchema submitted in an executor) and check that
 // refreshSchema never called.
 verify(schemaDisabledControlConnection, after(1000).never())
   .refreshSchema(
     any(SchemaElement.class),
     any(String.class),
     any(String.class),
     anyListOf(String.class));
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

/**
 * Ensures that if the core connection pool is full that borrowConnection will create and use a
 * new connection.
 *
 * @jira_ticket JAVA-419
 * @test_category connection:connection_pool
 * @since 2.0.10, 2.1.6
 */
@Test(groups = "short")
public void should_add_extra_connection_when_core_full() throws Exception {
 Cluster cluster = createClusterBuilder().build();
 List<MockRequest> allRequests = newArrayList();
 try {
  HostConnectionPool pool = createPool(cluster, 1, 2);
  Connection.Factory factory = spy(cluster.manager.connectionFactory);
  cluster.manager.connectionFactory = factory;
  Connection core = pool.connections.get(0);
  // Fill core connection + 1
  List<MockRequest> requests = MockRequest.sendMany(NEW_CONNECTION_THRESHOLD, pool);
  assertBorrowedConnection(requests, core);
  allRequests.addAll(requests);
  allRequests.add(MockRequest.send(pool));
  // Reaching the threshold should have triggered the creation of an extra one
  verify(factory, after(2000).times(1)).open(any(HostConnectionPool.class));
  assertPoolSize(pool, 2);
 } finally {
  MockRequest.completeAll(allRequests);
  cluster.close();
 }
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

allRequests.add(MockRequest.send(pool));
verify(factory, after(2000).times(1)).open(any(HostConnectionPool.class));
assertThat(pool.connections).hasSize(2);

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

verify(factory, after(2000).times(1)).open(any(HostConnectionPool.class));
assertPoolSize(pool, 2);

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

allRequests.add(MockRequest.send(pool));
verify(factory, after(2000).times(1)).open(any(HostConnectionPool.class));
assertPoolSize(pool, 2);
Connection connection2 = pool.connections.get(1);
verify(factory, after(2000).times(1)).open(any(HostConnectionPool.class));
assertPoolSize(pool, 2);

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

allRequests.add(MockRequest.send(pool));
verify(factory, after(2000).times(1)).open(any(HostConnectionPool.class));
assertPoolSize(pool, 2);
reset(factory);
allRequests.add(MockRequest.send(pool));
assertThat(connection2.inFlight.get()).isEqualTo(101);
verify(factory, after(2000).times(1)).open(any(HostConnectionPool.class));
assertPoolSize(pool, 2);

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

allRequests.add(MockRequest.send(pool));
verify(factory, after(2000).times(1)).open(any(HostConnectionPool.class));
assertThat(pool.connections).hasSize(2);

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

verify(factory, after((reconnectInterval + readTimeout) * 2).never())
   .open(any(HostConnectionPool.class));
} finally {

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

verify(factory, after(2000).times(1)).open(any(HostConnectionPool.class));
assertThat(pool.connections).hasSize(2);
verify(factory, after(readTimeout).never()).open(any(HostConnectionPool.class));

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

verify(factory, after(reconnectInterval * 2).atLeast(2)).open(host);

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

schemaDisabledCluster.getConfiguration().getQueryOptions().setMetadataEnabled(true);
 verify(schemaDisabledControlConnection, after(1000)).refreshSchema(null, null, null, null);
 verify(schemaDisabledControlConnection, after(1000).never())
   .refreshSchema(null, null, null, null);
} finally {

代码示例来源:origin: info.magnolia/magnolia-core

@Test
public void ignoreChangesOutsideObservedPath() throws Exception {
  // Given
  ObservationUtil.registerChangeListener(WEBSITE, "/a", listener);
  // When
  session.getNode("/b").addNode("b1", NodeTypes.ContentNode.NAME);
  session.save();
  // Then
  verify(callback, after(TIMEOUT).never()).processEvent(anyInt(), anyString());
}

代码示例来源:origin: info.magnolia/magnolia-core

@Test
public void ignoreChangesOutsideObservedPath() throws Exception {
  // GIVEN
  WorkspaceEventListenerRegistration.Handle handle = WorkspaceEventListenerRegistration.observe(WEBSITE, "/a", listener).register();
  // WHEN
  session.getNode("/b").addNode("b1", NodeTypes.ContentNode.NAME);
  session.save();
  // THEN
  verify(callback, after(TIMEOUT).never()).processEvent(anyInt(), anyString());
  // cleanup
  handle.unregister();
}

代码示例来源:origin: apache/james-project

@Test
default void registerShouldNotDispatchPastEventsInDistributedContext() throws Exception {
  MailboxListener listener = newListener();
  eventBus2().dispatch(EVENT, ImmutableSet.of(KEY_1)).block();
  eventBus().register(listener, KEY_1);
  verify(listener, after(FIVE_HUNDRED_MS.toMillis()).never())
    .event(any());
}

代码示例来源:origin: apache/james-project

@Test
default void registerShouldNotDispatchPastEvents() throws Exception {
  MailboxListener listener = newListener();
  eventBus().dispatch(EVENT, ImmutableSet.of(KEY_1)).block();
  eventBus().register(listener, KEY_1);
  verify(listener, after(FIVE_HUNDRED_MS.toMillis()).never())
    .event(any());
}

代码示例来源:origin: apache/james-project

@Test
default void unregisteredDistantListenersShouldNotBeNotified() throws Exception {
  MailboxListener mailboxListener = newListener();
  eventBus().register(mailboxListener, KEY_1).unregister();
  eventBus2().dispatch(EVENT, ImmutableSet.of(KEY_1)).block();
  verify(mailboxListener, after(FIVE_HUNDRED_MS.toMillis()).never())
    .event(any());
}

代码示例来源:origin: apache/james-project

@Test
default void unregisteredGroupListenerShouldNotReceiveEvents() throws Exception {
  MailboxListener listener = newListener();
  Registration registration = eventBus().register(listener, GROUP_A);
  registration.unregister();
  eventBus().dispatch(EVENT, NO_KEYS).block();
  verify(listener, after(FIVE_HUNDRED_MS.toMillis()).never())
    .event(any());
}

代码示例来源:origin: apache/james-project

@Test
default void dispatchShouldNotifyOnlyRegisteredListener() throws Exception {
  MailboxListener listener = newListener();
  MailboxListener listener2 = newListener();
  eventBus().register(listener, KEY_1);
  eventBus().register(listener2, KEY_2);
  eventBus().dispatch(EVENT, ImmutableSet.of(KEY_1)).block();
  verify(listener, timeout(ONE_SECOND.toMillis()).times(1)).event(any());
  verify(listener2, after(FIVE_HUNDRED_MS.toMillis()).never())
    .event(any());
}

代码示例来源:origin: apache/james-project

@Test
default void groupListenersShouldNotReceiveNoopEvents() throws Exception {
  MailboxListener listener = newListener();
  eventBus().register(listener, GROUP_A);
  MailboxListener.Added noopEvent = new MailboxListener.Added(MailboxSession.SessionId.of(18), User.fromUsername("bob"), MailboxPath.forUser("bob", "mailbox"), TestId.of(58), ImmutableSortedMap.of(), Event.EventId.random());
  eventBus().dispatch(noopEvent, NO_KEYS).block();
  verify(listener, after(FIVE_HUNDRED_MS.toMillis()).never())
    .event(any());
}

代码示例来源:origin: apache/james-project

@Test
default void registeredListenersShouldNotReceiveNoopEvents() throws Exception {
  MailboxListener listener = newListener();
  eventBus().register(listener, KEY_1);
  MailboxListener.Added noopEvent = new MailboxListener.Added(MailboxSession.SessionId.of(18), User.fromUsername("bob"), MailboxPath.forUser("bob", "mailbox"), TestId.of(58), ImmutableSortedMap.of(), Event.EventId.random());
  eventBus().dispatch(noopEvent, KEY_1).block();
  verify(listener, after(FIVE_HUNDRED_MS.toMillis()).never())
    .event(any());
}

相关文章