You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -12,71 +12,79 @@ In turn this can be used by [Diffblue Cover](https://diffblue.com/cover) to tune
12
12
13
13
## Installation
14
14
15
-
Cover Annotations is published in the [Maven central repository](https://central.sonatype.com/artifact/com.diffblue.cover/cover-annotations/overview).
16
-
In order to use the annotations simply add `cover-annotations` as a dependency to your project, for example copying the snippet for Maven or Gradle from the repository page.
15
+
Cover Annotations is published in
16
+
the [Maven central repository](https://central.sonatype.com/artifact/com.diffblue.cover/cover-annotations/overview).
17
+
In order to use the annotations simply add `cover-annotations` as a dependency to your project, for example copying the
18
+
snippet for Maven or Gradle from the repository page.
17
19
18
20
### Maven
19
21
20
-
For installation into a Maven project the `provided` scope is recommended so that the annotations are available at compile and test time, but are not bundled with the project output:
22
+
For installation into a Maven project the `provided` scope is recommended so that the annotations are available at
23
+
compile and test time, but are not bundled with the project output:
21
24
22
25
```
23
26
<dependencies>
24
27
<dependency>
25
28
<groupId>com.diffblue.cover</groupId>
26
29
<artifactId>cover-annotations</artifactId>
27
-
<version>1.8.0</version>
30
+
<version>1.9.0</version>
28
31
<scope>provided</scope>
29
32
</dependency>
30
33
</dependencies>
31
34
```
32
35
33
36
### Gradle
34
37
35
-
For installation into a Gradle project the `compileOnly` and `testImplementation` configurations are recommended so that the annotations are available at compile and test time, but are not bundled with the project output:
38
+
For installation into a Gradle project the `compileOnly` and `testImplementation` configurations are recommended so that
39
+
the annotations are available at compile and test time, but are not bundled with the project output:
Perhaps you have a method that Diffblue Cover is unable to test, and you think it could make more progress using `Mockito.mockConstruction(Random.class)`.
100
+
Perhaps you have a method that Diffblue Cover is unable to test, and you think it could make more progress using
101
+
`Mockito.mockConstruction(Random.class)`.
93
102
In this case you could annotate the method (or class, or package) to recommend mocking construction of `Random`:
94
103
95
104
```java
96
105
public class ClassUnderTest {
97
-
@InTestsMockConstruction(Random.class)
98
-
public static int methodUnderTest() {
99
-
return new Random().nextInt();
100
-
}
106
+
@InTestsMockConstruction(Random.class)
107
+
public static int methodUnderTest() {
108
+
return new Random().nextInt();
109
+
}
101
110
}
102
111
```
103
112
104
113
> [!NOTE]
105
-
> Note that using `@InTestsMockConstruction` has the same effect as, and can be overridden by, Cover CLI command line option:
114
+
> Note that using `@InTestsMockConstruction` has the same effect as, and can be overridden by, Cover CLI command line
Perhaps you have a method that Diffblue Cover is unable to test, and you think it could make more progress using `Mockito.mockStatic(UUID.class)`.
123
+
Perhaps you have a method that Diffblue Cover is unable to test, and you think it could make more progress using
124
+
`Mockito.mockStatic(UUID.class)`.
114
125
In this case you could annotate the method (or class, or package) to recommend mocking static methods of `UUID`:
115
126
116
127
```java
117
128
public class ClassUnderTest {
118
-
@InTestsMockStatic(UUID.class)
119
-
public static Path methodUnderTest() {
120
-
return Paths.get(UUID.randomUUID() + ".zip");
121
-
}
129
+
@InTestsMockStatic(UUID.class)
130
+
public static Path methodUnderTest() {
131
+
return Paths.get(UUID.randomUUID() + ".zip");
132
+
}
122
133
}
123
134
```
124
135
@@ -136,7 +147,8 @@ Custom input annotations allow particular inputs to be recommended to Diffblue C
136
147
#### Using `@InTestsUseEnums`
137
148
138
149
The `@InTestsUseEnums` annotation allows the user to recommend specific `enum` literal values to use in tests.
139
-
Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is unable to identify values to cover all cases.
150
+
Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is
151
+
unable to identify values to cover all cases.
140
152
141
153
```java
142
154
public static boolean isDateOrTimeBased(@InTestsUseEnums({"SECONDS", "YEARS", "FOREVER"}) ChronoUnit chronoUnit) {
@@ -147,7 +159,8 @@ public static boolean isDateOrTimeBased(@InTestsUseEnums({"SECONDS", "YEARS", "F
147
159
#### Using `@InTestsUseClasses`
148
160
149
161
The `@InTestsUseClasses` annotation allows the user to recommend specific `Class` literal values to use in tests.
150
-
Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is unable to identify values to cover all cases.
162
+
Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is
163
+
unable to identify values to cover all cases.
151
164
For example the following method is annotated with an example class literal to achieve a positive test:
152
165
153
166
```java
@@ -159,8 +172,10 @@ public static boolean isAnnotation(@InTestsUseClasses(Nullable.class) Class<?> t
159
172
#### Using `@InTestsUseStrings`
160
173
161
174
The `@InTestsUseStrings` annotation allows the user to recommend specific `String` values to use in tests.
162
-
Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is unable to identify values to cover all cases.
163
-
For example the following method is annotated with some genuine examples of song titles that can be used to achieve coverage:
175
+
Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is
176
+
unable to identify values to cover all cases.
177
+
For example the following method is annotated with some genuine examples of song titles that can be used to achieve
178
+
coverage:
164
179
165
180
```java
166
181
publicstaticboolean isDayRelatedSongTitle(@InTestsUseStrings({"I Don't Like Mondays", "Here Comes The Weekend"}) String title) {
@@ -174,10 +189,13 @@ public static boolean isDayRelatedSongTitle(@InTestsUseStrings({"I Don't Like Mo
174
189
#### Using `@InTestsUseCharacters`
175
190
176
191
The `@InTestsUseCharacters` annotation allows the user to recommend specific `char` values to use in tests.
177
-
Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is unable to identify values to cover all cases.
178
-
For example the following method is annotated with a genuine examples characters that make up a Unicode surrogate pair that can be used to achieve a positive test:
192
+
Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is
193
+
unable to identify values to cover all cases.
194
+
For example the following method is annotated with a genuine examples characters that make up a Unicode surrogate pair
195
+
that can be used to achieve a positive test:
179
196
180
197
```java
198
+
181
199
@Nullable
182
200
publicstaticInteger toNullableCodePoint(
183
201
@InTestsUseCharacters('\uD801') char high,
@@ -192,31 +210,34 @@ public static Integer toNullableCodePoint(
192
210
#### Using `@InTestsUseBytes`
193
211
194
212
The `@InTestsUseBytes` annotation allows the user to recommend specific `byte` values to use in tests.
195
-
Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is unable to identify values to cover all cases.
213
+
Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is
214
+
unable to identify values to cover all cases.
196
215
For example the following method is annotated to use a specific preferred value:
The `@InTestsUseShorts` annotation allows the user to recommend specific `short` values to use in tests.
207
-
Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is unable to identify values to cover all cases.
226
+
Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is
227
+
unable to identify values to cover all cases.
208
228
For example the following method is annotated to use a specific preferred value:
209
229
210
230
```java
211
-
publicstaticString toUpperHexString(@InTestsUseShorts((short)0xD1FF) short input) {
231
+
publicstaticString toUpperHexString(@InTestsUseShorts((short)0xD1FF) short input) {
212
232
returnLong.toHexString(input).toUpperCase();
213
233
}
214
234
```
215
235
216
236
#### Using `@InTestsUseIntegers`
217
237
218
238
The `@InTestsUseIntegers` annotation allows the user to recommend specific `int` values to use in tests.
219
-
Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is unable to identify values to cover all cases.
239
+
Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is
240
+
unable to identify values to cover all cases.
220
241
For example the following method is annotated to use a specific preferred value:
221
242
222
243
```java
@@ -228,7 +249,8 @@ public static String toUpperHexString(@InTestsUseIntegers(0xD1FFB) int input) {
228
249
#### Using `@InTestsUseLongs`
229
250
230
251
The `@InTestsUseLongs` annotation allows the user to recommend specific `long` values to use in tests.
231
-
Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is unable to identify values to cover all cases.
252
+
Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is
253
+
unable to identify values to cover all cases.
232
254
For example the following method is annotated to use a specific preferred value:
233
255
234
256
```java
@@ -240,7 +262,8 @@ public static String toUpperHexString(@InTestsUseLongs(0xD1FFBL) long input) {
240
262
#### Using `@InTestsUseFloats`
241
263
242
264
The `@InTestsUseFloats` annotation allows the user to recommend specific `float` values to use in tests.
243
-
Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is unable to identify values to cover all cases.
265
+
Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is
266
+
unable to identify values to cover all cases.
244
267
For example the following method is annotated to use a specific preferred value:
The `@InTestsUseDoubles` annotation allows the user to recommend specific `double` values to use in tests.
255
-
Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is unable to identify values to cover all cases.
278
+
Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is
279
+
unable to identify values to cover all cases.
256
280
For example the following method is annotated to use a specific preferred value:
Interesting value annotations allow users to promote existing fields and methods to be identified and used in particular roles by Diffblue Cover when writing tests.
290
+
Interesting value annotations allow users to promote existing fields and methods to be identified and used in particular
291
+
roles by Diffblue Cover when writing tests.
267
292
268
293
#### Using `@InterestingTestFactory`
269
294
270
295
Indicates the annotated method as a useful factory method for use in tests.
271
-
Cover will automatically recognise factory methods that simply return a newly created instance, but may not identify more complicated factories.
296
+
Cover will automatically recognise factory methods that simply return a newly created instance, but may not identify
297
+
more complicated factories.
272
298
This annotation allows such factory methods to be manually annotated so that Cover considers them for producing inputs.
273
-
For example the following method under test takes a `User` as input, but the `User` constructor is private and Cover doesn't naturally consider `ofStaff(String)` to be a safe factory method to call.
274
-
By annotating the `ofStaff(String)` with `@InterstingTestFactory` we can tell Cover that this should be considered a good factory method to use in tests.
299
+
For example the following method under test takes a `User` as input, but the `User` constructor is private and Cover
300
+
doesn't naturally consider `ofStaff(String)` to be a safe factory method to call.
301
+
By annotating the `ofStaff(String)` with `@InterstingTestFactory` we can tell Cover that this should be considered a
302
+
good factory method to use in tests.
275
303
276
304
```java
277
305
publicString getUserDisplayString(User user) {
278
306
if (user.manager) {
279
307
return user.username +" (manager)";
280
-
}
281
-
else {
308
+
} else {
282
309
return user.username;
283
310
}
284
311
}
@@ -326,7 +353,7 @@ public class CarFactory {
326
353
publicstaticCargetFirstCar() {
327
354
returnINSTANCE.cars.get(0);
328
355
}
329
-
356
+
330
357
// and so on...
331
358
}
332
359
```
@@ -343,6 +370,33 @@ public class CarPainter {
343
370
}
344
371
```
345
372
373
+
### Test Organization Annotations
374
+
375
+
Test organization annotations allow users to control how tests are organized and structured.
376
+
377
+
#### Using `@WriteTestsTo`
378
+
379
+
The `@WriteTestsTo` annotation directs Diffblue Cover to write tests for a specific source class into a designated test
380
+
class file, rather than following the default naming template (configured via `--class-name-template`).
381
+
382
+
The specified test class name must be alphanumeric, and the test class will be created in the test folder under the same
383
+
package structure as the source class.
384
+
385
+
```java
386
+
packagecom.example.myapp;
387
+
388
+
@WriteTestsTo("CustomTestClassName")
389
+
publicclassSourceClass {
390
+
publicStringgetValue() {
391
+
return"example";
392
+
}
393
+
}
394
+
// Tests will be written to: src/test/java/com/example/myapp/CustomTestClassName.java
395
+
```
396
+
397
+
> [!NOTE]
398
+
> This annotation can only be applied at the class level.
399
+
346
400
### Experimental Annotations
347
401
348
402
Experimental annotations should not be used in a production setting, but are
0 commit comments