Top Description Fields Constructors Methods
org.easymock.internal

public Class MockBuilder<T>

extends Object
implements IMockBuilder<T>
Class Inheritance
All Implemented Interfaces
org.easymock.IMockBuilder
Type Parameters
<T>
type of the mock created
Imports
org.easymock.ConstructorArgs, .EasyMock, .EasyMockSupport, .IMockBuilder, .IMocksControl, .MockType, java.lang.reflect.Constructor, .Method, .Modifier, java.util.HashSet, .Set, java.util.function.Predicate

Default implementation of IMockBuilder.

The original idea and part of the code where contributed by Rodrigo Damazio and Bruno Fonseca at Google

Author
Henri Tremblay

Field Summary

Modifier and TypeField and Description
private static final Predicate<Method>
private Constructor<?>
private ConstructorArgs
private Set<Method>
private final EasyMockSupport
private final Class<?>

Constructor Summary

AccessConstructor and Description
public
MockBuilder(Class<?> toMock)

public
MockBuilder(Class<?>
The class of the mock to create
toMock
,
EasyMockSupport
The EasyMockSupport used to create mocks. Null if none
support
)

Used by EasyMockSupport to allow the mock registration in the list of controls

Method Summary

Modifier and TypeMethod and Description
public IMockBuilder<T>
addMockedMethod(Method
method to be mocked
method
)

Implements org.easymock.IMockBuilder.addMockedMethod.

Adds a method to be mocked in the testing class.
public IMockBuilder<T>
addMockedMethod(String
name of the method to be mocked
methodName
)

Implements org.easymock.IMockBuilder.addMockedMethod.

Adds a method to be mocked in the testing class.
public IMockBuilder<T>
addMockedMethod(String
name of the method to be mocked
methodName
,
Class<?>...
types of the parameters of the method
parameterTypes
)

Implements org.easymock.IMockBuilder.addMockedMethod.

Adds a method to be mocked in the testing class.
public IMockBuilder<T>
addMockedMethods(String...
names of the methods to be mocked
methodNames
)

Implements org.easymock.IMockBuilder.addMockedMethods.

Adds methods to be mocked in the testing class.
public IMockBuilder<T>
addMockedMethods(Method...
methods to be mocked
methods
)

Implements org.easymock.IMockBuilder.addMockedMethods.

Adds methods to be mocked in the testing class.
private void
public <R> R
createMock(MockType
the mock type
type
)

Implements org.easymock.IMockBuilder.createMock.

Create mock of the request type from this builder.
public <R> R
createMock(String
the mock name
name
,
MockType
the mock type
type
)

Implements org.easymock.IMockBuilder.createMock.

Create a named mock of the request type from this builder.
public <R> R
createMock(IMocksControl
org.easymock.IMocksControl used to create the object
control
)

Implements org.easymock.IMockBuilder.createMock.

Create mock from the provided mock control using the arguments passed to the builder.
public <R> R
createMock()

Implements org.easymock.IMockBuilder.createMock.

Create a default mock from this builder.
public <R> R
createMock(String
the mock name
name
,
IMocksControl
org.easymock.IMocksControl used to create the object
control
)

Implements org.easymock.IMockBuilder.createMock.

Create named mock from the provided mock control using the arguments passed to the builder.
public <R> R
createMock(String
the mock name
name
)

Implements org.easymock.IMockBuilder.createMock.

Create named mock from the provided mock control using the arguments passed to the builder.
public <R> R
createNiceMock()

Implements org.easymock.IMockBuilder.createNiceMock.

Create a nice mock from this builder.
public <R> R
createNiceMock(String
the mock name
name
)

Implements org.easymock.IMockBuilder.createNiceMock.

Create a named nice mock from this builder.
public <R> R
createStrictMock()

Implements org.easymock.IMockBuilder.createStrictMock.

Create a strict mock from this builder.
public <R> R
createStrictMock(String
the mock name
name
)

Implements org.easymock.IMockBuilder.createStrictMock.

Create a named strict mock from this builder.
public IMockBuilder<T>
withArgs(Object...
the arguments to pass to the constructor
initArgs
)

Implements org.easymock.IMockBuilder.withArgs.

Defines the arguments to be passed to the constructor of the class.
public IMockBuilder<T>
withConstructor(Constructor<?>
the constructor to be called
constructor
)

Implements org.easymock.IMockBuilder.withConstructor.

Defines the constructor to use to instantiate the mock.
public IMockBuilder<T>
public IMockBuilder<T>
withConstructor()

Implements org.easymock.IMockBuilder.withConstructor.

Defines the empty constructor should be called.
public IMockBuilder<T>
withConstructor(Object...
arguments of the constructor
initArgs
)

Implements org.easymock.IMockBuilder.withConstructor.

Defines the constructor parameters for the mocked class.
public IMockBuilder<T>
withConstructor(Class<?>...
the exact argument types of the constructor
argTypes
)

Implements org.easymock.IMockBuilder.withConstructor.

