-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathannotation_transformers.html
162 lines (162 loc) · 5.64 KB
/
annotation_transformers.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
<!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>Annotation Transformers</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="sectlevel3">
<li><a href="#_annotation_transformers">Annotation Transformers</a></li>
<li><a href="#_via_xml_suite_file">Via xml suite file</a></li>
<li><a href="#_via_command_line_arguments">Via command line arguments</a></li>
<li><a href="#_via_your_code">Via your code</a></li>
</ul>
</div>
</div>
<div id="content">
<div class="sect3">
<h4 id="_annotation_transformers">Annotation Transformers</h4>
<div class="paragraph">
<p>TestNG allows you to modify the content of all the annotations at runtime.</p>
</div>
<div class="paragraph">
<p>This is especially useful if the annotations in the source code are right most of the time, but there are a few situations where you’d like to override their value.</p>
</div>
<div class="paragraph">
<p>In order to achieve this, you can build a class that implements <a href="https://javadoc.io/static/org.testng/testng/7.9.0/org/testng/IAnnotationTransformer.html">IAnnotationTransformer</a></p>
</div>
<div class="paragraph">
<p>This is a special TestNG listener. It can be added into TestNG via the following mechanisms.</p>
</div>
</div>
<div class="sect3">
<h4 id="_via_xml_suite_file">Via xml suite file</h4>
<div class="paragraph">
<p>You can use the <code><listeners></code> tag to specify an implementation of <code>IAnnotationTransformer</code> in your suite xml file.</p>
</div>
</div>
<div class="sect3">
<h4 id="_via_command_line_arguments">Via command line arguments</h4>
<div class="paragraph">
<p>You can use the command line argument <code>-listener</code> to specify the fully qualified class name of the implementation of
<code>IAnnotationTransformer</code> as shown below.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="bash">java org.testng.TestNG -listener MyTransformer testng.xml</code></pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_via_your_code">Via your code</h4>
<div class="paragraph">
<p>An implementation of <code>IAnnotationTransformer</code> can be wired in via your code as well (In case you are working with using the TestNG APIs for programmatically running your tests.)</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="java">TestNG tng = new TestNG();
tng.addListener(new MyTransformer());
// ...</code></pre>
</div>
</div>
<div class="admonitionblock warning">
<table>
<tr>
<td class="icon">
<i class="fa icon-warning" title="Warning"></i>
</td>
<td class="content">
Please don’t use the <code>@Listeners</code> annotation to wire-in an implementation of <code>org.testng.IAnnotationTransformer</code>.
Doing so will cause your implementation to be ignored. This is because TestNG needs to be able to parse all annotations
before starting to execute them and <code>@Listeners</code> is also one such annotation.
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>The annotation transformer allows you to alter the below types of annotations at runtime:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>@Test</code> annotation on test methods.</p>
</li>
<li>
<p>Any of the common attributes associated with the below listed configuration annotations:</p>
<div class="ulist">
<ul>
<li>
<p><code>@BeforeSuite</code></p>
</li>
<li>
<p><code>@AfterSuite</code></p>
</li>
<li>
<p><code>@BeforeTest</code></p>
</li>
<li>
<p><code>@AfterTest</code></p>
</li>
<li>
<p><code>@BeforeClass</code></p>
</li>
<li>
<p><code>@AfterClass</code></p>
</li>
<li>
<p><code>@BeforeMethod</code></p>
</li>
<li>
<p><code>@AfterMethod</code></p>
</li>
</ul>
</div>
</li>
<li>
<p><code>@Listeners</code> annotation on test classes.</p>
</li>
<li>
<p><code>@Factory</code> annotation used to mark constructors or a factory method as test factories.</p>
</li>
<li>
<p><code>@DataProvider</code> annotated data providers.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>When the method <code>transform()</code> is invoked, you can call any of the setters on the <code>ITestAnnotation</code> test parameter to alter its value before TestNG proceeds further.</p>
</div>
<div class="paragraph">
<p>For example, here is how you would override the attribute invocationCount but only on the test method invoke() of one of your test classes:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="java">public class MyTransformer implements IAnnotationTransformer {
public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
if ("invoke".equals(testMethod.getName())) {
annotation.setInvocationCount(5);
}
}
}</code></pre>
</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>