org.apache.tapestry
Class TestBase

java.lang.Object
  extended by org.testng.Assert
      extended by org.apache.tapestry.TestBase
Direct Known Subclasses:
BaseComponentTestCase

public class TestBase
extends org.testng.Assert

A base class for creating TestNG unit tests for Tapestry 4 applications. With slightly more effort, this may be used as a utility class with other frameworks, such as JUnit.

A single strict mock control is used for all mocks, which means that order of operations is checked not just for any single mock but across mocks.

Provides common mock factory and mock trainer methods.

Provides easy access to instantiated component instances.

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

TestNG supports running tests in parallel, as does this class. The EasyMock control is stored in a thread local. This is necessary as TestNG instantiates a single instance of the test case class, and invokes methods on it from multiple threads.

Author:
Howard M. Lewis Ship

Constructor Summary
TestBase()
           
 
Method Summary
 void cleanupControlSource()
          Discards any mock objects created during the test.
 org.easymock.IMocksControl getMocksControl()
          Returns the control object used for all mocks created by this test case.
<T> T
newInstance(Class<T> componentClass, Object... properties)
          Creates a new instance of the provided class using the Creator utility.
 org.apache.tapestry.IMarkupWriter newMarkupWriter()
          Creates a mock for IMarkupWriter.
<T> T
newMock(Class<T> mockClass)
          Creates a new mock object of the indicated type.
 org.apache.tapestry.IRequestCycle newRequestCycle()
          Simply returns a new mock around IRequestCycle.
 void replay()
          Replay's the mocks control, preparing all mocks for testing.
<T> void
setReturnValue(T returnValue)
          Sets the return value for the most recent method call upon the mock.
 void setThrowable(Throwable throwable)
          Trains a mock object to throw an exception.
 void unreachable()
          Invoked to indicate code should not reach a point.
 void verify()
          Verifies the mocks control, ensuring that all mocks completed all trained method invocations, then resets the control to allow more training of the mocks.
 
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

newInstance

public final <T> T newInstance(Class<T> componentClass,
                               Object... properties)
Creates a new instance of the provided class using the Creator utility.

Parameters:
componentClass - the component type to build
properties - alternating property names and property values to be injected into the instance
Returns:
the instantiated and configured component instance

cleanupControlSource

public final void cleanupControlSource()
Discards any mock objects created during the test. When using TestBase as a utility class, not a base class, you must be careful to either invoke this method, or discard the TestBase instance at the end of each test.


newMock

public final <T> T newMock(Class<T> mockClass)
Creates a new mock object of the indicated type. The created object is retained for the duration of the test (specifically to support replay() and verify()).

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

replay

public final void replay()
Replay's the mocks control, preparing all mocks for testing.


verify

public final void verify()
Verifies the mocks control, ensuring that all mocks completed all trained method invocations, then resets the control to allow more training of the mocks.


getMocksControl

public final org.easymock.IMocksControl getMocksControl()
Returns the control object used for all mocks created by this test case.

Returns:
The IMocksControl used to manage mocks.

setReturnValue

public final <T> void setReturnValue(T returnValue)
Sets the return value for the most recent method call upon the mock.

Parameters:
returnValue - value to be returned from the method call

setThrowable

public final void setThrowable(Throwable throwable)
Trains a mock object to throw an exception.

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

unreachable

public final void unreachable()
Invoked to indicate code should not reach a point. This is typically used after code that should throw an exception.


newRequestCycle

public final org.apache.tapestry.IRequestCycle newRequestCycle()
Simply returns a new mock around IRequestCycle.

Returns:
The mocked request.

newMarkupWriter

public final org.apache.tapestry.IMarkupWriter newMarkupWriter()
Creates a mock for IMarkupWriter.

Returns:
The mock.


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