Skip to content

Commit 714f9a7

Browse files
authored
Merge pull request #2 from diffblue/roxspring/docs-update
@InTestsUseEnums + Docs
2 parents fe4974d + 0e19cae commit 714f9a7

15 files changed

+128
-12
lines changed

README.md

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,17 @@ dependencies {
3737
}
3838
```
3939

40-
## Annotations
41-
4240
Annotations placed on packages affect tests for all classes and methods under test in that package.
4341
Annotations placed on classes affect tests for that class and all it's methods under test, overriding package level annotations.
4442
Annotations placed on methods affect just that method under test, overriding package and class level annotations.
4543

46-
| Annotation | Equivalent `dcover create` option |
47-
|:---------------------------|:----------------------------------|
48-
| `@InTestsMock` | `--mock`, `--disable-mock-inputs` |
49-
| `@InTestsMockConstruction` | `--mock-construction` |
50-
| `@InTestsMockStatic` | `--mock-static` |
51-
5244
The annotations will be respected by Diffblue Cover via both command line and IntelliJ Plugin.
5345
When used from the command line in conjunction with equivalent options then the command line options take priority over the annotations found.
5446

47+
## Mocking Annotations
48+
49+
Mocking annotations allow fine grained control over what mocking should be preferred when testing.
50+
5551
### Using `@InTestsMock`
5652

5753
Perhaps you have a method that Diffblue Cover would ordinarily test using an `Integer` but you'd prefer to see it tested using `Mockito.mock(..)`.
@@ -77,6 +73,13 @@ public class ClassUnderTest {
7773
}
7874
```
7975

76+
> [!NOTE]
77+
> Using `@InTestsMock` has the same effect as, and can be overridden by, Cover CLI command line options:
78+
>
79+
> ```
80+
> dcover create --mock ClassToMock --disable-mock-inputs ClassToForbidMocking
81+
> ```
82+
8083
### Using `@InTestsMockConstruction`
8184
8285
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)`.
@@ -91,6 +94,13 @@ public class ClassUnderTest {
9194
}
9295
```
9396
97+
> [!NOTE]
98+
> Note that using `@InTestsMockConstruction` has the same effect as, and can be overridden by, Cover CLI command line option:
99+
>
100+
> ```
101+
> dcover create --mock-construction ClassToMockConstruction
102+
> ```
103+
94104
### Using `@InTestsMockStatic`
95105
96106
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)`.
@@ -105,6 +115,28 @@ public class ClassUnderTest {
105115
}
106116
```
107117
118+
> [!NOTE]
119+
> Using `@InTestsMockStatic` has the same effect as, and can be overridden by, Cover CLI command line option:
120+
>
121+
> ```
122+
> dcover create --mock-static ClassToMockStatic
123+
> ```
124+
125+
## Custom Input Annotations
126+
127+
Custom input annotations allow particular inputs to be recommended to Diffblue Cover when writing tests.
128+
129+
### Using `@InTestsUseEnum`
130+
131+
The `@InTestsUseEnum` annotation allows the user to recommend specific `enum` literal values to use in tests.
132+
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.
133+
134+
```java
135+
public static boolean isDateOrTimeBased(@InTestsUseEnums({"SECONDS", "YEARS", "FOREVER"}) ChronoUnit chronoUnit) {
136+
return chronoUnit.isDateBased() || chronoUnit.isTimeBased();
137+
}
138+
```
139+
108140
### Using `@InTestsUseClasses`
109141

110142
The `@InTestsUseClasses` annotation allows the user to recommend specific `Class` literal values to use in tests.
@@ -126,9 +158,9 @@ For example the following method is annotated with some genuine examples of song
126158
```java
127159
public static boolean isDayRelatedSongTitle(@InTestsUseStrings({"I Don't Like Mondays", "Here Comes The Weekend"}) String title) {
128160
return Stream.of(DayOfWeek.values())
129-
.map(DayOfWeek::name)
130-
.map(String::toLowerCase)
131-
.anyMatch(title.toLowerCase()::contains);
161+
.map(DayOfWeek::name)
162+
.map(String::toLowerCase)
163+
.anyMatch(title.toLowerCase()::contains);
132164
}
133165
```
134166