Defines the exact argument types for the constructor to use.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CAN_BE_MOCKEDback to summary
private static final Predicate<Method> CAN_BE_MOCKED
constructorback to summary
private Constructor<?> constructor
constructorArgsback to summary
private ConstructorArgs constructorArgs
mockedMethodsback to summary
private Set<Method> mockedMethods
supportback to summary
private final EasyMockSupport support
toMockback to summary
private final Class<?> toMock

Constructor Detail

MockBuilderback to summary
public MockBuilder(Class<?> toMock)
MockBuilderback to summary
public MockBuilder(Class<?> toMock, EasyMockSupport support)

Used by EasyMockSupport to allow the mock registration in the list of controls

Parameters
toMock:Class<?>

The class of the mock to create

support:EasyMockSupport

The EasyMockSupport used to create mocks. Null if none

Method Detail

addMockedMethodback to summary
public IMockBuilder<T> addMockedMethod(Method method)

Implements org.easymock.IMockBuilder.addMockedMethod.

Doc from org.easymock.IMockBuilder.addMockedMethod.

Adds a method to be mocked in the testing class. Each call will add a new method to the result mock. The method is searched for in the class itself as well as superclasses.

Parameters
method:Method

method to be mocked

Returns:IMockBuilder<T>

this

addMockedMethodback to summary
public IMockBuilder<T> addMockedMethod(String methodName)

Implements org.easymock.IMockBuilder.addMockedMethod.

Doc from org.easymock.IMockBuilder.addMockedMethod.

Adds a method to be mocked in the testing class. Each call will add a new method to the result mock. The method is searched for in the class itself as well as superclasses. There must be no overload of the method. You will have to rely on the other addMockedMethods in this class if that is the case.

Parameters
methodName:String

name of the method to be mocked

Returns:IMockBuilder<T>

this

addMockedMethodback to summary
public IMockBuilder<T> addMockedMethod(String methodName, Class<?>... parameterTypes)

Implements org.easymock.IMockBuilder.addMockedMethod.

Doc from org.easymock.IMockBuilder.addMockedMethod.

Adds a method to be mocked in the testing class. Each call will add a new method to the result mock. The method is searched for in the class itself as well as superclasses.

Parameters
methodName:String

name of the method to be mocked

parameterTypes:Class<?>[]

types of the parameters of the method

Returns:IMockBuilder<T>

this

addMockedMethodsback to summary
public IMockBuilder<T> addMockedMethods(String... methodNames)

Implements org.easymock.IMockBuilder.addMockedMethods.

Doc from org.easymock.IMockBuilder.addMockedMethods.

Adds methods to be mocked in the testing class. Same as addMockedMethod(String) but to mock many methods at once.

Parameters
methodNames:String[]

names of the methods to be mocked

Returns:IMockBuilder<T>

this

addMockedMethodsback to summary
public IMockBuilder<T> addMockedMethods(Method... methods)

Implements org.easymock.IMockBuilder.addMockedMethods.

Doc from org.easymock.IMockBuilder.addMockedMethods.

Adds methods to be mocked in the testing class. Same as addMockedMethod(Method) but to mock many methods at once.

Parameters
methods:Method[]

methods to be mocked

Returns:IMockBuilder<T>

this

checkConstructorNotInitializedback to summary
private void checkConstructorNotInitialized()
createMockback to summary
public <R> R createMock(MockType type)

Implements org.easymock.IMockBuilder.createMock.

Doc from org.easymock.IMockBuilder.createMock.

Create mock of the request type from this builder. The same builder can be called to create multiple mocks.

Parameters
<R>
the returned type. In general T == R but when mocking a generic type, it won't so to be nice with the caller, we return a different type
type:MockType

the mock type

Returns:R

the newly created mock

createMockback to summary
public <R> R createMock(String name, MockType type)

Implements org.easymock.IMockBuilder.createMock.

Doc from org.easymock.IMockBuilder.createMock.

Create a named mock of the request type from this builder. The same builder can be called to create multiple mocks.

Parameters
<R>
the returned type. In general T == R but when mocking a generic type, it won't so to be nice with the caller, we return a different type
name:String

the mock name

type:MockType

the mock type

Returns:R

the newly created mock

createMockback to summary
public <R> R createMock(IMocksControl control)

Implements org.easymock.IMockBuilder.createMock.

Doc from org.easymock.IMockBuilder.createMock.

Create mock from the provided mock control using the arguments passed to the builder.

Parameters
<R>
the returned type. In general T == R but when mocking a generic type, it won't so to be nice with the caller, we return a different type
control:IMocksControl

org.easymock.IMocksControl used to create the object

Returns:R

the newly created mock

createMockback to summary
public <R> R createMock()

Implements org.easymock.IMockBuilder.createMock.

Doc from org.easymock.IMockBuilder.createMock.

Create a default mock from this builder. The same builder can be called to create multiple mocks.

