org.apache.tapestry5.ioc.test
Class TestBase

java.lang.Object
  extended by org.testng.Assert
      extended by org.apache.tapestry5.ioc.test.TestBase
Direct Known Subclasses:
IOCTestCase

public class TestBase
extends org.testng.Assert

Manages a set of EasyMock mock objects. Used as a base class for test cases.

Extends from Assert to bring in all the public static assert methods without requiring extra imports.

Provides a common mock factory method, newMock(Class). A single standard mock control is used for all mock objects. Standard mocks do not care about the exact order in which methods are invoked, though they are as rigourous as strict mocks when checking that parameters are the correct values.

This base class is created with the intention of use within a TestNG test suite; if using JUnit, you can get the same functionality using MockTester.

This class is thread safe (it uses a thread local to store the mock control). In theory, this should allow TestNG to execute tests in parallel.

See Also:
EasyMock.createControl(), MockTester

Constructor Summary
TestBase()
           
 
Method Summary
protected static
<T> void
assertArraysEqual(T[] actual, T... expected)
          Convenience for assertListsEquals(List, List).
protected static
<T> void
assertListsEquals(List<T> actual, List<T> expected)
          Compares two lists for equality; first all the elements are individually compared for equality (if the lists are of unequal length, only elements up to the shorter length are compared).
protected static
<T> void
assertListsEquals(List<T> actual, T... expected)
          Convenience for assertListsEquals(List, List).
protected static void assertMessageContains(Throwable t, String... substrings)
          Asserts that the message property of the throwable contains each of the provided substrings.
protected static
<T> T
create(Class<T> objectType, Object... fieldValues)
          Creates a new instance of the object using its default constructor, and initializes it (via set(Object, Object[])).
 void discardMockControl()
          Discards any mock objects created during the test.
protected static
<T> org.easymock.IExpectationSetters<T>
expect(T value)
          Convienience for EasyMock.expect(Object).
protected static Object get(Object object, String fieldName)
          Reads the content of a private field.
protected  org.easymock.IMocksControl getMocksControl()
          Returns the IMocksControl for this thread.
protected static
<T> org.easymock.Capture<T>
newCapture()
          A factory method to create EasyMock Capture objects.
protected
<T> T
newMock(Class<T> mockClass)
          Creates a new mock object of the indicated type.
protected  void replay()
          Switches each mock object created by newMock(Class) into replay mode (out of the initial training mode).
protected static
<T> T
set(T object, Object... fieldValues)
          Initializes private fields (via reflection).
protected static void setAnswer(org.easymock.IAnswer answer)
          Convienience for EasyMock.expectLastCall() with IExpectationSetters.andAnswer(org.easymock.IAnswer).
protected static void setThrowable(Throwable throwable)
          Convienience for EasyMock.expectLastCall() with IExpectationSetters.andThrow(Throwable).
protected static void unreachable()
          Invoked from code that should not be reachable.
protected  void verify()
          Verifies that all trained methods have been invoked on all mock objects (created by newMock(Class), then switches each mock object back to training mode.
 
Methods inherited from class org.testng.Assert
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEqualsNoOrder, assertEqualsNoOrder, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, fail, fail, fail
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TestBase

public TestBase()
Method Detail

getMocksControl

protected final org.easymock.IMocksControl getMocksControl()
Returns the IMocksControl for this thread.


discardMockControl

public final void discardMockControl()
Discards any mock objects created during the test.


newMock

protected final <T> T newMock(Class<T> mockClass)
Creates a new mock object of the indicated type. The shared mock control does not check order, but does fail on any unexpected method invocations.

Type Parameters:
T - the type of the mock object
Parameters:
mockClass - the class to mock
Returns:
the mock object, ready for training

replay

protected final void replay()
Switches each mock object created by newMock(Class) into replay mode (out of the initial training mode).


verify

protected final void verify()
Verifies that all trained methods have been invoked on all mock objects (created by newMock(Class), then switches each mock object back to training mode.


setThrowable

protected static void setThrowable(Throwable throwable)
Convienience for EasyMock.expectLastCall() with IExpectationSetters.andThrow(Throwable).

Parameters:
throwable - the exception to be thrown by the most recent method call on any mock

setAnswer

protected static void setAnswer(org.easymock.IAnswer answer)
Convienience for EasyMock.expectLastCall() with IExpectationSetters.andAnswer(org.easymock.IAnswer).

Parameters:
answer - callback for the most recent method invocation

unreachable

protected static void unreachable()
Invoked from code that should not be reachable. For example, place a call to unreachable() after invoking a method that is expected to throw an exception.


expect

protected static <T> org.easymock.IExpectationSetters<T> expect(T value)
Convienience for EasyMock.expect(Object).

Type Parameters:
T -
Parameters:
value -
Returns:
expectation setter, for setting return value, etc.

assertMessageContains

protected static void assertMessageContains(Throwable t,
                                            String... substrings)
Asserts that the message property of the throwable contains each of the provided substrings.

Parameters:
t - throwable to check
substrings - some number of expected substrings

assertListsEquals

protected static <T> void assertListsEquals(List<T> actual,
                                            List<T> expected)
Compares two lists for equality; first all the elements are individually compared for equality (if the lists are of unequal length, only elements up to the shorter length are compared). Then the length of the lists are compared. This generally gives

Type Parameters:
T - type of objects to compare
Parameters:
actual - actual values to check
expected - expected values

assertListsEquals

protected static <T> void assertListsEquals(List<T> actual,
                                            T... expected)
Convenience for assertListsEquals(List, List).

Type Parameters:
T - type of objects to compare
Parameters:
actual - actual values to check
expected - expected values

assertArraysEqual

protected static <T> void assertArraysEqual(T[] actual,
                                            T... expected)
Convenience for assertListsEquals(List, List).

Type Parameters:
T - type of objects to compare
Parameters:
actual - actual values to check
expected - expected values

newCapture

protected static <T> org.easymock.Capture<T> newCapture()
A factory method to create EasyMock Capture objects.


create

protected static <T> T create(Class<T> objectType,
                              Object... fieldValues)
Creates a new instance of the object using its default constructor, and initializes it (via set(Object, Object[])).

Parameters:
objectType - typeof object to instantiate
fieldValues - string field names and corresponding field values
Returns:
the initialized instance

set

protected static <T> T set(T object,
                           Object... fieldValues)
Initializes private fields (via reflection).

Parameters:
object - object to be updated
fieldValues - string field names and corresponding field values
Returns:
the object

get

protected static Object get(Object object,
                            String fieldName)
Reads the content of a private field.

Parameters:
object - to read the private field from
fieldName - name of field to read
Returns:
value stored in the field
Since:
5.1.0.5


Copyright © 2006-2009 Apache Software Foundation. All Rights Reserved.