org.mockito.MockSettings.extraInterfaces()方法的使用及代码示例

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

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

MockSettings.extraInterfaces介绍

[英]Specifies extra interfaces the mock should implement. Might be useful for legacy code or some corner cases. For background, see issue 51 here

This mysterious feature should be used very occasionally. The object under test should know exactly its collaborators & dependencies. If you happen to use it often than please make sure you are really producing simple, clean & readable code.

Examples:

Foo foo = mock(Foo.class, withSettings().extraInterfaces(Bar.class, Baz.class)); 
//now, the mock implements extra interfaces, so following casting is possible: 
Bar bar = (Bar) foo; 
Baz baz = (Baz) foo;

[中]指定模拟应实现的额外接口。对于遗留代码或某些特殊情况可能很有用。背景资料见第51期{{0$}
这种神秘的功能应该偶尔使用。被测试对象应该确切地知道它的合作者和依赖关系。如果你碰巧经常使用它,请确保你真的产生了简单、干净和可读的代码。
示例:

Foo foo = mock(Foo.class, withSettings().extraInterfaces(Bar.class, Baz.class)); 
//now, the mock implements extra interfaces, so following casting is possible: 
Bar bar = (Bar) foo; 
Baz baz = (Baz) foo;

代码示例

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

@Test
public void proxyingWorksIfInfoReturnsNullEntityManagerInterface() {
  EntityManagerFactory emf = mock(EntityManagerFactory.class,
      withSettings().extraInterfaces(EntityManagerFactoryInfo.class));
  // EntityManagerFactoryInfo.getEntityManagerInterface returns null
  assertThat(SharedEntityManagerCreator.createSharedEntityManager(emf), is(notNullValue()));
}

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

public Object process(Mock annotation, Field field) {
    MockSettings mockSettings = Mockito.withSettings();
    if (annotation.extraInterfaces().length > 0) { // never null
      mockSettings.extraInterfaces(annotation.extraInterfaces());
    }
    if ("".equals(annotation.name())) {
      mockSettings.name(field.getName());
    } else {
      mockSettings.name(annotation.name());
    }

    // see @Mock answer default value
    mockSettings.defaultAnswer(annotation.answer().get());
    return Mockito.mock(field.getType(), mockSettings);
  }
}

代码示例来源:origin: CalebFenton/simplify

private static ExecutionGraphManipulator getMockedGraph(int address, HeapItem value) {
  ExecutionGraphManipulator manipulator = mock(ExecutionGraphManipulator.class);
  BuilderInstruction instruction =
      mock(BuilderInstruction.class, withSettings().extraInterfaces(OneRegisterInstruction.class));
  when(((OneRegisterInstruction) instruction).getRegisterA()).thenReturn(REGISTER);
  when(manipulator.getRegisterConsensus(address, REGISTER)).thenReturn(value);
  when(manipulator.getInstruction(address)).thenReturn(instruction);
  return manipulator;
}

代码示例来源:origin: CalebFenton/simplify

private BuilderInstruction buildInstruction22t(Opcode opcode, int offset) {
  BuilderInstruction instruction =
      mock(BuilderInstruction.class, withSettings().extraInterfaces(Instruction22t.class));
  when(location.getInstruction()).thenReturn(instruction);
  when(instruction.getLocation()).thenReturn(location);
  when(instruction.getCodeUnits()).thenReturn(0);
  when(instruction.getOpcode()).thenReturn(opcode);
  when(((Instruction22t) instruction).getRegisterA()).thenReturn(ARG1_REGISTER);
  when(((Instruction22t) instruction).getRegisterB()).thenReturn(ARG2_REGISTER);
  when(((Instruction22t) instruction).getCodeOffset()).thenReturn(offset);
  return instruction;
}

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