Parameters
<R>
the returned type. In general T == R but when mocking a generic type, it won't so to be nice with the caller, we return a different type
Returns:R

the newly created mock

createMockback to summary
public <R> R createMock(String name, IMocksControl control)

Implements org.easymock.IMockBuilder.createMock.

Doc from org.easymock.IMockBuilder.createMock.

Create named mock from the provided mock control using the arguments passed to the builder.

Parameters
<R>
the returned type. In general T == R but when mocking a generic type, it won't so to be nice with the caller, we return a different type
name:String

the mock name

control:IMocksControl

org.easymock.IMocksControl used to create the object

Returns:R

the newly created mock

createMockback to summary
public <R> R createMock(String name)

Implements org.easymock.IMockBuilder.createMock.

Doc from org.easymock.IMockBuilder.createMock.

Create named mock from the provided mock control using the arguments passed to the builder.

Parameters
<R>
the returned type. In general T == R but when mocking a generic type, it won't so to be nice with the caller, we return a different type
name:String

the mock name

Returns:R

the newly created mock

createNiceMockback to summary
public <R> R createNiceMock()

Implements org.easymock.IMockBuilder.createNiceMock.

Doc from org.easymock.IMockBuilder.createNiceMock.

Create a nice mock from this builder. The same builder can be called to create multiple mocks.

Parameters
<R>
the returned type. In general T == R but when mocking a generic type, it won't so to be nice with the caller, we return a different type
Returns:R

the newly created mock

createNiceMockback to summary
public <R> R createNiceMock(String name)

Implements org.easymock.IMockBuilder.createNiceMock.

Doc from org.easymock.IMockBuilder.createNiceMock.

Create a named nice mock from this builder. The same builder can be called to create multiple mocks.

Parameters
<R>
the returned type. In general T == R but when mocking a generic type, it won't so to be nice with the caller, we return a different type
name:String

the mock name

Returns:R

the newly created mock

createStrictMockback to summary
public <R> R createStrictMock()

Implements org.easymock.IMockBuilder.createStrictMock.

Doc from org.easymock.IMockBuilder.createStrictMock.

Create a strict mock from this builder. The same builder can be called to create multiple mocks.

Parameters
<R>
the returned type. In general T == R but when mocking a generic type, it won't so to be nice with the caller, we return a different type
Returns:R

the newly created mock

createStrictMockback to summary
public <R> R createStrictMock(String name)

Implements org.easymock.IMockBuilder.createStrictMock.

Doc from org.easymock.IMockBuilder.createStrictMock.

Create a named strict mock from this builder. The same builder can be called to create multiple mocks.

Parameters
<R>
the returned type. In general T == R but when mocking a generic type, it won't so to be nice with the caller, we return a different type
name:String

the mock name

Returns:R

the newly created mock

withArgsback to summary
public IMockBuilder<T> withArgs(Object... initArgs)

Implements org.easymock.IMockBuilder.withArgs.

Doc from org.easymock.IMockBuilder.withArgs.

Defines the arguments to be passed to the constructor of the class. The types of the arguments must match those previously defined with withConstructor(Class...) or withConstructor(Constructor).

Parameters
initArgs:Object[]

the arguments to pass to the constructor

Returns:IMockBuilder<T>

this

withConstructorback to summary
public IMockBuilder<T> withConstructor(Constructor<?> constructor)

Implements org.easymock.IMockBuilder.withConstructor.

Doc from org.easymock.IMockBuilder.withConstructor.

Defines the constructor to use to instantiate the mock. It is expected that you call withArgs with the actual constructor argument values after this.

Parameters
constructor:Constructor<?>

the constructor to be called

Returns:IMockBuilder<T>

this

withConstructorback to summary
public IMockBuilder<T> withConstructor(ConstructorArgs constructorArgs)
withConstructorback to summary
public IMockBuilder<T> withConstructor()

Implements org.easymock.IMockBuilder.withConstructor.

Doc from org.easymock.IMockBuilder.withConstructor.

Defines the empty constructor should be called.

Returns:IMockBuilder<T>

this

withConstructorback to summary
public IMockBuilder<T> withConstructor(Object... initArgs)

Implements org.easymock.IMockBuilder.withConstructor.

Doc from org.easymock.IMockBuilder.withConstructor.

Defines the constructor parameters for the mocked class. The builder will automatically find a constructor with compatible argument types. This throws an exception if there is more than one constructor which would accept the given parameters.

Parameters
initArgs:Object[]

arguments of the constructor

Returns:IMockBuilder<T>

this

withConstructorback to summary
public IMockBuilder<T> withConstructor(Class<?>... argTypes)

Implements org.easymock.IMockBuilder.withConstructor.

Doc from org.easymock.IMockBuilder.withConstructor.

Defines the exact argument types for the constructor to use. It is expected that you call withArgs with the actual constructor argument values after this.

Parameters
argTypes:Class<?>[]

the exact argument types of the constructor

Returns:IMockBuilder<T>

this