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

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

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

MockSettings.name介绍

[英]Specifies mock name. Naming mocks can be helpful for debugging - the name is used in all verification errors.

Beware that naming mocks is not a solution for complex code which uses too many mocks or collaborators. If you have too many mocks then refactor the code so that it's easy to test/debug without necessity of naming mocks.

If you use @Mock annotation then you've got naming mocks for free! @Mock uses field name as mock name. Mock

Examples:

Foo foo = mock(Foo.class, withSettings().name("foo")); 
//Below does exactly the same: 
Foo foo = mock(Foo.class, "foo");

[中]指定模拟名称。命名mock有助于调试——所有验证错误都会使用该名称。
请注意,命名mock并不是使用太多mock或协作者的复杂代码的解决方案。如果有太多的mock,那么重构代码,以便在不需要命名mock的情况下轻松地进行测试/调试。
如果你使用@Mock annotation,那么你就可以免费命名Mock了@Mock使用字段名作为Mock名称。嘲弄
示例:

Foo foo = mock(Foo.class, withSettings().name("foo")); 
//Below does exactly the same: 
Foo foo = mock(Foo.class, "foo");

代码示例

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

private static Object spyInstance(Field field, Object instance) {
  return Mockito.mock(instance.getClass(),
            withSettings().spiedInstance(instance)
                            .defaultAnswer(CALLS_REAL_METHODS)
                            .name(field.getName()));
}

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

/**
 * Specifies mock name. Naming mocks can be helpful for debugging - the name is used in all verification errors. 
 * <p>
 * Beware that naming mocks is not a solution for complex code which uses too many mocks or collaborators. 
 * <b>If you have too many mocks then refactor the code</b> so that it's easy to test/debug without necessity of naming mocks.
 * <p>
 * <b>If you use <code>&#064;Mock</code> annotation then you've got naming mocks for free!</b> <code>&#064;Mock</code> uses field name as mock name. {@link Mock Read more.}
 * <p>
 * 
 * See examples in javadoc for {@link Mockito} class
 * 
 * @param classToMock class or interface to mock
 * @param name of the mock 
 * @return mock object
 */
public static <T> T mock(Class<T> classToMock, String name) {
  return mock(classToMock, withSettings()
      .name(name)
      .defaultAnswer(RETURNS_DEFAULTS));
}

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

/**
 * Specifies mock name. Naming mocks can be helpful for debugging - the name is used in all verification errors.
 * <p>
 * Beware that naming mocks is not a solution for complex code which uses too many mocks or collaborators.
 * <b>If you have too many mocks then refactor the code</b> so that it's easy to test/debug without necessity of naming mocks.
 * <p>
 * <b>If you use <code>&#064;Mock</code> annotation then you've got naming mocks for free!</b> <code>&#064;Mock</code> uses field name as mock name. {@link Mock Read more.}
 * <p>
 *
 * See examples in javadoc for {@link Mockito} class
 *
 * @param classToMock class or interface to mock
 * @param name of the mock
 * @return mock object
 */
@CheckReturnValue
public static <T> T mock(Class<T> classToMock, String name) {
  return mock(classToMock, withSettings()
      .name(name)
      .defaultAnswer(RETURNS_DEFAULTS));
}

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

private static Object spyNewInstance(Object testInstance, Field field)
    throws InstantiationException, IllegalAccessException, InvocationTargetException {
  MockSettings settings = withSettings().defaultAnswer(CALLS_REAL_METHODS)
                     .name(field.getName());
  Class<?> type = field.getType();
  if (type.isInterface()) {

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

@Override
  protected boolean processInjection(Field field, Object fieldOwner, Set<Object> mockCandidates) {
    FieldReader fieldReader = new FieldReader(fieldOwner, field);

    // TODO refoctor : code duplicated in SpyAnnotationEngine
    if(!fieldReader.isNull() && field.isAnnotationPresent(Spy.class)) {
      try {
        Object instance = fieldReader.read();
        if (MockUtil.isMock(instance)) {
          // A. instance has been spied earlier
          // B. protect against multiple use of MockitoAnnotations.initMocks()
          Mockito.reset(instance);
        } else {
          Object mock = Mockito.mock(instance.getClass(), withSettings()
            .spiedInstance(instance)
            .defaultAnswer(Mockito.CALLS_REAL_METHODS)
            .name(field.getName()));
          setField(fieldOwner, field, mock);
        }
      } catch (Exception e) {
        throw new MockitoException("Problems initiating spied field " + field.getName(), e);
      }
    }

    return false;
  }
}

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

.spiedInstance(instance)
.defaultAnswer(Mockito.CALLS_REAL_METHODS)
.name(field.getName())));

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

