Test Module

Last modified by Vincent Massol on 2021/03/17 21:48

cogGeneric test framework that make it easy to test Components
TypeJAR
Category
Developed by

XWiki Development Team

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1
Bundled With

XWiki Standard

Compatibility

Since XWiki 4.2RC1

Table of contents

Description

See also the Testing section.

Capturing Logs

It's a good practice to capture logs output by code under test for the following reasons:

  • It pollutes the maven logs unnecessarily. For example imagine that you're expecting an error in your test and that the stacktrace is logged to the console. When looking at the maven console logs the reader will wonder if there was an error or if it was expected.
  • The test must assert whatever it needs to validate the code under test. So if it expects a log to be printed it should assert it.

In order to make this easy we have a JUnit LogRule. Here's an example usage:

public class RestrictParseLocationEventHandlerTest
{
  private RestrictParseLocationEventHandler handler = new RestrictParseLocationEventHandler();

  /**
    * Capture logs with WARN or higher severity to assert them.
    */

  @Rule public LogRule logRule = new LogRule() {{
       record(LogLevel.WARN);
       recordLoggingForType(RestrictParseLocationEventHandler.class);
  }};

  @Test
  public void includeEventWhenIllegalPath()
  {
       Assert.assertNull("Template shouldn't have been returned",
          this.handler.includeEvent("../WEB-INF/xwiki.cfg", "xwiki:Main.WebHome", "parse"));

       Assert.assertEquals(1, this.logRule.size());
       Assert.assertTrue(this.logRule.contains(
          "Direct access to template file [/WEB-INF/xwiki.cfg] refused. Possible break-in attempt!"));
  }
}
Tags:
    

Get Connected