@@ -222,6 +254,10 @@ public static boolean isNearPi(@InTestsUseDoubles(Math.PI) float input) {
222254
}
223255
```
224256

257+
## Interesting Value Annotations
258+
259+
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.
260+
225261
### Using `@InterestingTestFactory`
226262

227263
Indicates the annotated method as a useful factory method for use in tests.

src/main/java/com/diffblue/cover/annotations/InTestsMock.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
* #value()}. Without annotation then mocking is assumed to be {@link MockDecision#ALLOWED}, but
3030
* with annotation then the decision defaults to {@link MockDecision#RECOMMENDED}. The decision can
3131
* be overridden with an explicit {@link #decision()} value.
32+
*
33+
* @since Diffblue Cover 2024.04.02
3234
*/
3335
@Retention(CLASS)
3436
@Target({PACKAGE, TYPE, METHOD})

src/main/java/com/diffblue/cover/annotations/InTestsMockConstruction.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
* MockDecision#ALLOWED}, but with annotation then the decision defaults to {@link
3131
* MockDecision#RECOMMENDED}. The decision can be overridden with an explicit {@link #decision()}
3232
* value.
33+
*
34+
* @since Diffblue Cover 2024.04.02
3335
*/
3436
@Retention(CLASS)
3537
@Repeatable(InTestsMockConstruction.Repeatable.class)

src/main/java/com/diffblue/cover/annotations/InTestsMockStatic.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
* MockDecision#ALLOWED}, but with annotation then the decision defaults to {@link
3131
* MockDecision#RECOMMENDED}. The decision can be overridden with an explicit {@link #decision()}
3232
* value.
33+
*
34+
* @since Diffblue Cover 2024.04.02
3335
*/
3436
@Retention(CLASS)
3537
@Repeatable(InTestsMockStatic.Repeatable.class)

src/main/java/com/diffblue/cover/annotations/InTestsUseBytes.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
/**
2828
* Identifies values that should be considered when writing tests that require inputs of type {@code
2929
* byte} or {@link Byte}.
30+
*
31+
* @since Diffblue Cover 2024.05.02
3032
*/
3133
@Retention(RUNTIME)
3234
@Repeatable(InTestsUseBytes.Repeatable.class)

src/main/java/com/diffblue/cover/annotations/InTestsUseCharacters.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
/**
2828
* Identifies values that should be considered when writing tests that require inputs of type {@code
2929
* char} or {@link Character}.
30+
*
31+
* @since Diffblue Cover 2024.05.02
3032
*/
3133
@Retention(RUNTIME)
3234
@Repeatable(InTestsUseCharacters.Repeatable.class)

src/main/java/com/diffblue/cover/annotations/InTestsUseClasses.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
/**
2828
* Identifies values that should be considered when writing tests that require inputs of type {@link
2929
* Class}.
30+
*
31+
* @since Diffblue Cover 2024.05.02
3032
*/
3133
@Retention(RUNTIME)
3234
@Repeatable(InTestsUseClasses.Repeatable.class)

src/main/java/com/diffblue/cover/annotations/InTestsUseDoubles.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
/**
2828
* Identifies values that should be considered when writing tests that require inputs of type {@code
2929
* double} or {@link Double}.
30+
*
31+
* @since Diffblue Cover 2024.05.02
3032
*/
3133
@Retention(RUNTIME)
3234
@Repeatable(InTestsUseDoubles.Repeatable.class)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2024 Diffblue Limited.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
package com.diffblue.cover.annotations;
16+
17+
import static java.lang.annotation.ElementType.METHOD;
18+
import static java.lang.annotation.ElementType.PACKAGE;
19+
import static java.lang.annotation.ElementType.PARAMETER;
20+
import static java.lang.annotation.ElementType.TYPE;
21+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
22+
23+
import java.lang.annotation.Repeatable;
24+
import java.lang.annotation.Retention;
25+
import java.lang.annotation.Target;
26+
27+
/**
28+
* Identifies values that should be considered when writing tests that require inputs of type {@link
29+
* Enum}.
30+
*
31+
* @since Diffblue Cover 2024.06.01
32+
*/
33+
@Retention(RUNTIME)
34+
@Repeatable(InTestsUseEnums.Repeatable.class)
35+
public @interface InTestsUseEnums {
36+
37+
/** Collects multiple {@link InTestsUseEnums} annotations. */
38+
@Retention(RUNTIME)
39+
@Target({PACKAGE, TYPE, METHOD, PARAMETER})
40+
@interface Repeatable {
41+
42+
/**
43+
* @return the repeated {@link InTestsUseEnums} annotations.
44+
*/
45+
InTestsUseEnums[] value();
46+
}
47+
48+
/**
49+
* @return {@link String} values that should be tried as names of the annotated enum.
50+
*/
51+
String[] value() default {};
52+
}

src/main/java/com/diffblue/cover/annotations/InTestsUseFloats.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
/**
2828
* Identifies values that should be considered when writing tests that require inputs of type {@code
2929
* float} or {@link Float}.
30+
*
31+
* @since Diffblue Cover 2024.05.02
3032
*/
3133
@Retention(RUNTIME)
3234
@Repeatable(InTestsUseFloats.Repeatable.class)

src/main/java/com/diffblue/cover/annotations/InTestsUseIntegers.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
/**
2828
* Identifies values that should be considered when writing tests that require inputs of type {@code
2929
* int} or {@link Integer}.
30+
*
31+
* @since Diffblue Cover 2024.05.02
3032
*/
3133
@Retention(RUNTIME)
3234
@Repeatable(InTestsUseIntegers.Repeatable.class)

src/main/java/com/diffblue/cover/annotations/InTestsUseLongs.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
/**
2828
* Identifies values that should be considered when writing tests that require inputs of type {@code
2929
* long} or {@link Long}.
30+
*
31+
* @since Diffblue Cover 2024.05.02
3032
*/
3133
@Retention(RUNTIME)
3234
@Repeatable(InTestsUseLongs.Repeatable.class)

src/main/java/com/diffblue/cover/annotations/InTestsUseShorts.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
/**
2828
* Identifies values that should be considered when writing tests that require inputs of type {@code
2929
* short} or {@link Short}.
30+
*
31+
* @since Diffblue Cover 2024.05.02
3032
*/
3133
@Retention(RUNTIME)
3234
@Repeatable(InTestsUseShorts.Repeatable.class)

src/main/java/com/diffblue/cover/annotations/InTestsUseStrings.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
/**
2828
* Identifies values that should be considered when writing tests that require inputs of type {@link
2929
* String}.
30+
*
31+
* @since Diffblue Cover 2024.05.02
3032
*/
3133
@Retention(RUNTIME)
3234
@Repeatable(InTestsUseStrings.Repeatable.class)

src/main/java/com/diffblue/cover/annotations/InterestingTestFactory.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
import java.lang.annotation.Retention;
2121
import java.lang.annotation.Target;
2222

23-
/** Indicates the annotated method as a useful factory method for use in tests. */
23+
/**
24+
* Indicates the annotated method as a useful factory method for use in tests.
25+
*
26+
* @since Diffblue Cover 2024.05.02
27+
*/
2428
@Retention(RUNTIME)
2529
@Target(METHOD)
2630
public @interface InterestingTestFactory {}

0 commit comments

Comments
 (0)