@Override
  protected boolean processInjection(Field field, Object fieldOwner, Set<Object> mockCandidates) {
    FieldReader fieldReader = new FieldReader(fieldOwner, field);

    // TODO refoctor : code duplicated in SpyAnnotationEngine
    if(!fieldReader.isNull() && field.isAnnotationPresent(Spy.class)) {
      try {
        Object instance = fieldReader.read();
        if (new MockUtil().isMock(instance)) {
          // A. instance has been spied earlier
          // B. protect against multiple use of MockitoAnnotations.initMocks()
          Mockito.reset(instance);
        } else {
          new FieldSetter(fieldOwner, field).set(
            Mockito.mock(instance.getClass(), withSettings()
              .spiedInstance(instance)
              .defaultAnswer(Mockito.CALLS_REAL_METHODS)
              .name(field.getName()))
          );
        }
      } catch (Exception e) {
        throw new MockitoException("Problems initiating spied field " + field.getName(), e);
      }
    }

    return false;
  }
}

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

mockSettings.name(name);

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

@SuppressWarnings("unchecked")
private MockCreationSettings<Class> createStaticMockSettings(final Class mock) {
  return Mockito.withSettings()
         .name(mock.getName())
         .build((Class<Class>) mock);
}

代码示例来源:origin: com.google.code.maven-play-plugin.com.google.code.eamelink-mockito/mockito-all

/**
 * Specifies mock name. Naming mocks can be helpful for debugging - the name is used in all verification errors. 
 * <p>
 * Beware that naming mocks is not a solution for complex code which uses too many mocks or collaborators. 
 * <b>If you have too many mocks then refactor the code</b> so that it's easy to test/debug without necessity of naming mocks.
 * <p>
 * <b>If you use &#064;Mock annotation then you've got naming mocks for free!</b> &#064;Mock uses field name as mock name. {@link Mock Read more.}
 * <p>
 * 
 * See examples in javadoc for {@link Mockito} class
 * 
 * @param classToMock class or interface to mock
 * @param name of the mock 
 * @return mock object
 */
public static <T> T mock(Class<T> classToMock, String name) {
  return mock(classToMock, withSettings()
      .name(name)
      .defaultAnswer(RETURNS_DEFAULTS));
}

代码示例来源:origin: com.googlecode.gwt-test-utils/gwt-test-utils

private Object createMock(Object testInstance, Field field) {
    try {
      Object instance = field.get(testInstance);
      if (MockUtil.isMock(instance)) {
        // instance has been spied earlier
        // for example happens when MockitoAnnotations.initMocks is called two times.
        Mockito.reset(instance);
        return instance;
      } else {
        return Mockito.mock(
            GwtMockitoUtils.getTypeToMock(field),
            withSettings().name(field.getName())
        );
      }
    } catch (IllegalAccessException e) {
      throw new MockitoException("Problems initiating spied field " + field.getName(), e);
    }
  }
}

代码示例来源:origin: gwt-test-utils/gwt-test-utils

private Object createMock(Object testInstance, Field field) {
    try {
      Object instance = field.get(testInstance);
      if (MockUtil.isMock(instance)) {
        // instance has been spied earlier
        // for example happens when MockitoAnnotations.initMocks is called two times.
        Mockito.reset(instance);
        return instance;
      } else {
        return Mockito.mock(
            GwtMockitoUtils.getTypeToMock(field),
            withSettings().name(field.getName())
        );
      }
    } catch (IllegalAccessException e) {
      throw new MockitoException("Problems initiating spied field " + field.getName(), e);
    }
  }
}

