All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mockit.integration.junit4.internal.BlockJUnit4ClassRunnerDecorator Maven / Gradle / Ivy

Go to download

JMockit is a Java toolkit for developer (unit/integration) testing. It contains mocking APIs and other tools, supporting both JUnit and TestNG. The mocking APIs allow all kinds of Java code, without testability restrictions, to be tested in isolation from selected dependencies.

There is a newer version: 1.7
Show newest version
/*
 * Copyright (c) 2006-2013 Rogério Liesenfeld
 * This file is subject to the terms of the MIT license (see LICENSE.txt).
 */
package mockit.integration.junit4.internal;

import java.lang.reflect.*;

import org.jetbrains.annotations.*;

import org.junit.runners.*;
import org.junit.runners.model.*;

import mockit.*;
import mockit.integration.internal.*;
import mockit.internal.state.*;
import mockit.internal.util.*;

/**
 * Startup mock which works in conjunction with {@link JUnit4TestRunnerDecorator} to provide JUnit 4.5+ integration.
 * 

* This class is not supposed to be accessed from user code. JMockit will automatically load it at startup. */ public final class BlockJUnit4ClassRunnerDecorator extends MockUp { private static final Method getTestClass; static { Method getTestClassMethod; try { getTestClassMethod = ParentRunner.class.getDeclaredMethod("getTestClass"); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } if (getTestClassMethod.isAccessible()) { getTestClass = null; } else { getTestClassMethod.setAccessible(true); getTestClass = getTestClassMethod; } } @Mock @Nullable public Object createTest(@NotNull Invocation invocation) throws Throwable { TestRun.enterNoMockingZone(); try { ParentRunner it = invocation.getInvokedInstance(); assert it != null; TestClass junitTestClass = getTestClass == null ? it.getTestClass() : (TestClass) getTestClass.invoke(it); Class newTestClass = junitTestClass.getJavaClass(); Class currentTestClass = TestRun.getCurrentTestClass(); if (currentTestClass != null && !currentTestClass.isAssignableFrom(newTestClass)) { TestRunnerDecorator.cleanUpMocksFromPreviousTestClass(); } return invocation.proceed(); } catch (InvocationTargetException e) { Throwable cause = e.getCause(); StackTrace.filterStackTrace(cause); throw cause; } finally { TestRun.exitNoMockingZone(); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy