Skip to content

Commit 84a45d8

Browse files
committed
Update documentation
1 parent 537eda1 commit 84a45d8

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

docs/SpeculativeFieldNonNullability.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,31 @@ is desirable, as it increases the coverage, but it has a downside. It is possibl
1616
most of generated branches would be `NPE` branches, while useful paths could be lost due to timeout.
1717

1818
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.
2022
it is also often true for non-public fields from standard library and third-party libraries (even setters often do not
2123
allow `null` values). Automatically generated tests assign `null` values to fields using reflection,
2224
but these tests may be uninformative as the corresponding `NPE` branches would never occur
2325
in 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

2734
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
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
3244
some special cases like `Optional<T>` class, which uses the `null` value of its final field
3345
as a marker of an empty value.
3446

0 commit comments

Comments
 (0)