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

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

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

MockSettings.lenient介绍

[英]Lenient mocks bypass "strict stubbing" validation (see Strictness#STRICT_STUBS). When mock is declared as lenient none of its stubbings will be checked for potential stubbing problems such as 'unnecessary stubbing' ( UnnecessaryStubbingException) or for 'stubbing argument mismatch' PotentialStubbingProblem.

Foo mock = mock(Foo.class, withSettings.lenient());

For more information and an elaborate example, see Mockito#lenient().
[中]宽松的模拟绕过了“严格存根”验证(请参见严格性#严格存根)。当mock被宣布为宽松时,不会检查其存根是否存在潜在的存根问题,例如“不必要的存根”(unnecessary stubbing Exception)或“存根参数不匹配”潜在的存根问题

Foo mock = mock(Foo.class, withSettings.lenient());

有关详细信息和详细示例,请参阅Mockito#lenient()。

代码示例

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

public static Object processAnnotationForMock(Mock annotation, Class<?> type, String name) {
    MockSettings mockSettings = Mockito.withSettings();
    if (annotation.extraInterfaces().length > 0) { // never null
      mockSettings.extraInterfaces(annotation.extraInterfaces());
    }
    if ("".equals(annotation.name())) {
      mockSettings.name(name);
    } else {
      mockSettings.name(annotation.name());
    }
    if(annotation.serializable()){
      mockSettings.serializable();
    }
    if(annotation.stubOnly()){
      mockSettings.stubOnly();
    }
    if(annotation.lenient()){
      mockSettings.lenient();
    }

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

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

public static MuleContextWithRegistry mockMuleContext() {
 final MuleContextWithRegistry muleContext =
   mock(DefaultMuleContext.class,
      withSettings().defaultAnswer(RETURNS_DEEP_STUBS).extraInterfaces(PrivilegedMuleContext.class).lenient());
 when(muleContext.getUniqueIdString()).thenReturn(UUID.getUUID());
 when(muleContext.getDefaultErrorHandler(empty())).thenReturn(new OnErrorPropagateHandler());
 StreamingManager streamingManager = mock(StreamingManager.class, RETURNS_DEEP_STUBS);
 try {
  MuleRegistry registry = mock(MuleRegistry.class, withSettings().lenient());
  when(muleContext.getRegistry()).thenReturn(registry);
  ComponentInitialStateManager componentInitialStateManager =
    mock(ComponentInitialStateManager.class, withSettings().lenient());
  when(componentInitialStateManager.mustStartMessageSource(any())).thenReturn(true);
  when(registry.lookupObject(ComponentInitialStateManager.SERVICE_ID)).thenReturn(componentInitialStateManager);
  doReturn(streamingManager).when(registry).lookupObject(StreamingManager.class);
  doReturn(mock(NotificationDispatcher.class)).when(registry).lookupObject(NotificationDispatcher.class);
  doReturn(mock(ObjectStoreManager.class, RETURNS_DEEP_STUBS)).when(registry).lookupObject(OBJECT_STORE_MANAGER);
 } catch (RegistrationException e) {
  throw new RuntimeException(e);
 }
 return muleContext;
}

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

ErrorTypeRepository errorTypeRepository = mock(ErrorTypeRepository.class, withSettings().lenient());
when(muleContext.getErrorTypeRepository()).thenReturn(errorTypeRepository);
when(errorTypeRepository.getErrorType(any(ComponentIdentifier.class))).thenReturn(of(mock(ErrorType.class)));
ConfigurationProperties configProps = mock(ConfigurationProperties.class, withSettings().lenient());
when(configProps.resolveBooleanProperty(any())).thenReturn(empty());
  mock(ConfigurationComponentLocator.class, withSettings().lenient());
when(configurationComponentLocator.find(any(Location.class))).thenReturn(empty());
when(configurationComponentLocator.find(any(ComponentIdentifier.class))).thenReturn(emptyList());

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

public static ParameterGroupModel mockParameters(ParameterizedModel parameterizedModel, String groupName,
                         ParameterModel... parameterModels) {
 ParameterGroupModel group = mock(ParameterGroupModel.class, withSettings().lenient());
 when(group.getName()).thenReturn(groupName);
 when(group.getModelProperty(ParameterGroupModelProperty.class)).thenReturn(empty());
 when(parameterizedModel.getParameterGroupModels()).thenReturn(asList(group));
 when(group.getParameterModels()).thenReturn(asList(parameterModels));
 when(parameterizedModel.getAllParameterModels()).thenReturn(asList(parameterModels));
 return group;
}

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

private OutputModel mockOutputModel(Type type) {
 OutputModel om = mock(OutputModel.class, withSettings().lenient());
 when(om.getType()).thenReturn(loader.load(type));
 return om;
}

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

private static ParameterModel getParameter() {
 ParameterModel parameterModel = mock(ParameterModel.class, withSettings().lenient());
 when(parameterModel.getModelProperty(any())).thenReturn(Optional.empty());
 when(parameterModel.getDslConfiguration()).thenReturn(ParameterDslConfiguration.getDefaultInstance());
 when(parameterModel.getRole()).thenReturn(BEHAVIOUR);
 return parameterModel;
}

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

private OutputModel mockOutputModel(Type type) {
 OutputModel om = mock(OutputModel.class, withSettings().lenient());
 when(om.getType()).thenReturn(loader.load(type));
 return om;
}

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

private OperationModel mockOperationModel(OutputModel output, OutputModel attributes, ParameterModel... params) {
  OperationModel op = mock(OperationModel.class, withSettings().lenient());
  when(op.getOutput()).thenReturn(output);
  when(op.getOutputAttributes()).thenReturn(attributes);
  if (params != null) {
   mockParameters(op, params);
  }
  return op;
 }
}

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

private OperationModel mockOperationModel(OutputModel output, OutputModel attributes, ParameterModel... params) {
  OperationModel op = mock(OperationModel.class, withSettings().lenient());
  when(op.getOutput()).thenReturn(output);
  when(op.getOutputAttributes()).thenReturn(attributes);
  if (params != null) {
   mockParameters(op, params);
  }
  return op;
 }
}

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

@Test(expected = IllegalModelDefinitionException.class)
public void repeatedEncodingOperationArgumentObjectFields() {
 when(extensionModel.getOperationModels()).thenReturn(asList(operationModel));
 withMethod(operationModel, "argumentWithRepeatedEncodingFields");
 ParameterModel parameterModel = mock(ParameterModel.class, withSettings().lenient());
 when(parameterModel.getType()).thenReturn(toMetadataType(RepeatedEncoding.class));
 mockParameters(operationModel, parameterModel);
 validate(extensionModel, validator);
}

相关文章