org.mockito.exceptions.base.MockitoException类的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(10.7k)|赞(0)|评价(0)|浏览(1246)

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

MockitoException介绍

[英]Raised by mockito to emit an error either due to Mockito, or due to the User. All exception classes that inherit from this class will have the stack trace filtered. Filtering removes Mockito internal stack trace elements to provide clean stack traces and improve productivity.

The stack trace is filtered from mockito calls if you are using #getStackTrace(). For debugging purpose though you can still access the full stacktrace using #getUnfilteredStackTrace(). However note that other calls related to the stackTrace will refer to the filter stacktrace.

Advanced users and framework integrators can control stack trace filtering behavior via org.mockito.plugins.StackTraceCleanerProvider classpath plugin.
[中]由mockito引发,以发出由mockito或用户引起的错误。从该类继承的所有异常类都将筛选堆栈跟踪。过滤会删除Mockito内部堆栈跟踪元素,以提供干净的堆栈跟踪并提高生产率。
如果使用#getStackTrace(),堆栈跟踪将从mockito调用中过滤。出于调试目的,您仍然可以使用#getUnfilteredStackTrace()访问完整的stacktrace。但是请注意,与stackTrace相关的其他调用将引用filter stackTrace。
高级用户和框架集成商可以通过org控制堆栈跟踪过滤行为。莫基托。插件。StackTraceCleanerProvider类路径插件。

代码示例

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

public Calls(int wantedNumberOfInvocations) {
  if( wantedNumberOfInvocations <= 0 ) {
    throw new MockitoException( "Negative and zero values are not allowed here" );
  }
  this.wantedCount = wantedNumberOfInvocations;
}

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

public MockitoException(String message, Throwable t) {
  super(message, t);
  filterStackTrace();
}

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

if (new MockUtil().isMock(instance)) {
throw new MockitoException("Problems initiating spied field " + field.getName(), e);

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

public void cannotInjectDependency(Field field, Object matchingMock, Exception details) {
  throw new MockitoException(join(
      "Mockito couldn't inject mock dependency '" + new MockUtil().getMockName(matchingMock) + "' on field ",
      "'" + field + "'",
      "whose type '" + field.getDeclaringClass().getCanonicalName() + "' was annotated by @InjectMocks in your test.",
      "Also I failed because: " + details.getCause().getMessage(),
      ""
  ), details);
}

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

@Override
public void process(Class<?> context, Object testInstance) {
  Field[] fields = context.getDeclaredFields();
  for (Field field : fields) {
    if (field.isAnnotationPresent(Spy.class) && !field.isAnnotationPresent(InjectMocks.class)) {
      assertNoIncompatibleAnnotations(Spy.class, field, Mock.class, Captor.class);
      field.setAccessible(true);
      Object instance;
      try {
        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);
        } else if (instance != null) {
          field.set(testInstance, spyInstance(field, instance));
        } else {
          field.set(testInstance, spyNewInstance(testInstance, field));
        }
      } catch (Exception e) {
        throw new MockitoException("Unable to initialize @Spy annotated field '" + field.getName() + "'.\n" + e.getMessage(), e);
      }
    }
  }
}

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

public static MockitoException delegatedMethodDoesNotExistOnDelegate(Method mockMethod, Object mock, Object delegate) {
  return new MockitoException(join(
      "Methods called on mock must exist in delegated instance.",
      "When calling: " + mockMethod + " on mock: " + MockUtil.getMockName(mock),
      "no such method was found.",
      "Check that the instance passed to delegatesTo() is of the correct type or contains compatible methods",
      "(delegate instance had type: " + delegate.getClass().getSimpleName() + ")"
  ));
}

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

public boolean processInjection(Field field, Object fieldOwner, Set<Object> mockCandidates) {
  try {
    SimpleArgumentResolver simpleArgumentResolver = new SimpleArgumentResolver(mockCandidates);
    FieldInitializationReport report = new FieldInitializer(fieldOwner, field, simpleArgumentResolver).initialize();
    return report.fieldWasInitializedUsingContructorArgs();
  } catch (MockitoException e) {
    if(e.getCause() instanceof InvocationTargetException) {
      Throwable realCause = e.getCause().getCause();
      throw fieldInitialisationThrewException(field, realCause);
    }
    // other causes should be fine
    return false;
  }
}

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

private FieldInitializationReport initializeInjectMocksField(Field field, Object fieldOwner) {
  try {
    return new FieldInitializer(fieldOwner, field).initialize();
  } catch (MockitoException e) {
    if(e.getCause() instanceof InvocationTargetException) {
      Throwable realCause = e.getCause().getCause();
      throw fieldInitialisationThrewException(field, realCause);
    }
    throw cannotInitializeForInjectMocksAnnotation(field.getName(),e.getMessage());
  }
}

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

private void filterStackTrace() {
  unfilteredStackTrace = getStackTrace();
  ConditionalStackTraceFilter filter = new ConditionalStackTraceFilter();
  filter.filter(this);
}

代码示例来源: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: google/j2objc

public int invalidArgumentPositionRangeAtInvocationTime(InvocationOnMock invocation, boolean willReturnLastParameter, int argumentIndex) {
  throw new MockitoException(
      join("Invalid argument index for the current invocation of method : ",
         " -> " + new MockUtil().getMockName(invocation.getMock()) + "." + invocation.getMethod().getName() + "()",
         "",
         (willReturnLastParameter ?
             "Last parameter wanted" :
             "Wanted parameter at position " + argumentIndex) + " but " + possibleArgumentTypesOf(invocation),
         "The index need to be a positive number that indicates a valid position of the argument in the invocation.",
         "However it is possible to use the -1 value to indicates that the last argument should be returned.",
         ""));
}

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

public static MockitoException delegatedMethodHasWrongReturnType(Method mockMethod, Method delegateMethod, Object mock, Object delegate) {
  return new MockitoException(join(
      "Methods called on delegated instance must have compatible return types with the mock.",
      "When calling: " + mockMethod + " on mock: " + MockUtil.getMockName(mock),
      "return type should be: " + mockMethod.getReturnType().getSimpleName() + ", but was: " + delegateMethod.getReturnType().getSimpleName(),
      "Check that the instance passed to delegatesTo() is of the correct type or contains compatible methods",
      "(delegate instance had type: " + delegate.getClass().getSimpleName() + ")"
  ));
}

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

private FieldInitializationReport initializeInjectMocksField(Field field, Object fieldOwner) {
  FieldInitializationReport report = null;
  try {
    report = new FieldInitializer(fieldOwner, field).initialize();
  } catch (MockitoException e) {
    if(e.getCause() instanceof InvocationTargetException) {
      Throwable realCause = e.getCause().getCause();
      new Reporter().fieldInitialisationThrewException(field, realCause);
    }
    new Reporter().cannotInitializeForInjectMocksAnnotation(field.getName(), e);
  }
  return report; // never null
}

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

private void filterStackTrace() {
  unfilteredStackTrace = getStackTrace();
  
  ConditionalStackTraceFilter filter = new ConditionalStackTraceFilter();
  filter.filter(this);
}

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

public static MockitoException moreThanOneAnnotationNotAllowed(String fieldName) {
  return new MockitoException("You cannot have more than one Mockito annotation on a field!\n" +
                    "The field '" + fieldName + "' has multiple Mockito annotations.\n" +
                    "For info how to use annotations see examples in javadoc for MockitoAnnotations class.");
}

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

public void cannotInjectDependency(Field field, Object matchingMock, Exception details) {
    throw new MockitoException(join(
        "Mockito couldn't inject mock dependency '" + new MockUtil().getMockName(matchingMock) + "' on field ",
        "'" + field + "'",
        "whose type '" + field.getDeclaringClass().getCanonicalName() + "' was annotated by @InjectMocks in your test.",
        "Also I failed because: " + details.getCause().getMessage(),
        ""
    ), details);
  }
}

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

public static MockitoException cannotInjectDependency(Field field, Object matchingMock, Exception details) {
  return new MockitoException(join(
      "Mockito couldn't inject mock dependency '" + MockUtil.getMockName(matchingMock) + "' on field ",
      "'" + field + "'",
      "whose type '" + field.getDeclaringClass().getCanonicalName() + "' was annotated by @InjectMocks in your test.",
      "Also I failed because: " + exceptionCauseMessageIfAvailable(details),
      ""
  ), details);
}

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

public MockitoException(String message) {
  super(message);
  filterStackTrace();
}

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

public boolean processInjection(Field field, Object fieldOwner, Set<Object> mockCandidates) {
  try {
    SimpleArgumentResolver simpleArgumentResolver = new SimpleArgumentResolver(mockCandidates);
    FieldInitializationReport report = new FieldInitializer(fieldOwner, field, simpleArgumentResolver).initialize();
    return report.fieldWasInitializedUsingContructorArgs();
  } catch (MockitoException e) {
    if(e.getCause() instanceof InvocationTargetException) {
      Throwable realCause = e.getCause().getCause();
      new Reporter().fieldInitialisationThrewException(field, realCause);
    }
    // other causes should be fine
    return false;
  }
}

相关文章

微信公众号

最新文章

更多