Show last authors
1 {{info}}Since XWiki 7.3M1{{/info}} It's now possible to write unit tests for wiki pages on the filesystem (in XML format).
2 {{info}}Since XWiki 10.5RC1{{/info}} Tests extending ##PageTest## must now be using JUnit5.
3
4 The way those tests work is that the XML file representing the wiki pages are loaded from the filesystem into ##XWikiDocument## instances and a stubbed environment is defined so that ##XWikiDocument## can then ben rendered in the desired syntax.
5
6 To write such a test:
7
8 * Make your POM depend on the ##org.xwiki.platform:xwiki-platform-test-page## module
9 * Write a Java test class that extends ##PageTest##
10 * Possibly add some extra component registration that you need through existing annotations (##*ComponentList## annotations) or through the custom ##ComponentList## annotation. Note that when extending ##PageTest## this automatically brings some base components registration (the list defined in ##PageComponentList## and ##ReferenceComponentList##). Example of other existing annotations:
11 ** ##XWikiSyntax20ComponentList##: XWiki Syntax 2.0-related components
12 ** ##XWikiSyntax21ComponentList##: XWiki Syntax 2.1-related components
13 ** ##XHTML10ComponentList##: XHTML 1.0-related components
14 * You then verify the rendering of a page by setting the output syntax you wish to have, the query parameters and then call ##renderPage()##. For example:(((
15 {{code language="java"}}
16 setOutputSyntax(Syntax.XHTML_1_0);
17 request.put("section", "Links");
18 request.put("xpage", "print");
19 ...
20 String result = renderPage(new DocumentReference("xwiki", "XWiki", "XWikiSyntax"));
21 assertTrue("...explanation if test fails", result.contains("...content that we wish to verify..."));
22 {{/code}}
23 )))
24 * If you need to have some other pages loaded from sources too (for example if the page you're rendering contains an include macro that loads another page), you'll use the(((
25 loadPage() API as in:##{{code language="java"}}loadPage(new DocumentReference("xwiki", "XWiki", "XWikiSyntaxLinks"));{{/code}}##
26 )))
27
28 Here's a full {{scm path="xwiki-platform-core/xwiki-platform-activitystream/xwiki-platform-activitystream-ui"}}example{{/scm}}:
29
30 {{code language="java"}}
31 @XWikiSyntax20ComponentList
32 @XHTML10ComponentList
33 public class WebRssTest extends PageTest
34 {
35 private ScriptQuery query;
36
37 @Before
38 public void setUp() throws Exception
39 {
40 setOutputSyntax(Syntax.PLAIN_1_0);
41 request.put("outputSyntax", "plain");
42 request.put("xpage", "plain");
43
44 QueryManagerScriptService qmss = mock(QueryManagerScriptService.class);
45 oldcore.getMocker().registerComponent(ScriptService.class, "query", qmss);
46 query = mock(ScriptQuery.class);
47 when(qmss.xwql("where 1=1 order by doc.date desc")).thenReturn(query);
48 }
49
50 @Test
51 public void webRssFiltersHiddenDocuments() throws Exception
52 {
53 // Render the page to test
54 renderPage(new DocumentReference("xwiki", "Main", "WebRss"));
55
56 // This is the real test!!
57 // We want to verify that the hidden document filter is called when executing the XWQL
58 // query to get the list of modified pages
59 verify(query).addFilter("hidden/document");
60 }
61
62 @Test
63 public void webRssDisplay() throws Exception
64 {
65 when(query.addFilter(anyString())).thenReturn(query);
66 when(query.setLimit(20)).thenReturn(query);
67 when(query.setOffset(0)).thenReturn(query);
68 when(query.execute()).thenReturn(Arrays.<Object>asList("Space1.Page1", "Space2.Page2"));
69
70 FeedPlugin plugin = new FeedPlugin("feed", FeedPlugin.class.getName(), context);
71 FeedPluginApi pluginApi = new FeedPluginApi(plugin, context);
72 when(xwiki.getPluginApi("feed", context)).thenReturn(pluginApi);
73
74 // Render the page to test
75 String xml = renderPage(new DocumentReference("xwiki", "Main", "WebRss"));
76
77 assertTrue(xml.contains("<title>activity.rss.feed.description</title>"));
78 assertTrue(xml.contains("<title>Page1</title>"));
79 assertTrue(xml.contains("<title>Page2</title>"));
80 }
81 }
82 {{/code}}

Get Connected