@@ -16,19 +16,31 @@ is desirable, as it increases the coverage, but it has a downside. It is possibl
1616most of generated branches would be ` NPE ` branches, while useful paths could be lost due to timeout.
1717
1818Beyond 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.
2022it is also often true for non-public fields from standard library and third-party libraries (even setters often do not
2123allow ` null ` values). Automatically generated tests assign ` null ` values to fields using reflection,
2224but these tests may be uninformative as the corresponding ` NPE ` branches would never occur
2325in the real code that limits itself to the public API.
2426
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+
2532## The solution
2633
2734To 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
3041
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
3244some special cases like ` Optional<T> ` class, which uses the ` null ` value of its final field
3345as a marker of an empty value.
3446
0 commit comments