After my last blog on Unit testing a Confluence Action, I said I would follow up with a how to test a confluence macro. Here it is.
Setting up the project is the same as Unit testing a Confluence Action make sure you update your pom.xml with the things listed there.
Testing the macro is quite simple. Create a space, put a page in it, load the page and assert your result. In code form you want something like:
public void testMyMacro() {
String macro = "{macro}";
SpaceHelper spaceHelper = getSpaceHelper();
spaceHelper.setKey("MACROTESTSPACE");
spaceHelper.setName("MACRO TEST SPACE");
spaceHelper.setDescription("A space for testing my macro");
assertTrue("Error creating test space.", spaceHelper.create());
PageHelper pageHelper = getPageHelper();
pageHelper.setContent("Page content " + macro);
pageHelper.setSpaceKey("MACROTESTSPACE");
pageHelper.setTitle("Macro Test Page");
assertTrue("Error creating test page.", pageHelper.create());
gotoPage("/pages/viewpage.action?pageId=" + pageHelper.getId());
String macroExpectedOutput = "macro return string";
assertTrue(getPageSource().contains("Page content " + macroExpectedOutput));
}
Nice and simple.

