IMockBuilder (EasyMock class extension 2.5.2 API)
Date: 2009-12-20

Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## org.easymock.classextension
Interface IMockBuilder<T>

Type Parameters:
  `T` - type of the object being created

    public interface IMockBuilder<T>

Helps the creation of partial mocks with `EasyMock`.

Example of usage:

     public class MyClass {
         public MyClass(A a, B b) {
         }
     }

     public class MyClassTest {
         @Test
         public void testFoo() throws Exception {
             IMocksControl mockControl = createControl();
             A a = mockControl.createMock(A.class);
             B b = mockControl.createMock(B.class);

             MyClass myClass = createMockBuilder(MyClass.class)
                     .withConstructor(a, b).createMock(mockControl);

             // Set the expectations of A and B and test some method in MyClass
         }
     }

This class also has support for partial mocks as shown by the example below:

     public class MyMockedClass {
         // Empty class is also valid for IMockBuilder.
         public MyMockedClass() {
         }

         public void foo(int a) {
             blah(a);
             bleh();
         }

         public void blah(int a) {
         }

         public void bleh() {
         }
     }

     public class MyMockedClassTest {
         @Test
         public void testFoo() throws Exception {
             MyMockedClass myMockedClass = createMockBuilder(MyMockedClass.class)
                     .withConstructor().addMockedMethod("blah", int.class)
                     .addMockedMethod("bleh").createMock();

             // These are the expectations.
             myMockedClass.blah(1);
             myMockedClass.bleh();
             replay(myMockedClass);

             myMockedClass.foo(1);
             verify(myMockedClass);
         }
     }

Warning: There may be ambiguities when there are two different constructors with compatible types. For instance:

     public class A {
     }

     public class B extends A {
     }

     public class ClassWithAmbiguity {
         public ClassWithAmbiguity(A a) {
         }

         public ClassWithAmbiguity(B b) {
         }
     }

will cause problems if using `withConstructor(Object...)`. To solve this, you can explicitly define the constructor parameter types to use by calling `withConstructor(Class...)` and then `withArgs(Object...)`, like this:

     createMockBuilder(MyMockedClass.class).withConstructor(A.class).withArgs(
             new A()).createMock();

Method Summary
`IMockBuilder<T>` | `addMockedMethod(java.lang.reflect.Method method)` Adds a method to be mocked in the testing class.
`IMockBuilder<T>` | `addMockedMethod(java.lang.String methodName)` Adds a method to be mocked in the testing class.
`IMockBuilder<T>` | `addMockedMethod(java.lang.String methodName, java.lang.Class<?>... parameterTypes)` Adds a method to be mocked in the testing class.
`IMockBuilder<T>` | `addMockedMethods(java.lang.reflect.Method... methods)` Adds methods to be mocked in the testing class.
`IMockBuilder<T>` | `addMockedMethods(java.lang.String... methodNames)` Adds methods to be mocked in the testing class.
`T` | `createMock()` Create a default mock from this builder.
`T` | `createMock(IMocksControl control)` Create mock from the provided mock control using the arguments passed to the builder.
`T` | `createMock(java.lang.String name)` Create named mock from the provided mock control using the arguments passed to the builder.
`T` | `createMock(java.lang.String name, IMocksControl control)` Create named mock from the provided mock control using the arguments passed to the builder.
`T` | `createNiceMock()` Create a nice mock from this builder.
`T` | `createNiceMock(java.lang.String name)` Create a named nice mock from this builder.
`T` | `createStrictMock()` Create a strict mock from this builder.
`T` | `createStrictMock(java.lang.String name)` Create a named strict mock from this builder.
`IMockBuilder<T>` | `withArgs(java.lang.Object... initArgs)` Defines the arguments to be passed to the constructor of the class.
`IMockBuilder<T>` | `withConstructor()` Defines the empty constructor should be called.
`IMockBuilder<T>` | `withConstructor(java.lang.Class<?>... argTypes)` Defines the exact argument types for the constructor to use.
`IMockBuilder<T>` | `withConstructor(java.lang.reflect.Constructor<?> constructor)` Defines the constructor to use to instantiate the mock.
`IMockBuilder<T>` | `withConstructor(ConstructorArgs constructorArgs)` Defines the constructor and arguments to use to instantiate the mock.
`IMockBuilder<T>` | `withConstructor(java.lang.Object... initArgs)` Defines the constructor parameters for the mocked class.

