Class OorianTestHarness
Provides a lightweight test environment that allows pages to be rendered and inspected without a servlet container or browser. The harness manages mock objects for the application context, HTTP session, request, and response.
Quick Start:
// One-time setup (e.g., in @BeforeClass)
OorianTestHarness.setup();
// Render a page
String html = OorianTestHarness.render(new MyPage(), "/my-page");
// Inspect the output
assert html.contains("<title>My Page</title>");
// Teardown (e.g., in @AfterClass)
OorianTestHarness.teardown();
Advanced Usage:
OorianTestHarness.setup();
// Create custom request with parameters
TestHttpRequest request = OorianTestHarness.createRequest("/product/42");
request.setParameter("format", "json");
request.setHeader("Accept-Language", "fr-FR");
// Render with custom request
TestHttpResponse response = OorianTestHarness.render(new ProductPage(), request);
String html = response.getContent();
int status = response.getStatus();
- Since:
- 2.1
- Version:
- 1.0
- Author:
- Marvin P. Warble Jr.
-
Method Summary
Modifier and TypeMethodDescriptionstatic TestHttpRequestcreateRequest(String path) Creates a newTestHttpRequestfor the specified path.static TestHttpResponseCreates a newTestHttpResponse.static TestAppContextReturns the test application context.static TestHttpSessionReturns the shared test session.static TestHttpResponserender(HttpFile page, TestHttpRequest request) Renders a page with a custom request and returns the response.static StringRenders a page and returns the HTML output.static voidResets the shared session, clearing all session attributes.static voidsetup()Initializes the test environment with default settings.static voidsetup(TestAppContext context) Initializes the test environment with a custom application context.static voidteardown()Cleans up the test environment.
-
Method Details
-
setup
public static void setup()Initializes the test environment with default settings.Creates a
TestAppContextwith an empty context path and a sharedTestHttpSession. The application context is registered withOorianSessionso that session management works during rendering.Call this method once before running tests (e.g., in a
@BeforeClassmethod). -
setup
Initializes the test environment with a custom application context.- Parameters:
context- the application context to use
-
teardown
public static void teardown()Cleans up the test environment.Resets the application context and shared session. Call this method after all tests have completed (e.g., in an
@AfterClassmethod). -
createRequest
Creates a newTestHttpRequestfor the specified path.The request is pre-configured with the shared test session and the context path from the test application context.
- Parameters:
path- the request path (e.g., "/my-page")- Returns:
- a new test request
-
createResponse
Creates a newTestHttpResponse.- Returns:
- a new test response
-
render
Renders a page and returns the HTML output.This is the simplest way to render a page for testing. It creates a default request for the specified path, registers a session, renders the page, and returns the HTML content.
- Parameters:
page- the page to renderpath- the request path (e.g., "/my-page")- Returns:
- the rendered HTML content
-
render
Renders a page with a custom request and returns the response.Registers the session for the current thread, then invokes the full page lifecycle (
initializeFile,createFile, rendering). The complete response (content, status, headers) is available on the returnedTestHttpResponse.- Parameters:
page- the page to renderrequest- the test request- Returns:
- the test response containing the rendered output
-
getSession
Returns the shared test session.The shared session persists across multiple
render(com.oorian.HttpFile, java.lang.String)calls within the same test setup, allowing session attributes to accumulate as they would in a real user session.- Returns:
- the shared test session
-
getAppContext
Returns the test application context.- Returns:
- the test application context
-
resetSession
public static void resetSession()Resets the shared session, clearing all session attributes.Creates a new empty session. Useful between tests to ensure a clean session state.
-