/** {@inheritDoc} */
 public void notifyAfterTestMethod(boolean successful) {
   final Object test = MockRepository.getAdditionalState(Keys.CURRENT_TEST_INSTANCE);
   final Method testMethod = (Method) MockRepository.getAdditionalState(Keys.CURRENT_TEST_METHOD);
   final Object[] testArguments =
       (Object[]) MockRepository.getAdditionalState(Keys.CURRENT_TEST_METHOD_ARGUMENTS);
   final TestMethodResult testResult =
       new TestMethodResultImpl((successful ? Result.SUCCESSFUL : Result.FAILED));
   notifyAfterTestMethod(test, testMethod, testArguments, testResult);
 }
 /** {@inheritDoc} */
 public void notifyBeforeTestMethod(Object testInstance, Method testMethod, Object[] arguments) {
   MockRepository.putAdditionalState(Keys.CURRENT_TEST_INSTANCE, testInstance);
   MockRepository.putAdditionalState(Keys.CURRENT_TEST_METHOD, testMethod);
   MockRepository.putAdditionalState(Keys.CURRENT_TEST_METHOD_ARGUMENTS, arguments);
   for (int i = 0; i < powerMockTestListeners.length; i++) {
     final PowerMockTestListener testListener = powerMockTestListeners[i];
     try {
       testListener.beforeTestMethod(testInstance, testMethod, arguments);
     } catch (Exception e) {
       throw new RuntimeException(
           String.format(ERROR_MESSAGE_TEMPLATE, "beforeTestMethod", testListener), e);
     }
   }
 }
Esempio n. 3
0
  @Override
  protected <T> T createMock(
      Class<T> type,
      boolean isStatic,
      boolean isSpy,
      Object delegator,
      MockSettings mockSettings,
      Method... methods) {
    if (type == null) {
      throw new IllegalArgumentException("The class to mock cannot be null");
    }

    validateType(type, isStatic, isSpy);

    final String mockName = toInstanceName(type, mockSettings);

    MockRepository.addAfterMethodRunner(new MockitoStateCleanerRunnable());

    final Class<T> typeToMock;
    if (isFinalJavaSystemClass(type)) {
      typeToMock = (Class<T>) new ClassReplicaCreator().createClassReplica(type);
    } else {
      typeToMock = type;
    }

    final MockData<T> mockData =
        createMethodInvocationControl(
            mockName, typeToMock, methods, isSpy, delegator, mockSettings);

    T mock = mockData.getMock();
    if (isFinalJavaSystemClass(type) && !isStatic) {
      mock = Whitebox.newInstance(type);
      DefaultFieldValueGenerator.fillWithDefaultValues(mock);
    }

    if (isStatic) {
      MockRepository.putStaticMethodInvocationControl(type, mockData.getMethodInvocationControl());
    } else {
      MockRepository.putInstanceMethodInvocationControl(
          mock, mockData.getMethodInvocationControl());
    }

    if (isSpy) {
      new LenientCopyTool().copyToMock(delegator, mock);
    }

    return mock;
  }
Esempio n. 4
0
  /**
   * Add a method that should be intercepted and return another value ( <code>returnObject</code>)
   * (i.e. the method is stubbed).
   */
  public static void stubMethod(Class<?> declaringClass, String methodName, Object returnObject) {
    if (declaringClass == null) {
      throw new IllegalArgumentException("declaringClass cannot be null");
    }
    if (methodName == null || methodName.length() == 0) {
      throw new IllegalArgumentException("methodName cannot be empty");
    }
    Method[] methods = Whitebox.getMethods(declaringClass, methodName);
    if (methods.length == 0) {
      throw new MethodNotFoundException(
          String.format(
              "Couldn't find a method with name %s in the class hierarchy of %s",
              methodName, declaringClass.getName()));
    } else if (methods.length > 1) {
      throw new TooManyMethodsFoundException(
          String.format(
              "Found %d methods with name %s in the class hierarchy of %s.",
              methods.length, methodName, declaringClass.getName()));
    }

    MockRepository.putMethodToStub(methods[0], returnObject);
  }
Esempio n. 5
0
 /**
  * Add a method that should be intercepted and return another value ( <code>returnObject</code>)
  * (i.e. the method is stubbed).
  */
 public static void stubMethod(Method method, Object returnObject) {
   MockRepository.putMethodToStub(method, returnObject);
 }