@@ -16,19 +16,31 @@ is desirable, as it increases the coverage, but it has a downside. It is possibl
16
16
most of generated branches would be ` NPE ` branches, while useful paths could be lost due to timeout.
17
17
18
18
Beyond that, in many cases the ` null ` value of a field can't be generated using the public API
19
- of the class. This is particularly true for final fields, especially in system classes.
19
+ of the class.
20
+
21
+ - First of all, this is particularly true for final fields, especially in system classes.
20
22
it is also often true for non-public fields from standard library and third-party libraries (even setters often do not
21
23
allow ` null ` values). Automatically generated tests assign ` null ` values to fields using reflection,
22
24
but these tests may be uninformative as the corresponding ` NPE ` branches would never occur
23
25
in the real code that limits itself to the public API.
24
26
27
+ - After that, field may be declared with some annotation that shows that null value is actually impossible.
28
+ For example, in Spring applications ` @InjectMocks ` and ` @Mock ` annotations on the fields of class under test
29
+ mean that these fields always have value, so ` NPE ` branches for them would never occur in real code.
30
+
31
+
25
32
## The solution
26
33
27
34
To discard irrelevant ` NPE ` branches, we can speculatively mark fields we as non-nullable even they
28
- do not have an explicit ` @NotNull ` annotation. In particular, we can use this approach to final and non-public
29
- fields of system classes, as they are usually correctly initialized and are not equal ` null ` .
35
+ do not have an explicit ` @NotNull ` annotation.
36
+
37
+ - In particular, we can use this approach to final and non-public
38
+ fields of system classes, as they are usually correctly initialized and are not equal ` null `
39
+ - For Spring application, we use this approach for the fields of class
40
+ under test not obtained from Spring bean definitions
30
41
31
- At the same time, we can't always add the "not null" hard constraint for the field: it would break
42
+ At the same time, for non-Spring classes,
43
+ we can't always add the "not null" hard constraint for the field: it would break
32
44
some special cases like ` Optional<T> ` class, which uses the ` null ` value of its final field
33
45
as a marker of an empty value.
34
46
0 commit comments