@Test
public void handlerMappingOrder() {
  HandlerMapping hm1 = mock(HandlerMapping.class, withSettings().extraInterfaces(Ordered.class));
  HandlerMapping hm2 = mock(HandlerMapping.class, withSettings().extraInterfaces(Ordered.class));
  when(((Ordered) hm1).getOrder()).thenReturn(1);
  when(((Ordered) hm2).getOrder()).thenReturn(2);
  when((hm1).getHandler(any())).thenReturn(Mono.just((Supplier<String>) () -> "1"));
  when((hm2).getHandler(any())).thenReturn(Mono.just((Supplier<String>) () -> "2"));
  StaticApplicationContext context = new StaticApplicationContext();
  context.registerBean("b2", HandlerMapping.class, () -> hm2);
  context.registerBean("b1", HandlerMapping.class, () -> hm1);
  context.registerBean(HandlerAdapter.class, SupplierHandlerAdapter::new);
  context.registerBean(HandlerResultHandler.class, StringHandlerResultHandler::new);
  context.refresh();
  DispatcherHandler dispatcherHandler = new DispatcherHandler(context);
  MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/"));
  dispatcherHandler.handle(exchange).block(Duration.ofSeconds(0));
  assertEquals("1", exchange.getResponse().getBodyAsString().block(Duration.ofSeconds(5)));
}

代码示例来源:origin: org.mockito/mockito-core

private MockSettings withSettingsUsing(GenericMetadataSupport returnTypeGenericMetadata, MockCreationSettings parentMockSettings) {
  MockSettings mockSettings = returnTypeGenericMetadata.hasRawExtraInterfaces() ?
      withSettings().extraInterfaces(returnTypeGenericMetadata.rawExtraInterfaces())
      : withSettings();
  return propagateSerializationSettings(mockSettings, parentMockSettings)
      .defaultAnswer(returnsDeepStubsAnswerUsing(returnTypeGenericMetadata));
}

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

@Test
public void localSettingsOverrideClientDefaultSettings() throws Exception {
  RequestConfig defaultConfig = RequestConfig.custom()
      .setConnectTimeout(1234).setConnectionRequestTimeout(6789).build();
  CloseableHttpClient client = mock(CloseableHttpClient.class,
      withSettings().extraInterfaces(Configurable.class));
  Configurable configurable = (Configurable) client;
  when(configurable.getConfig()).thenReturn(defaultConfig);
  HttpComponentsClientHttpRequestFactory hrf = new HttpComponentsClientHttpRequestFactory(client);
  hrf.setConnectTimeout(5000);
  RequestConfig requestConfig = retrieveRequestConfig(hrf);
  assertEquals(5000, requestConfig.getConnectTimeout());
  assertEquals(6789, requestConfig.getConnectionRequestTimeout());
  assertEquals(-1, requestConfig.getSocketTimeout());
}

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

@Test
public void defaultSettingsOfHttpClientMergedOnExecutorCustomization() throws Exception {
  RequestConfig defaultConfig = RequestConfig.custom().setConnectTimeout(1234).build();
  CloseableHttpClient client = mock(CloseableHttpClient.class,
      withSettings().extraInterfaces(Configurable.class));
  Configurable configurable = (Configurable) client;
  when(configurable.getConfig()).thenReturn(defaultConfig);
  HttpComponentsClientHttpRequestFactory hrf = new HttpComponentsClientHttpRequestFactory(client);
  assertSame("Default client configuration is expected", defaultConfig, retrieveRequestConfig(hrf));
  hrf.setConnectionRequestTimeout(4567);
  RequestConfig requestConfig = retrieveRequestConfig(hrf);
  assertNotNull(requestConfig);
  assertEquals(4567, requestConfig.getConnectionRequestTimeout());
  // Default connection timeout merged
  assertEquals(1234, requestConfig.getConnectTimeout());
}

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

