mockit.StrictExpectations 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 automated developer testing.
It contains mocking/faking APIs and a code coverage tool, 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 Rogério Liesenfeld
* This file is subject to the terms of the MIT license (see LICENSE.txt).
*/
package mockit;
/**
* Used to record strict expectations on {@linkplain Mocked mocked} types and their instances.
*
* During replay, invocations matching strictly recorded expectations must occur in the same number and the
* same order.
* Any invocation that doesn't match a recorded expectation will cause an {@code UnexpectedInvocation} error to be
* thrown, failing the test.
*
* Strict expectations do not allow the use of {@linkplain Verifications verification blocks} for explicit verification.
* Instead, one or more strict expectation blocks are meant to fully specify all the invocations to mocked
* types/instances that are expected and allowed to occur from the code under test.
*
* @see #StrictExpectations()
* @see #StrictExpectations(Object...)
* @see #StrictExpectations(Integer, Object...)
* @see Tutorial
*/
public abstract class StrictExpectations extends Expectations
{
/**
* Registers one or more strict expectations recorded on available mocked types and/or mocked instances, as written
* inside the instance initialization body of an anonymous subclass or the called constructor of a named subclass.
*
* @see #StrictExpectations(Object...)
* @see #StrictExpectations(Integer, Object...)
*/
protected StrictExpectations() {}
/**
* Same as {@link #StrictExpectations()}, except that one or more classes will be partially mocked according to the
* expectations recorded in the expectation block.
*
* The classes to be partially mocked are those directly specified through their {@code Class} objects as well as
* those to which any given objects belong.
* During replay, any invocations to one of these classes or objects will execute real production code, unless a
* matching expectation was recorded.
* This mechanism, however, does not apply to {@code native} methods, which are not supported for partial mocking.
*
* For a given {@code Class} object, all constructors and methods can be mocked, from the specified class up to but
* not including {@code java.lang.Object}.
* For a given object, only methods can be mocked, not constructors; also, during replay, invocations to
* instance methods will only match expectations recorded on the given instance (or instances, if more than one was
* given).
*
* @param classesOrObjectsToBePartiallyMocked one or more classes or objects whose classes are to be partially mocked
*
* @throws IllegalArgumentException if given a class literal for an interface, an annotation, an array, a
* primitive/wrapper type, or a {@linkplain java.lang.reflect.Proxy#isProxyClass(Class) proxy class} created for an
* interface, or if given a value/instance of such a type
*
* @see #StrictExpectations(Integer, Object...)
* @see Tutorial
*/
protected StrictExpectations(Object... classesOrObjectsToBePartiallyMocked)
{
super(classesOrObjectsToBePartiallyMocked);
}
/**
* Same as {@link #StrictExpectations(Object...)}, but considering that the invocations inside the block will occur
* in a given number of iterations.
*
* The effect of specifying a number of iterations larger than 1 (one) is equivalent to duplicating the whole
* sequence of invocations in the expectation block.
*
* @param numberOfIterations the positive number of iterations for the whole sequence of invocations recorded inside
* the block; when not specified, 1 (one) iteration is assumed
* @param classesOrObjectsToBePartiallyMocked one or more classes or objects whose classes are to be partially mocked
*
* @see #StrictExpectations()
*/
protected StrictExpectations(Integer numberOfIterations, Object... classesOrObjectsToBePartiallyMocked)
{
super(classesOrObjectsToBePartiallyMocked);
getCurrentPhase().setNumberOfIterations(numberOfIterations);
}
}