@BrxmComponentTest // beanPackages, nodeTypes auto-detected
class MyComponentTest {
@Test
void testComponent(DynamicComponentTest brxm) {
// Create and initialize component
MyComponent component = new MyComponent();
component.init(null, brxm.getComponentConfiguration());
// Execute
component.doBeforeRender(brxm.getHstRequest(), brxm.getHstResponse());
// Assert on model attributes
MyModel model = brxm.getRequestAttributeValue("model");
assertNotNull(model);
}
}
Node Type Auto-Detection: Types are detected from @Node(jcrType="...") annotations.
@BrxmComponentTest(
beanPackages = {"org.example.beans"},
content = "/test-content.yaml",
contentRoot = "/content/documents/myproject"
)
class HeroBannerTest {
@Test
void testWithDocumentParameter(DynamicComponentTest brxm) {
// Create component
HeroBanner component = new HeroBanner();
component.init(null, brxm.getComponentConfiguration());
// Mock the ParametersInfo interface
HeroBannerInfo paramInfo = mock(HeroBannerInfo.class);
when(paramInfo.getDocument()).thenReturn("herobanners/test-hero");
brxm.setComponentParameters(paramInfo);
// Execute
component.doBeforeRender(brxm.getHstRequest(), brxm.getHstResponse());
// Assert
HeroBannerModel model = brxm.getRequestAttributeValue("heroBanner");
assertThat(model.getTitle()).isEqualTo("Welcome");
}
@Test
void testWithMultipleParameters(DynamicComponentTest brxm) {
CardCollectionSectionInfo paramInfo = mock(CardCollectionSectionInfo.class);
when(paramInfo.getTitle()).thenReturn("Featured Cards");
when(paramInfo.getPageSize()).thenReturn(3);
when(paramInfo.getSortOrder()).thenReturn("desc");
brxm.setComponentParameters(paramInfo);
// Execute and verify...
}
}
@Test
void doBeforeRender_setsExpectedAttributes(DynamicComponentTest brxm) {
component.doBeforeRender(brxm.getHstRequest(), brxm.getHstResponse());
// Type-safe retrieval
HeroBannerModel model = brxm.getRequestAttributeValue("heroBanner");
assertThat(model).isNotNull();
assertThat(model.getTitle()).isEqualTo("Expected Title");
assertThat(model.getDescription()).contains("expected text");
// Boolean attributes
Boolean isLoggedIn = brxm.getRequestAttributeValue("loggedin");
assertThat(isLoggedIn).isTrue();
// Null checks for optional attributes
assertThat((Object) brxm.getRequestAttributeValue("optionalAttr")).isNull();
}
@Test
void doBeforeRender_withSearchQuery_returnsFilteredResults(DynamicComponentTest brxm) {
brxm.addRequestParameter("q", "golden retriever");
brxm.addRequestParameter("category", "dogs");
brxm.addRequestParameter("page", "2");
component.doBeforeRender(brxm.getHstRequest(), brxm.getHstResponse());
SearchResults results = brxm.getRequestAttributeValue("searchResults");
assertThat(results.getQuery()).isEqualTo("golden retriever");
assertThat(results.getCurrentPage()).isEqualTo(2);
}
@Test
void testWithSession(DynamicComponentTest brxm) {
// Get or create session (lazy initialization)
HttpSession session = brxm.getHstRequest().getSession();
session.setAttribute("user", new User("John"));
component.doBeforeRender(brxm.getHstRequest(), brxm.getHstResponse());
// Verify session was used
assertThat(session.getAttribute("loginCount")).isEqualTo(1);
}
@Test
void testWithMockedSession(DynamicComponentTest brxm) {
HttpSession session = mock(HttpSession.class);
when(session.getAttribute("user")).thenReturn(new User("Jane"));
brxm.getHstRequest().setSession(session);
component.doBeforeRender(brxm.getHstRequest(), brxm.getHstResponse());
}
| Parameter | Type | Default | Description |
|---|---|---|---|
beanPackages |
String[] | auto-detected | HST content bean packages |
nodeTypes |
String[] | auto-detected | Node types (from @Node annotations) |
content |
String | "" | Classpath path to YAML file |
contentRoot |
String | "" | JCR path where content is imported |
| Method | Description |
|---|---|
getHstRequest() |
Mock HST request |
getHstResponse() |
Mock HST response |
getComponentConfiguration() |
Mock component config for init() |
getRequestAttributeValue(name) |
Type-safe attribute retrieval |
setComponentParameters(params) |
Set mocked ParametersInfo |
addRequestParameter(name, value) |
Add query parameter |
getRootNode() |
JCR root node |
setSiteContentBasePath(path) |
Set content base path |
Always include a basic infrastructure verification test:
@Test
@DisplayName("Infrastructure: HST request is properly initialized")
void infrastructure_hstRequestInitialized(DynamicComponentTest brxm) {
assertThat(brxm.getHstRequest()).isNotNull();
assertThat(brxm.getHstResponse()).isNotNull();
}