代码示例来源:origin: com.google.code.maven-play-plugin.com.google.code.eamelink-mockito/mockito-all

private Object processAnnotationOn(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.springframework.boot/spring-boot-test

@SuppressWarnings("unchecked")
public <T> T createMock(String name) {
  MockSettings settings = MockReset.withSettings(getReset());
  if (StringUtils.hasLength(name)) {
    settings.name(name);
  }
  if (!this.extraInterfaces.isEmpty()) {
    settings.extraInterfaces(ClassUtils.toClassArray(this.extraInterfaces));
  }
  settings.defaultAnswer(this.answer);
  if (this.serializable) {
    settings.serializable();
  }
  return (T) Mockito.mock(this.typeToMock.resolve(), settings);
}

代码示例来源:origin: org.springframework.boot/spring-boot-test

@SuppressWarnings("unchecked")
public <T> T createSpy(String name, Object instance) {
  Assert.notNull(instance, "Instance must not be null");
  Assert.isInstanceOf(this.typeToSpy.resolve(), instance);
  if (Mockito.mockingDetails(instance).isSpy()) {
    return (T) instance;
  }
  MockSettings settings = MockReset.withSettings(getReset());
  if (StringUtils.hasLength(name)) {
    settings.name(name);
  }
  settings.spiedInstance(instance);
  settings.defaultAnswer(Mockito.CALLS_REAL_METHODS);
  if (this.isProxyTargetAware()) {
    settings.verificationStartedListeners(
        new SpringAopBypassingVerificationStartedListener());
  }
  return (T) Mockito.mock(instance.getClass(), settings);
}

代码示例来源:origin: org.powermock/powermock-api-mockito2

private void standardInject(Object testInstance) throws IllegalAccessException {
  Set<Field> fields = Whitebox.getFieldsAnnotatedWith(testInstance, getMockAnnotations());
  for (Field field : fields) {
    if (field.get(testInstance) != null) {
      continue;
    }
    final Class<?> type = field.getType();
    if (field.isAnnotationPresent(org.mockito.Mock.class)) {
      org.mockito.Mock mockAnnotation = field.getAnnotation(org.mockito.Mock.class);
      MockSettings mockSettings = withSettings();
      Answers answers = mockAnnotation.answer();
      if (answers != null) {
        mockSettings.defaultAnswer(answers.get());
      }
      Class<?>[] extraInterfaces = mockAnnotation.extraInterfaces();
      if (extraInterfaces != null && extraInterfaces.length > 0) {
        mockSettings.extraInterfaces(extraInterfaces);
      }
      String name = mockAnnotation.name();
      if (name != null && name.length() > 0) {
        mockSettings.name(name);
      }
      field.set(testInstance, mock(type, mockSettings));
    } else {
      field.set(testInstance, mock(type));
    }
  }
}

代码示例来源:origin: name.falgout.jeffrey.testing.junit5/mockito-extension

@Override
 public Object getParameterValue(Parameter parameter) {
  Mock annotation = parameter.getAnnotation(Mock.class);
  MockSettings settings = Mockito.withSettings();
  if (annotation.extraInterfaces().length > 0) {
   settings.extraInterfaces(annotation.extraInterfaces());
  }
  if (annotation.serializable()) {
   settings.serializable();
  }
  settings.name(annotation.name().isEmpty() ? parameter.getName() : annotation.name());
  settings.defaultAnswer(annotation.answer());

  return Mockito.mock(parameter.getType(), settings);
 }
}

代码示例来源:origin: org.kubek2k/springockito-annotations

private MockSettings createMockSettings() {
  MockSettings mockSettings = new MockSettingsImpl();
  if (extraInterfaces != null && extraInterfaces.length > 0) {
    mockSettings.extraInterfaces(extraInterfaces);
  }
  if (defaultAnswer != null) {
    mockSettings.defaultAnswer(defaultAnswer.get());
  } else {
    mockSettings.defaultAnswer(Answers.RETURNS_DEFAULTS.get());
  }
  if (mockName != null) {
    mockSettings.name(mockName);
  }
  return mockSettings;
}

相关文章