Method Detail

### addMockedMethod

    IMockBuilder<T> addMockedMethod(java.lang.reflect.Method method)

  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 to be mocked
  Returns:
    this

### addMockedMethod

    IMockBuilder<T> addMockedMethod(java.lang.String methodName)

  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 `addMockedMethod`s in this class if that is the case.

  Parameters:
    `methodName` - name of the method to be mocked
  Returns:
    this

### addMockedMethod

    IMockBuilder<T> addMockedMethod(java.lang.String methodName,
                                    java.lang.Class<?>... parameterTypes)

  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` - name of the method to be mocked
    `parameterTypes` - types of the parameters of the method
  Returns:
    this

### addMockedMethods

    IMockBuilder<T> addMockedMethods(java.lang.String... methodNames)

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

  Parameters:
    `methodNames` - names of the methods to be mocked
  Returns:
    this

### addMockedMethods

    IMockBuilder<T> addMockedMethods(java.lang.reflect.Method... methods)

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

  Parameters:
    `methods` - methods to be mocked
  Returns:
    this

### withConstructor

    IMockBuilder<T> withConstructor(java.lang.reflect.Constructor<?> constructor)

  Defines the constructor to use to instantiate the mock. It is expected that you call `withArgs(java.lang.Object...)` with the actual constructor argument values after this.

  Parameters:
    `constructor` - the constructor to be called
  Returns:
    this

### withConstructor

    IMockBuilder<T> withConstructor(ConstructorArgs constructorArgs)

  Defines the constructor and arguments to use to instantiate the mock.

  Parameters:
    `constructorArgs` - constructor and arguments
  Returns:
    this

### withConstructor

    IMockBuilder<T> withConstructor()

  Defines the empty constructor should be called.

  Returns:
    this

### withConstructor

    IMockBuilder<T> withConstructor(java.lang.Object... initArgs)

  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` - arguments of the constructor
  Returns:
    this

### withConstructor

    IMockBuilder<T> withConstructor(java.lang.Class<?>... argTypes)

  Defines the exact argument types for the constructor to use. It is expected that you call `withArgs(java.lang.Object...)` with the actual constructor argument values after this.

  Parameters:
    `argTypes` - the exact argument types of the constructor
  Returns:
    this

### withArgs

    IMockBuilder<T> withArgs(java.lang.Object... initArgs)

  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` - the arguments to pass to the constructor
  Returns:
    this

### createStrictMock

    T createStrictMock()

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

  Returns:
    the newly created mock

### createMock

    T createMock()

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

  Returns:
    the newly created mock

### createNiceMock

    T createNiceMock()

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

  Returns:
    the newly created mock

### createMock

    T createMock(IMocksControl control)

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

  Parameters:
    `control` - `IMocksControl` used to create the object
  Returns:
    the newly created mock

### createStrictMock

    T createStrictMock(java.lang.String name)

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

  Parameters:
    `name` - the mock name
  Returns:
    the newly created mock

### createMock

    T createMock(java.lang.String name)

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

  Parameters:
    `name` - the mock name
  Returns:
    the newly created mock

### createNiceMock

    T createNiceMock(java.lang.String name)

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

  Parameters:
    `name` - the mock name
  Returns:
    the newly created mock

### createMock

    T createMock(java.lang.String name,
                 IMocksControl control)

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

  Parameters:
    `name` - the mock name
    `control` - `IMocksControl` used to create the object
  Returns:
    the newly created mock

Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Copyright © 2003-2009 OFFIS, Henri Tremblay. This documentation is provided under the terms of the MIT licence.