Java Unit Testing - JUnit4
Last modified by Vincent Massol on 2018/11/25 11:05
Unit Testing
- These tests must not output anything to stdout or stderr (or it'll fail the build). Note that if the code under tests output logs, they need to be captured and possibly asserted. For example:
... /** * Capture logs. */ @Rule public AllLogRule logRule = new AllLogRule(); ... assertEquals("Error getting resource [bad resource] because of invalid path format. Reason: [invalid url]", this.logRule.getMessage(0)); ...
- If you're testing Components, use MockitoComponentMockingRule (its javadoc explains how to use it in details)
- Example 1: Canonical use case
public class DefaultDiffManagerTest { @Rule public final MockitoComponentMockingRule<DiffManager> mocker = new MockitoComponentMockingRule(DefaultDiffManager.class); ... @Test public void testDiffStringList() throws Exception { // Null DiffResult<String> result = this.mocker.getComponentUnderTest().diff(null, null, null); ...
- Example 2: Example showing how to not mock one dependency being injected (ObservationManager is excluded in the example)
@ComponentList({DefaultLoggerManager.class, DefaultObservationManager.class, LogbackEventGenerator.class}) public class DefaultLoggerManagerTest { @Rule public final MockitoComponentMockingRule<DefaultLoggerManager> mocker = new MockitoComponentMockingRule<DefaultLoggerManager>(DefaultLoggerManager.class); ...
- Example 1: Canonical use case
- If you're testing non Components, just use pure JUnit and Mockito (you're on your own!)
- Examples:
Integration Testing
An integration test tests several classes together but without a running XWiki instance. For example if you have one Component which has dependencies on other Components and they are tested together this is called Integration testing.
Java Integration Testing
- These tests are written using JUnit 5.x and Mockito (we were using JMock 2.x before and still have a lot of tests not converted to Mockito yet).
- Your Maven module must depend on the xwiki-commons-test module.
- Use the MockitoComponentMockingRule Rule (its javadoc explains how to use it in details).
- Examples:
- example2
- Other example:
@ComponentList({ DefaultExtensionLicenseManager.class }) public class DefaultExtensionLicenseManagerTest { @Rule public final ComponentManagerRule componentManager = new ComponentManagerRule(); ... @Before public void setUp() throws Exception { this.licenseManager = this.componentManager.getInstance(ExtensionLicenseManager.class); } ...
- Another example (using the new @BeforeComponent annotation):
@ComponentList({ DefaultExtensionManagerConfiguration.class, DefaultLoggerManager.class, DefaultObservationManager.class }) public class DefaultExtensionManagerConfigurationTest { @Rule public final MockitoComponentManagerRule componentManager = new MockitoComponentManagerRule(); private ExtensionManagerConfiguration configuration; private MemoryConfigurationSource source; @BeforeComponent public void registerComponents() throws Exception { // Register a Mocked Environment since we need to provide one. this.componentManager.registerMockComponent(Environment.class); // Register some in-memory Configuration Source for the test this.source = this.componentManager.registerMemoryConfigurationSource(); } @Before public void setUp() throws Exception { this.configuration = this.componentManager.getInstance(ExtensionManagerConfiguration.class); } ...
@BeforeComponent is used by ComponentManagerRule and methods annotated with it are called before other components are registered (i.e. before processing of @AllComponents and @ComponentList annotations).
MockitoComponentManagerRule extends ComponentManagerRule and just adds some helper methods to register a mock component.