-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathdependency_injection.html
379 lines (363 loc) · 18.2 KB
/
dependency_injection.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Asciidoctor 2.0.18">
<title>Dependency injection</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300,300italic,400,400italic,600,600italic%7CNoto+Serif:400,400italic,700,700italic%7CDroid+Sans+Mono:400,700">
<link rel="stylesheet" href="./asciidoctor.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prettify/r298/prettify.min.css">
</head>
<body class="article toc2 toc-left">
<div id="header">
<div id="toc" class="toc2">
<div id="toctitle">Table of Contents</div>
<ul class="sectlevel2">
<li><a href="#_dependency_injection">Dependency injection</a></li>
</ul>
</div>
</div>
<div id="content">
<div class="sect2">
<h3 id="_dependency_injection">Dependency injection</h3>
<div class="paragraph">
<p>TestNG supports two different kinds of dependency injection: native (performed by TestNG itself) and external (performed by a dependency injection framework such as Guice).</p>
</div>
<div class="sect3">
<h4 id="_native_dependency_injection">Native dependency injection</h4>
<div class="paragraph">
<p>TestNG lets you declare additional parameters in your methods. When this happens, TestNG will automatically fill these parameters with the right value. Dependency injection can be used in the following places:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Any <code>@Before</code> method or <code>@Test</code> method can declare a parameter of type <code>ITestContext</code>.</p>
</li>
<li>
<p>Any <code>@AfterMethod</code> method can declare a parameter of type <code>ITestResult</code>, which will reflect the result of the test method that was just run.</p>
</li>
<li>
<p>Any <code>@Before</code> and <code>@After</code> methods (except <code>@BeforeSuite</code> and <code>@AfterSuite</code>) can declare a parameter of type <code>XmlTest</code>, which contain the current <code><test></code> tag.</p>
</li>
<li>
<p>Any <code>@BeforeMethod</code> (and <code>@AfterMethod</code>) can declare a parameter of type <code>java.lang.reflect.Method</code>. This parameter will receive the test method that will be called once this <code>@BeforeMethod</code> finishes (or after the method as run for <code>@AfterMethod</code>).</p>
</li>
<li>
<p>Any <code>@BeforeMethod</code> can declare a parameter of type <code>Object[]</code>. This parameter will receive the list of parameters that are about to be fed to the upcoming test method, which could be either injected by TestNG, such as <code>java.lang.reflect.Method</code> or come from a <code>@DataProvider</code>.</p>
</li>
<li>
<p>Any <code>@DataProvider</code> can declare a parameter of type <code>ITestContext</code> or <code>java.lang.reflect.Method</code>. The latter parameter will receive the test method that is about to be invoked.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>You can turn off injection with the <code>@NoInjection</code> annotation:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="java">public class NoInjectionTest {
@DataProvider(name = "provider")
public Object[][] provide() throws Exception {
return new Object[][] { { CC.class.getMethod("f") } };
}
@Test(dataProvider = "provider")
public void withoutInjection(@NoInjection Method m) {
Assert.assertEquals(m.getName(), "f");
}
@Test(dataProvider = "provider")
public void withInjection(Method m) {
Assert.assertEquals(m.getName(), "withInjection");
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>The below table summarises the parameter types that can be natively injected for the various TestNG annotations:</p>
</div>
<table class="tableblock frame-all grid-all stretch">
<colgroup>
<col style="width: 14.2857%;">
<col style="width: 14.2857%;">
<col style="width: 14.2857%;">
<col style="width: 14.2857%;">
<col style="width: 14.2857%;">
<col style="width: 14.2857%;">
<col style="width: 14.2858%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">Annotation</th>
<th class="tableblock halign-left valign-top">ITestContext</th>
<th class="tableblock halign-left valign-top">XmlTest</th>
<th class="tableblock halign-left valign-top">Method</th>
<th class="tableblock halign-left valign-top">Object[]</th>
<th class="tableblock halign-left valign-top">ITestResult</th>
<th class="tableblock halign-left valign-top">ConstructorOrMethod</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>BeforeSuite</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>BeforeTest</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">Yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">Yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>BeforeGroups</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">Yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">Yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>BeforeClass</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">Yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">Yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>BeforeMethod</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">Yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">Yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">Yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">Yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">Yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Test</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">Yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>DataProvider</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">Yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">Yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">Yes</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>AfterMethod</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">Yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">Yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">Yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">Yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">Yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>AfterClass</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">Yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">Yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>AfterGroups</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">Yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">Yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>AfterTest</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">Yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">Yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>AfterSuite</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Factory</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">Yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">Yes</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock">No</p></td>
</tr>
</tbody>
</table>
</div>
<div class="sect3">
<h4 id="_guice_dependency_injection">Guice dependency injection</h4>
<div class="paragraph">
<p>If you use <a href="https://github.com/google/guice/wiki/Motivation">Guice</a>, TestNG gives you an easy way to inject your test objects with a Guice module:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="java">@Guice(modules = GuiceExampleModule.class)
public class GuiceTest extends SimpleBaseTest {
@Inject
ISingleton m_singleton;
@Test
public void singletonShouldWork() {
m_singleton.doSomething();
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>In this example, <code>GuiceExampleModule</code> is expected to bind the interface <code>ISingleton</code> to some concrete class:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="java">public class GuiceExampleModule implements Module {
@Override
public void configure(Binder binder) {
binder.bind(ISingleton.class).to(ExampleSingleton.class).in(Singleton.class);
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>If you need more flexibility in specifying which modules should be used to instantiate your test classes, you can specify a module factory:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="java">@Guice(moduleFactory = ModuleFactory.class)
public class GuiceModuleFactoryTest {
@Inject
ISingleton m_singleton;
@Test
public void singletonShouldWork() {
m_singleton.doSomething();
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>The module factory needs to implement the interface {javadocs-base-url}/org/testng/IModuleFactory.html[org.testng.IModuleFactory]</p>
</div>
<div class="paragraph">
<p>Your factory will be passed an instance of the test context and the test class that TestNG needs to instantiate. Your <code>createModule()</code> method should return a Guice Module that will know how to instantiate this test class. You can use the test context to find out more information about your environment, such as parameters specified in testng.xml, etc…​ You will get even more flexibility and Guice power with <code>parent-module</code> and <code>guice-stage</code> suite parameters.</p>
</div>
<div class="paragraph">
<p><code>guice-stage</code> allow you to chose the <a href="https://github.com/google/guice/wiki/Bootstrap">Stage</a> used to create the parent injector. The default one is <code>DEVELOPMENT</code>. Other allowed values are <code>PRODUCTION</code> and <code>TOOL</code>. Here is how you can define <code>parent-module</code> in your <code>test.xml</code> file:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="xml"><suite parent-module="com.example.SuiteParenModule" guice-stage="PRODUCTION">
</suite></code></pre>
</div>
</div>
<div class="paragraph">
<p>TestNG will create this module only once for given suite. Will also use this module for obtaining instances of test specific Guice modules and module factories, then will create child injector for each test class. With such approach you can declare all common bindings in parent-module also you can inject binding declared in parent-module in module and module factory. Here is an example of this functionality:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="java">package com.example;
public class ParentModule extends AbstractModule {
@Override
protected void configure() {
bind(MyService.class).toProvider(MyServiceProvider.class);
bind(MyContext.class).to(MyContextImpl.class).in(Singleton.class);
}
}</code></pre>
</div>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="java">package com.example;
public class TestModule extends AbstractModule {
private final MyContext myContext;
@Inject
TestModule(MyContext myContext) {
this.myContext = myContext
}
@Override
protected void configure() {
bind(MySession.class).toInstance(myContext.getSession());
}
}</code></pre>
</div>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="xml"><suite parent-module="com.example.ParentModule">
</suite></code></pre>
</div>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="java">package com.example;
@Test
@Guice(modules = TestModule.class)
public class TestClass {
@Inject
MyService myService;
@Inject
MySession mySession;
public void testServiceWithSession() {
myService.serve(mySession);
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>As you see <code>ParentModule</code> declares binding for <code>MyService</code> and <code>MyContext</code> classes. Then <code>MyContext</code> is injected using constructor injection into <code>TestModule</code> class, which also declare binding for <code>MySession</code>. Then <code>parent-module</code> in test XML file is set to <code>ParentModule</code> class, this enables injection in <code>TestModule</code>. Later in <code>TestClass</code> you see two injections:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>MyService - binding taken from ParentModule</p>
</li>
<li>
<p>MySession - binding taken from TestModule</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>This configuration ensures you that all tests in this suite will be run with same session instance, the <code>MyContextImpl</code> object is only created once per suite, this give you possibility to configure common environment state for all tests in suite.</p>
</div>
</div>
</div>
</div>
<div id="footer">
<div id="footer-text">
Last updated 2025-02-27 18:51:48 UTC
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prettify/r298/run_prettify.min.js"></script>
</body>
</html>