org.mockito.exceptions.base.MockitoException.<init>()方法的使用及代码示例

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

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

MockitoException.<init>介绍

暂无

代码示例

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

private static Constructor<?> noArgConstructorOf(Class<?> type) {
  Constructor<?> constructor;
  try {
    constructor = type.getDeclaredConstructor();
  } catch (NoSuchMethodException e) {
    throw new MockitoException("Please ensure that the type '" + type.getSimpleName() + "' has a no-arg constructor.");
  }
  return constructor;
}

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

private void checkNotEnum(Field field) {
  if(field.getType().isEnum()) {
    throw new MockitoException("the type '" + field.getType().getSimpleName() + "' is an enum.");
  }
}

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

private void checkNotLocal(Field field) {
  if(field.getType().isLocalClass()) {
    throw new MockitoException("the type '" + field.getType().getSimpleName() + "' is a local class.");
  }
}

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

private void checkNotInner(Field field) {
  Class<?> type = field.getType();
  if(type.isMemberClass() && !isStatic(type.getModifiers())) {
    throw new MockitoException("the type '" + type.getSimpleName() + "' is an inner non static class.");
  }
}

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

private <T> void checkSupportedCombination(boolean subclassingRequired, MockFeatures<T> features) {
  if (subclassingRequired
      && !features.mockedType.isArray()
      && !features.mockedType.isPrimitive()
      && Modifier.isFinal(features.mockedType.getModifiers())) {
    throw new MockitoException("Unsupported settings with this type '" + features.mockedType.getName() + "'");
  }
}

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

public static MockitoException cannotMockClass(Class<?> clazz, String reason) {
  return new MockitoException(join(
      "Cannot mock/spy " + clazz.toString(),
      "Mockito cannot mock/spy because :",
      " - " + reason
  ));
}

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

public static MockitoException onlyVoidMethodsCanBeSetToDoNothing() {
  return new MockitoException(join(
      "Only void methods can doNothing()!",
      "Example of correct use of doNothing():",
      "    doNothing().",
      "    doThrow(new RuntimeException())",
      "    .when(mock).someVoidMethod();",
      "Above means:",
      "someVoidMethod() does nothing the 1st time but throws an exception the 2nd time is called"
  ));
}

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

public static MockitoException cannotInitializeForSpyAnnotation(String fieldName, Exception details) {
  return new MockitoException(join("Cannot instantiate a @Spy for '" + fieldName + "' field.",
                   "You haven't provided the instance for spying at field declaration so I tried to construct the instance.",
                   "However, I failed because: " + details.getMessage(),
                   "Examples of correct usage of @Spy:",
                   "   @Spy List mock = new LinkedList();",
                   "   @Spy Foo foo; //only if Foo has parameterless constructor",
                   "   //also, don't forget about MockitoAnnotations.initMocks();",
                   ""), details);
}

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

public static MockitoException serializableWontWorkForObjectsThatDontImplementSerializable(Class<?> classToMock) {
  return new MockitoException(join(
      "You are using the setting 'withSettings().serializable()' however the type you are trying to mock '" + classToMock.getSimpleName() + "'",
      "do not implement Serializable AND do not have a no-arg constructor.",
      "This combination is requested, otherwise you will get an 'java.io.InvalidClassException' when the mock will be serialized",
      "",
      "Also note that as requested by the Java serialization specification, the whole hierarchy need to implements Serializable,",
      "i.e. the top-most superclass has to implements Serializable.",
      ""
  ));
}

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

public static MockitoException extraInterfacesDoesNotAcceptNullParameters() {
  return new MockitoException(join(
      "extraInterfaces() does not accept null parameters."
  ));
}

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

public static MockitoException cannotInitializeForInjectMocksAnnotation(String fieldName, String causeMessage) {
  return new MockitoException(join("Cannot instantiate @InjectMocks field named '" + fieldName + "'! Cause: "+causeMessage,
                   "You haven't provided the instance at field declaration so I tried to construct the instance.",
                   "Examples of correct usage of @InjectMocks:",
                   "   @InjectMocks Service service = new Service();",
                   "   @InjectMocks Service service;",
                   "   //and... don't forget about some @Mocks for injection :)",
                   ""));
}

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

public static MockitoException invocationListenerThrewException(InvocationListener listener, Throwable listenerThrowable) {
  return new MockitoException(join(
      "The invocation listener with type " + listener.getClass().getName(),
      "threw an exception : " + listenerThrowable.getClass().getName() + listenerThrowable.getMessage()), listenerThrowable);
}

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

public static MockitoException mockedTypeIsInconsistentWithDelegatedInstanceType(Class<?> mockedType, Object delegatedInstance) {
  return new MockitoException(join(
      "Mocked type must be the same as the type of your delegated instance.",
      "Mocked type must be: " + delegatedInstance.getClass().getSimpleName() + ", but is: " + mockedType.getSimpleName(),
      "  //correct delegate:",
      "  spy = mock( ->List.class<- , withSettings().delegatedInstance( ->new ArrayList()<- );",
      "  //incorrect - types don't match:",
      "  spy = mock( ->List.class<- , withSettings().delegatedInstance( ->new HashSet()<- );"
  ));
}

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

public static MockitoException cannotStubWithNullThrowable() {
  return new MockitoException(join(
      "Cannot stub with null throwable!"
  ));
}

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

public static MockitoException inOrderRequiresFamiliarMock() {
  return new MockitoException(join(
      "InOrder can only verify mocks that were passed in during creation of InOrder.",
      "For example:",
      "    InOrder inOrder = inOrder(mockOne);",
      "    inOrder.verify(mockOne).doStuff();"
  ));
}

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

public static MockitoException extraInterfacesCannotContainMockedType(Class<?> wrongType) {
  return new MockitoException(join(
      "extraInterfaces() does not accept the same type as the mocked type.",
      "You mocked following type: " + wrongType.getSimpleName(),
      "and you passed the same very interface to the extraInterfaces()"
  ));
}

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

private static SubclassLoader tryLookup() {
  try {
    Class<?> methodHandles = Class.forName("java.lang.invoke.MethodHandles");
    Object lookup = methodHandles.getMethod("lookup").invoke(null);
    Method privateLookupIn = methodHandles.getMethod("privateLookupIn", Class.class, Class.forName("java.lang.invoke.MethodHandles$Lookup"));
    Object codegenLookup = privateLookupIn.invoke(null, InjectionBase.class, lookup);
    return new WithLookup(lookup, codegenLookup, privateLookupIn);
  } catch (Exception exception) {
    throw new MockitoException(join(ERROR_MESSAGE, "", Platform.describe()), exception);
  }
}

代码示例来源: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);
}

相关文章

微信公众号

最新文章

更多