![JAR search and dependency download from the Maven repository](/logo.png)
mockit.integration.internal.TestRunnerDecorator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jmockit Show documentation
Show all versions of jmockit Show documentation
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.
/*
* Copyright (c) 2006-2013 Rogério Liesenfeld
* This file is subject to the terms of the MIT license (see LICENSE.txt).
*/
package mockit.integration.internal;
import java.lang.reflect.*;
import org.jetbrains.annotations.*;
import mockit.internal.*;
import mockit.internal.mockups.*;
import mockit.internal.expectations.*;
import mockit.internal.expectations.injection.*;
import mockit.internal.expectations.mocking.*;
import mockit.internal.state.*;
import mockit.*;
import mockit.internal.util.*;
/**
* Base class for "test runner decorators", which provide integration between JMockit and specific
* test runners from JUnit and TestNG.
*/
public class TestRunnerDecorator
{
private static SavePoint savePointForTest;
protected final void updateTestClassState(@Nullable Object target, Class> testClass)
{
try {
handleSwitchToNewTestClassIfApplicable(testClass);
if (target != null) {
handleMockFieldsForWholeTestClass(target);
}
}
catch (Error e) {
try {
SavePoint.rollbackForTestClass();
}
catch (Error err) {
StackTrace.filterStackTrace(err);
throw err;
}
throw e;
}
catch (RuntimeException e) {
SavePoint.rollbackForTestClass();
StackTrace.filterStackTrace(e);
throw e;
}
}
private void handleSwitchToNewTestClassIfApplicable(Class> testClass)
{
Class> currentTestClass = TestRun.getCurrentTestClass();
if (testClass != currentTestClass) {
if (currentTestClass == null) {
SavePoint.registerNewActiveSavePoint();
}
else if (!currentTestClass.isAssignableFrom(testClass)) {
cleanUpMocksFromPreviousTestClass();
SavePoint.registerNewActiveSavePoint();
}
setUpClassLevelMocksAndStubs(testClass);
TestRun.setCurrentTestClass(testClass);
}
}
public static void cleanUpMocksFromPreviousTestClass()
{
discardTestLevelMockedTypes();
SavePoint.rollbackForTestClass();
SharedFieldTypeRedefinitions redefinitions = TestRun.getSharedFieldTypeRedefinitions();
if (redefinitions != null) {
redefinitions.cleanUp();
TestRun.setSharedFieldTypeRedefinitions(null);
}
}
protected final void setUpClassLevelMocksAndStubs(@NotNull Class> testClass)
{
UsingMocksAndStubs mocksAndStubs = testClass.getAnnotation(UsingMocksAndStubs.class);
if (mocksAndStubs != null) {
setUpMocksAndStubs(mocksAndStubs.value());
}
}
public static void setUpMocksAndStubs(@NotNull Class>[] mockAndRealClasses)
{
TestRun.exitNoMockingZone();
try {
for (Class> mockOrRealClass : mockAndRealClasses) {
if (MockUp.class.isAssignableFrom(mockOrRealClass)) {
ConstructorReflection.newInstance(mockOrRealClass);
}
else {
new ClassStubbing(mockOrRealClass).stubOut();
}
}
}
finally {
TestRun.enterNoMockingZone();
}
}
protected final void prepareForNextTest()
{
discardTestLevelMockedTypes();
savePointForTest = new SavePoint();
TestRun.prepareForNextTest();
}
protected static void discardTestLevelMockedTypes()
{
if (savePointForTest != null) {
savePointForTest.rollback();
savePointForTest = null;
}
}
private void handleMockFieldsForWholeTestClass(@NotNull Object target)
{
SharedFieldTypeRedefinitions sharedRedefinitions = TestRun.getSharedFieldTypeRedefinitions();
if (sharedRedefinitions == null) {
sharedRedefinitions = new SharedFieldTypeRedefinitions(target);
sharedRedefinitions.redefineTypesForTestClass();
TestRun.setSharedFieldTypeRedefinitions(sharedRedefinitions);
}
if (target != TestRun.getCurrentTestInstance()) {
sharedRedefinitions.assignNewInstancesToMockFields(target);
}
}
protected final void createInstancesForTestedFields(@Nullable Object target)
{
SharedFieldTypeRedefinitions sharedRedefinitions = TestRun.getSharedFieldTypeRedefinitions();
if (sharedRedefinitions != null) {
TestedClassInstantiations testedClasses = sharedRedefinitions.getTestedClassInstantiations();
if (testedClasses != null) {
TestRun.enterNoMockingZone();
try {
testedClasses.assignNewInstancesToTestedFields(target);
}
finally {
TestRun.exitNoMockingZone();
}
}
}
}
@Nullable
protected final Object[] createInstancesForMockParameters(
@NotNull Object target, @NotNull Method testMethod, @Nullable Object[] parameterValues,
@NotNull SavePoint savePoint)
{
if (testMethod.getParameterTypes().length == 0) {
return null;
}
TestRun.enterNoMockingZone();
try {
ParameterTypeRedefinitions redefinitions = new ParameterTypeRedefinitions(target, testMethod, parameterValues);
TestRun.getExecutingTest().setParameterTypeRedefinitions(redefinitions);
savePoint.addRollbackAction(redefinitions.getCaptureOfNewInstances());
return redefinitions.getParameterValues();
}
finally {
TestRun.exitNoMockingZone();
}
}
protected final void concludeTestMethodExecution(
@NotNull SavePoint savePoint, @Nullable Throwable thrownByTest, boolean thrownAsExpected)
throws Throwable
{
TestRun.enterNoMockingZone();
Error expectationsFailure = RecordAndReplayExecution.endCurrentReplayIfAny();
try {
if (expectationsFailure == null && (thrownByTest == null || thrownAsExpected)) {
TestRun.verifyExpectationsOnAnnotatedMocks();
}
}
finally {
TestRun.resetExpectationsOnAnnotatedMocks();
savePoint.rollback();
TestRun.exitNoMockingZone();
}
if (thrownByTest != null) {
if (expectationsFailure == null || !thrownAsExpected || isUnexpectedOrMissingInvocation(thrownByTest)) {
throw thrownByTest;
}
Throwable expectationsFailureCause = expectationsFailure.getCause();
if (expectationsFailureCause != null) {
expectationsFailureCause.initCause(thrownByTest);
}
}
if (expectationsFailure != null) {
throw expectationsFailure;
}
}
private boolean isUnexpectedOrMissingInvocation(Throwable error)
{
Class> errorType = error.getClass();
return errorType == UnexpectedInvocation.class || errorType == MissingInvocation.class;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy