org.mockito.Answers类的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(9.1k)|赞(0)|评价(0)|浏览(492)

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

Answers介绍

[英]Enumeration of pre-configured mock answers

You can use it to pass extra parameters to @Mock annotation, see more info here: Mock

Example:

@Mock(answer = RETURNS_DEEP_STUBS) UserProvider userProvider;

This is not the full list of Answers available in Mockito. Some interesting answers can be found in org.mockito.stubbing.answers package.
[中]预配置模拟答案的枚举
您可以使用它将额外的参数传递给@Mock annotation,请参见此处的更多信息:Mock
例子:

@Mock(answer = RETURNS_DEEP_STUBS) UserProvider userProvider;

这不是Mockito中提供的完整答案列表。一些有趣的答案可以在org上找到。莫基托。存根。答案包。

代码示例

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

@Override
 public T apply(Object delegate) {
  T mock = mock(forwarderClass, CALLS_REAL_METHODS.get());
  try {
   T stubber = doReturn(delegate).when(mock);
   DELEGATE_METHOD.invoke(stubber);
  } catch (Exception e) {
   throw new RuntimeException(e);
  }
  return mock;
 }
});

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

public Object answer(InvocationOnMock invocation) throws Throwable {
  if (Modifier.isAbstract(invocation.getMethod().getModifiers())) {
    return RETURNS_DEFAULTS.answer(invocation);
  }
  return invocation.callRealMethod();
}

代码示例来源:origin: bumptech/glide

@Test
 public void testMissingPackageInfo() throws NameNotFoundException {
  Context context = mock(Context.class, Answers.RETURNS_DEEP_STUBS.get());
  String packageName = "my.package";
  when(context.getPackageName()).thenReturn(packageName);
  when(context.getPackageManager().getPackageInfo(packageName, 0)).thenReturn(null);

  Key key = ApplicationVersionSignature.obtain(context);

  assertNotNull(key);
 }
}

代码示例来源:origin: bumptech/glide

@Test
public void testUnresolvablePackageInfo() throws NameNotFoundException {
 Context context = mock(Context.class, Answers.RETURNS_DEEP_STUBS.get());
 String packageName = "my.package";
 when(context.getPackageName()).thenReturn(packageName);
 when(context.getPackageManager().getPackageInfo(packageName, 0))
   .thenThrow(new NameNotFoundException("test"));
 Key key = ApplicationVersionSignature.obtain(context);
 assertNotNull(key);
}

代码示例来源: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: org.powermock/powermock-api-mockito

Answers answers = mockAnnotation.answer();
if (answers != null) {
  mockSettings.defaultAnswer(answers.get());

代码示例来源:origin: mulesoft/mule

@Override
 public void configure(MuleContext muleContext) {
  if (muleContext.getExtensionManager() == null) {
   ExtensionManager mockExtensionManager = mock(ExtensionManager.class, Answers.RETURNS_DEEP_STUBS.get());
   when(mockExtensionManager.getExtensions()).thenReturn(emptySet());
   muleContext.setExtensionManager(mockExtensionManager);
  }
 }
}

代码示例来源:origin: mulesoft/mule

public static ExecutionCallback<CoreEvent> getFailureTransactionCallback() throws Exception {
 return () -> {
  throw mock(MessagingException.class, RETURNS_MOCKS.get());
 };
}

代码示例来源:origin: mulesoft/mule

private PhaseAfterValidationBeforeFlow createPhaseAfterValidation() {
 return mock(PhaseAfterValidationBeforeFlow.class, Answers.RETURNS_DEEP_STUBS.get());
}

代码示例来源:origin: mulesoft/mule

protected QueueStore createQueueWithCapacity(int capacity) {
 MuleContext mockMuleContext = mock(MuleContext.class, Answers.RETURNS_DEEP_STUBS.get());
 when(mockMuleContext.getConfiguration().getWorkingDirectory()).thenReturn(temporaryFolder.getRoot().getAbsolutePath());
 when(mockMuleContext.getExecutionClassLoader()).thenReturn(muleContext.getExecutionClassLoader());
 when(mockMuleContext.getObjectSerializer()).thenReturn(muleContext.getObjectSerializer());
 QueueStore queue = createQueueInfoDelegate(capacity, mockMuleContext);
 return queue;
}

代码示例来源:origin: mulesoft/mule

private MessageProcessPhase createNotSupportedPhase() {
 MessageProcessPhase notSupportedPhase = mock(MessageProcessPhase.class, Answers.RETURNS_DEEP_STUBS.get());
 when(notSupportedPhase.supportsTemplate(any(MessageProcessTemplate.class))).thenReturn(false);
 return notSupportedPhase;
}

代码示例来源:origin: mulesoft/mule

public static void stubRegistryKeys(MuleContext muleContext, final String... keys) {
 when(((MuleContextWithRegistry) muleContext).getRegistry().get(anyString())).thenAnswer(invocation -> {
  String name = (String) invocation.getArguments()[0];
  if (name != null) {
   for (String key : keys) {
    if (name.contains(key)) {
     return null;
    }
   }
  }
  return RETURNS_DEEP_STUBS.get().answer(invocation);
 });
}

代码示例来源:origin: mulesoft/mule

private MessageProcessPhase createFailureMessageProcessPhase() {
 FailureMessageProcessPhase failureMessageProcessPhase =
   mock(FailureMessageProcessPhase.class, Answers.RETURNS_DEEP_STUBS.get());
 when(failureMessageProcessPhase.supportsTemplate(any(MessageProcessTemplate.class))).thenCallRealMethod();
 doAnswer(invocationOnMock -> {
  PhaseResultNotifier phaseResultNotifier = (PhaseResultNotifier) invocationOnMock.getArguments()[2];
  phaseResultNotifier.phaseFailure(new DefaultMuleException("error"));
  return null;
 }).when(failureMessageProcessPhase).runPhase(any(MessageProcessTemplate.class), any(MessageProcessContext.class),
                        any(PhaseResultNotifier.class));
 return failureMessageProcessPhase;
}

代码示例来源:origin: inferred/FreeBuilder

@Test
public void testConstructor_avoidsEclipseWriterBug() throws IOException {
 // Due to a bug in Eclipse, we *must* call close on the object returned from openWriter().
 // Eclipse proxies a Writer but does not implement the fluent API correctly.
 // Here, we implement the fluent Writer API with the same bug:
 Writer mockWriter = Mockito.mock(Writer.class, (Answer<?>) invocation -> {
  if (Writer.class.isAssignableFrom(invocation.getMethod().getReturnType())) {
   // Erroneously return the delegate writer (matching the Eclipse bug!)
   return source;
  } else {
   return Answers.RETURNS_SMART_NULLS.get().answer(invocation);
  }
 });
 when(sourceFile.openWriter()).thenReturn(mockWriter);
 FilerUtils.writeCompilationUnit(filer, unit, originatingElement);
 verify(mockWriter).close();
}

代码示例来源:origin: mulesoft/mule

@Test
public void testOnNotificationWithLifecycleStateEnabledStarted() throws MuleException {
 mockStartableAndLifecycleStateEnabled = mock(StartableAndLifecycleStateEnabled.class, Answers.RETURNS_DEEP_STUBS.get());
 when(mockStartableAndLifecycleStateEnabled.getLifecycleState().isStarted()).thenReturn(true);
 this.notificationListener =
   new PrimaryNodeLifecycleNotificationListener(mockStartableAndLifecycleStateEnabled, notificationRegistrer);
 this.notificationListener.onNotification(mockServerNotification);
 verify(mockStartableAndLifecycleStateEnabled, times(1)).start();
}

代码示例来源:origin: mulesoft/mule

@Test
public void testOnNotificationWithLifecycleStateEnabledStopped() throws MuleException {
 mockStartableAndLifecycleStateEnabled = mock(StartableAndLifecycleStateEnabled.class, Answers.RETURNS_DEEP_STUBS.get());
 when(mockStartableAndLifecycleStateEnabled.getLifecycleState().isStarted()).thenReturn(false);
 this.notificationListener =
   new PrimaryNodeLifecycleNotificationListener(mockStartableAndLifecycleStateEnabled, notificationRegistrer);
 this.notificationListener.onNotification(mockServerNotification);
 verify(mockStartableAndLifecycleStateEnabled, times(0)).start();
}

代码示例来源:origin: mulesoft/mule

private void lockUnlockThenDestroy(int lockTimes) {
 mockLockProvider = Mockito.mock(LockProvider.class, Answers.RETURNS_DEEP_STUBS.get());
 InstanceLockGroup instanceLockGroup = new InstanceLockGroup(mockLockProvider);
 for (int i = 0; i < lockTimes; i++) {
  instanceLockGroup.lock("lockId");
 }
 instanceLockGroup.unlock("lockId");
 Mockito.verify(mockLockProvider, VerificationModeFactory.times(1)).createLock("lockId");
}

代码示例来源:origin: mulesoft/mule

@Override
protected void doSetUp() throws Exception {
 MuleApplicationClassLoader parentArtifactClassLoader = mock(MuleApplicationClassLoader.class);
 mockArtifactContext = mock(ArtifactContext.class);
 when(mockArtifactContext.getMuleContext()).thenReturn(muleContext);
 when(mockArtifactContext.getRegistry()).thenReturn(new DefaultRegistry(muleContext));
 ApplicationDescriptor applicationDescriptorMock = mock(ApplicationDescriptor.class, RETURNS_DEEP_STUBS.get());
 when(applicationDescriptorMock.getClassLoaderModel())
   .thenReturn(new ClassLoaderModel.ClassLoaderModelBuilder().containing(new URL("file:/target/classes")).build());
 application = new DefaultMuleApplication(applicationDescriptorMock, parentArtifactClassLoader, emptyList(),
                      null, mock(ServiceRepository.class),
                      mock(ExtensionModelLoaderRepository.class),
                      appLocation, null, null,
                      getRuntimeComponentBuildingDefinitionProvider());
 application.setArtifactContext(mockArtifactContext);
 muleContext.getInjector().inject(this);
}

代码示例来源:origin: mulesoft/mule

RETURNS_DEEP_STUBS.get()))));
mockParameters(extension1ConfigurationModel);
mockConfigurationInstance(extension1ConfigurationModel, configInstance);

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

private void should_invoke_netty_options_hooks(int hosts, int coreConnections) throws Exception {
 NettyOptions nettyOptions = mock(NettyOptions.class, CALLS_REAL_METHODS.get());
 EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
 Timer timer = new HashedWheelTimer();

相关文章

Answers类方法