@Test
public void localSettingsOverrideClientDefaultSettings() throws Exception {
  RequestConfig defaultConfig = RequestConfig.custom()
      .setConnectTimeout(1234).setConnectionRequestTimeout(6789).build();
  CloseableHttpClient client = mock(CloseableHttpClient.class,
      withSettings().extraInterfaces(Configurable.class));
  Configurable configurable = (Configurable) client;
  when(configurable.getConfig()).thenReturn(defaultConfig);
  HttpComponentsHttpInvokerRequestExecutor executor =
      new HttpComponentsHttpInvokerRequestExecutor(client);
  executor.setConnectTimeout(5000);
  HttpInvokerClientConfiguration config = mockHttpInvokerClientConfiguration("http://fake-service");
  HttpPost httpPost = executor.createHttpPost(config);
  RequestConfig requestConfig = httpPost.getConfig();
  assertEquals(5000, requestConfig.getConnectTimeout());
  assertEquals(6789, requestConfig.getConnectionRequestTimeout());
  assertEquals(-1, requestConfig.getSocketTimeout());
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldCreateRSA512AlgorithmWithPublicKey() throws Exception {
  RSAKey key = mock(RSAKey.class, withSettings().extraInterfaces(RSAPublicKey.class));
  Algorithm algorithm = Algorithm.RSA512(key);
  assertThat(algorithm, is(notNullValue()));
  assertThat(algorithm, is(instanceOf(RSAAlgorithm.class)));
  assertThat(algorithm.getDescription(), is("SHA512withRSA"));
  assertThat(algorithm.getName(), is("RS512"));
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldCreateRSA512AlgorithmWithPrivateKey() throws Exception {
  RSAKey key = mock(RSAKey.class, withSettings().extraInterfaces(RSAPrivateKey.class));
  Algorithm algorithm = Algorithm.RSA512(key);
  assertThat(algorithm, is(notNullValue()));
  assertThat(algorithm, is(instanceOf(RSAAlgorithm.class)));
  assertThat(algorithm.getDescription(), is("SHA512withRSA"));
  assertThat(algorithm.getName(), is("RS512"));
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldCreateECDSA384AlgorithmWithPublicKey() throws Exception {
  ECKey key = mock(ECKey.class, withSettings().extraInterfaces(ECPublicKey.class));
  Algorithm algorithm = Algorithm.ECDSA384(key);
  assertThat(algorithm, is(notNullValue()));
  assertThat(algorithm, is(instanceOf(ECDSAAlgorithm.class)));
  assertThat(algorithm.getDescription(), is("SHA384withECDSA"));
  assertThat(algorithm.getName(), is("ES384"));
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldCreateECDSA384AlgorithmWithPrivateKey() throws Exception {
  ECKey key = mock(ECKey.class, withSettings().extraInterfaces(ECPrivateKey.class));
  Algorithm algorithm = Algorithm.ECDSA384(key);
  assertThat(algorithm, is(notNullValue()));
  assertThat(algorithm, is(instanceOf(ECDSAAlgorithm.class)));
  assertThat(algorithm.getDescription(), is("SHA384withECDSA"));
  assertThat(algorithm.getName(), is("ES384"));
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldCreateECDSA512AlgorithmWithPrivateKey() throws Exception {
  ECKey key = mock(ECKey.class, withSettings().extraInterfaces(ECPrivateKey.class));
  Algorithm algorithm = Algorithm.ECDSA512(key);
  assertThat(algorithm, is(notNullValue()));
  assertThat(algorithm, is(instanceOf(ECDSAAlgorithm.class)));
  assertThat(algorithm.getDescription(), is("SHA512withECDSA"));
  assertThat(algorithm.getName(), is("ES512"));
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldCreateRSA256AlgorithmWithPublicKey() throws Exception {
  RSAKey key = mock(RSAKey.class, withSettings().extraInterfaces(RSAPublicKey.class));
  Algorithm algorithm = Algorithm.RSA256(key);
  assertThat(algorithm, is(notNullValue()));
  assertThat(algorithm, is(instanceOf(RSAAlgorithm.class)));
  assertThat(algorithm.getDescription(), is("SHA256withRSA"));
  assertThat(algorithm.getName(), is("RS256"));
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldCreateRSA384AlgorithmWithPrivateKey() throws Exception {
  RSAKey key = mock(RSAKey.class, withSettings().extraInterfaces(RSAPrivateKey.class));
  Algorithm algorithm = Algorithm.RSA384(key);
  assertThat(algorithm, is(notNullValue()));
  assertThat(algorithm, is(instanceOf(RSAAlgorithm.class)));
  assertThat(algorithm.getDescription(), is("SHA384withRSA"));
  assertThat(algorithm.getName(), is("RS384"));
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldCreateECDSA256AlgorithmWithPrivateKey() throws Exception {
  ECKey key = mock(ECKey.class, withSettings().extraInterfaces(ECPrivateKey.class));
  Algorithm algorithm = Algorithm.ECDSA256(key);
  assertThat(algorithm, is(notNullValue()));
  assertThat(algorithm, is(instanceOf(ECDSAAlgorithm.class)));
  assertThat(algorithm.getDescription(), is("SHA256withECDSA"));
  assertThat(algorithm.getName(), is("ES256"));
}

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

@Test
public void mergeBasedOnCurrentHttpClient() throws Exception {
  RequestConfig defaultConfig = RequestConfig.custom()
      .setSocketTimeout(1234).build();
  final CloseableHttpClient client = mock(CloseableHttpClient.class,
      withSettings().extraInterfaces(Configurable.class));
  Configurable configurable = (Configurable) client;
  when(configurable.getConfig()).thenReturn(defaultConfig);
  HttpComponentsClientHttpRequestFactory hrf = new HttpComponentsClientHttpRequestFactory() {
    @Override
    public HttpClient getHttpClient() {
      return client;
    }
  };
  hrf.setReadTimeout(5000);
  RequestConfig requestConfig = retrieveRequestConfig(hrf);
  assertEquals(-1, requestConfig.getConnectTimeout());
  assertEquals(-1, requestConfig.getConnectionRequestTimeout());
  assertEquals(5000, requestConfig.getSocketTimeout());
  // Update the Http client so that it returns an updated  config
  RequestConfig updatedDefaultConfig = RequestConfig.custom()
      .setConnectTimeout(1234).build();
  when(configurable.getConfig()).thenReturn(updatedDefaultConfig);
  hrf.setReadTimeout(7000);
  RequestConfig requestConfig2 = retrieveRequestConfig(hrf);
  assertEquals(1234, requestConfig2.getConnectTimeout());
  assertEquals(-1, requestConfig2.getConnectionRequestTimeout());
  assertEquals(7000, requestConfig2.getSocketTimeout());
}

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

@Test
public void defaultSettingsOfHttpClientMergedOnExecutorCustomization() throws IOException {
  RequestConfig defaultConfig = RequestConfig.custom().setConnectTimeout(1234).build();
  CloseableHttpClient client = mock(CloseableHttpClient.class,
      withSettings().extraInterfaces(Configurable.class));
  Configurable configurable = (Configurable) client;
  when(configurable.getConfig()).thenReturn(defaultConfig);
  HttpComponentsHttpInvokerRequestExecutor executor =
      new HttpComponentsHttpInvokerRequestExecutor(client);
  HttpInvokerClientConfiguration config = mockHttpInvokerClientConfiguration("http://fake-service");
  HttpPost httpPost = executor.createHttpPost(config);
  assertSame("Default client configuration is expected", defaultConfig, httpPost.getConfig());
  executor.setConnectionRequestTimeout(4567);
  HttpPost httpPost2 = executor.createHttpPost(config);
  assertNotNull(httpPost2.getConfig());
  assertEquals(4567, httpPost2.getConfig().getConnectionRequestTimeout());
  // Default connection timeout merged
  assertEquals(1234, httpPost2.getConfig().getConnectTimeout());
}

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

.setSocketTimeout(1234).build();
final CloseableHttpClient client = mock(CloseableHttpClient.class,
    withSettings().extraInterfaces(Configurable.class));
Configurable configurable = (Configurable) client;
when(configurable.getConfig()).thenReturn(defaultConfig);

相关文章