AssertJ Configuration Built Without Effect
ID |
java.assertj_config_applied |
Severity |
high |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Java |
Tags |
reliability, testing |
Description
Reports a local org.assertj.core.configuration.Configuration, built with new Configuration() and customized through its setters, that is never made effective with apply() or applyAndDisplay().
Rationale
An AssertJ Configuration only changes AssertJ’s behaviour once apply() or applyAndDisplay() runs. Without that call every setter invoked on it is dead code, and assertions in the test keep running under AssertJ’s default configuration instead of the one that was just built.
void checksOrderTotals() {
Configuration configuration = new Configuration();
configuration.setMaxLengthForSingleLineDescription(120);
configuration.setPrintAssertionsDescriptionEnabled(true);
// never applied: the settings above have no effect
}
Remediation
Call apply() (or applyAndDisplay() to also print the resulting configuration) once the configuration is fully built.
void checksOrderTotals() {
Configuration configuration = new Configuration();
configuration.setMaxLengthForSingleLineDescription(120);
configuration.setPrintAssertionsDescriptionEnabled(true);
configuration.apply();
}
What is not reported
Only a variable initialized directly with new Configuration() is tracked; one obtained from a factory method is out of scope, since there is no way to tell whether that method already applied it. Tracking also stops as soon as the variable is passed as an argument to another call: the configuration may escape into code this rule cannot see and be applied there, so nothing is reported either way.