Ejemplo n.º 1
0
  private void executeTestMethod(FrameworkMethod it, Object target, Object... parameters)
      throws Throwable {
    SavePoint savePoint = new SavePoint();
    TestRun.setSavePointForTestMethod(savePoint);

    Method testMethod = it.getMethod();
    Throwable testFailure = null;
    boolean testFailureExpected = false;

    try {
      Object[] mockParameters = null;
      /** modified by davey.wu * */
      String frameworkMethodName = it.getClass().getName();
      /** 不判断整个类名,是尽量避免代码package重构带来的影响 * */
      boolean isTest4JFrameworkMethod =
          frameworkMethodName.startsWith("org.test4j.junit.")
              && frameworkMethodName.endsWith(".FrameworkMethodWithParameters");
      if (!isTest4JFrameworkMethod) {
        mockParameters =
            createInstancesForMockParameters(target, testMethod, parameters, savePoint);
      }
      /** end modified by davey.wu * */
      createInstancesForTestedFields(target);

      TestRun.setRunningIndividualTest(target);
      it.invokeExplosively(target, mockParameters == null ? parameters : mockParameters);
    } catch (Throwable thrownByTest) {
      testFailure = thrownByTest;
      Class<? extends Throwable> expectedType = testMethod.getAnnotation(Test.class).expected();
      testFailureExpected = expectedType.isAssignableFrom(thrownByTest.getClass());
    } finally {
      concludeTestMethodExecution(savePoint, testFailure, testFailureExpected);
    }
  }
Ejemplo n.º 2
0
  Object invokeExplosively(FrameworkMethod it, Object target, Object... params) throws Throwable {
    Method method = it.getMethod();
    Class<?> testClass = target == null ? method.getDeclaringClass() : target.getClass();

    handleMockingOutsideTestMethods(it, target, testClass);

    // In case it isn't a test method, but a before/after method:
    if (it.getAnnotation(Test.class) == null) {
      if (shouldPrepareForNextTest && it.getAnnotation(Before.class) != null) {
        prepareForNextTest();
        shouldPrepareForNextTest = false;
      }

      TestRun.setRunningIndividualTest(target);
      TestRun.setSavePointForTestMethod(null);

      try {
        return it.invokeExplosively(target, params);
      } catch (Throwable t) {
        RecordAndReplayExecution.endCurrentReplayIfAny();
        StackTrace.filterStackTrace(t);
        throw t;
      } finally {
        if (it.getAnnotation(After.class) != null) {
          shouldPrepareForNextTest = true;
        }
      }
    }

    if (shouldPrepareForNextTest) {
      prepareForNextTest();
    }

    shouldPrepareForNextTest = true;

    try {
      executeTestMethod(it, target, params);
      return null; // it's a test method, therefore has void return type
    } catch (Throwable t) {
      StackTrace.filterStackTrace(t);
      throw t;
    } finally {
      /** modified by davey.wu * */
      TestRun.finishCurrentTestExecution(true);
      // TestRun.finishCurrentTestExecution(false);
      /** end modified by davey.wu * */
    }
  }