Wiki source code of Java Unit Testing - JUnit4

Last modified by Vincent Massol on 2018/11/25 11:05

Show last authors
1 {{box cssClass="floatinginfobox" title="**Contents**"}}
2 {{toc/}}
3 {{/box}}
4
5 {{warning}}
6 This is now deprecated and you should [[use JUnit5>>doc:Community.Testing.JavaUnitTesting.WebHome]].
7 {{/warning}}
8
9 = Unit Testing =
10
11 * 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:(((
12 {{code language="java"}}
13 ...
14 /**
15 * Capture logs.
16 */
17 @Rule
18 public AllLogRule logRule = new AllLogRule();
19 ...
20 assertEquals("Error getting resource [bad resource] because of invalid path format. Reason: [invalid url]",
21 this.logRule.getMessage(0));
22 ...
23 {{/code}}
24 )))
25 * If you're testing Components, use ##MockitoComponentMockingRule## (its javadoc explains how to use it in details)(((
26 * Example 1: Canonical use case(((
27 {{code language="java"}}
28 public class DefaultDiffManagerTest
29 {
30 @Rule
31 public final MockitoComponentMockingRule<DiffManager> mocker =
32 new MockitoComponentMockingRule(DefaultDiffManager.class);
33 ...
34 @Test
35 public void testDiffStringList() throws Exception
36 {
37 // Null
38
39 DiffResult<String> result = this.mocker.getComponentUnderTest().diff(null, null, null);
40 ...
41 {{/code}}
42 )))
43 * Example 2: Example showing how to not mock one dependency being injected (##ObservationManager## is excluded in the example)(((
44 {{code language="java"}}
45 @ComponentList({DefaultLoggerManager.class, DefaultObservationManager.class, LogbackEventGenerator.class})
46 public class DefaultLoggerManagerTest
47 {
48 @Rule
49 public final MockitoComponentMockingRule<DefaultLoggerManager> mocker =
50 new MockitoComponentMockingRule<DefaultLoggerManager>(DefaultLoggerManager.class);
51 ...
52 {{/code}}
53 )))
54 )))
55 * If you're testing non Components, just use pure JUnit and Mockito (you're on your own!)
56 * Examples:
57 ** {{scm path="xwiki-platform-core/xwiki-platform-rendering/xwiki-platform-rendering-macros/xwiki-platform-rendering-macro-script/src/test/java/org/xwiki/rendering/macro/script/NestedScriptMacroValidatorTest.java"}}example1{{/scm}}
58
59 = Integration Testing =
60
61 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.
62
63 == Java Integration Testing ==
64
65 * These tests are written using JUnit 5.x and [[Mockito>>https://code.google.com/p/mockito/]] (we were using [[JMock 2.x>>http://jmock.org/]] before and still have a lot of tests not converted to Mockito yet).
66 * Your Maven module must depend on the ##xwiki-commons-test## module.
67 * Use the ##MockitoComponentMockingRule## Rule (its javadoc explains how to use it in details).
68 * Examples:
69 ** {{scm project="xwiki-rendering" path="xwiki-rendering-transformations/xwiki-rendering-transformation-macro/src/test/java/org/xwiki/rendering/internal/macro/DefaultMacroManagerTest.java"}}example2{{/scm}}
70 ** Other example:(((
71 {{code language="java"}}
72 @ComponentList({
73 DefaultExtensionLicenseManager.class
74 })
75 public class DefaultExtensionLicenseManagerTest
76 {
77 @Rule
78 public final ComponentManagerRule componentManager = new ComponentManagerRule();
79 ...
80 @Before
81 public void setUp() throws Exception
82 {
83 this.licenseManager = this.componentManager.getInstance(ExtensionLicenseManager.class);
84 }
85 ...
86 {{/code}}
87 )))
88 ** Another example (using the new ##@BeforeComponent## annotation):(((
89 {{code language="java"}}
90 @ComponentList({
91 DefaultExtensionManagerConfiguration.class,
92 DefaultLoggerManager.class,
93 DefaultObservationManager.class
94 })
95 public class DefaultExtensionManagerConfigurationTest
96 {
97 @Rule
98 public final MockitoComponentManagerRule componentManager = new MockitoComponentManagerRule();
99
100 private ExtensionManagerConfiguration configuration;
101
102 private MemoryConfigurationSource source;
103
104 @BeforeComponent
105 public void registerComponents() throws Exception
106 {
107 // Register a Mocked Environment since we need to provide one.
108 this.componentManager.registerMockComponent(Environment.class);
109
110 // Register some in-memory Configuration Source for the test
111 this.source = this.componentManager.registerMemoryConfigurationSource();
112 }
113
114 @Before
115 public void setUp() throws Exception
116 {
117 this.configuration = this.componentManager.getInstance(ExtensionManagerConfiguration.class);
118 }
119 ...
120 {{/code}}
121
122 ##@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).
123
124 ##MockitoComponentManagerRule## extends ##ComponentManagerRule## and just adds some helper methods to register a mock component.
125 )))

Get Connected