Issue Overview:

In the current implementation of the validateAutowiredConfig method, there is an issue with the string comparison when checking for violations. This issue can lead to unexpected behavior and potential exceptions during runtime.

Issue Description: The validateAutowiredConfig method is responsible for validating the presence of certain annotations on test methods and test lifecycle methods within a test class. It ensures that no methods are annotated with @Autowired and instead suggests using annotations like @Qualifier or @Value on individual method parameters.

In the original code, the validation of violations relies on comparing the errorMessage string with NO_VIOLATIONS_DETECTED. However, this comparison is done using the != operator, which might not work as intended for string comparison. This is because the != operator checks for reference equality (whether both variables point to the same memory location), not the content of the strings. Consequently, if the errorMessage and NO_VIOLATIONS_DETECTED strings have different memory locations (which is very likely in this case), the condition will not be satisfied even if the errorMessage string has the same content as NO_VIOLATIONS_DETECTED.

Similarly, in the validateRecordApplicationEventsConfig method, there is a similar issue with the string comparison of the errorMessage variable. The original code used the != operator, and I have corrected it by using the equals method.

These fixes ensure that the methods correctly handle the error message comparison and throw an IllegalStateException only when necessary.

if (errorMessage != NO_VIOLATIONS_DETECTED) {
    throw new IllegalStateException(errorMessage);
}

Affects: 6.0.x

Fix: The proposed fix involves changing the string comparison from != to .equals() to compare the content of the two strings properly.

if (!errorMessage.equals(NO_VIOLATIONS_DETECTED)) {
    throw new IllegalStateException(errorMessage);
}

Comment From: sbrannen

  • duplicate of #30456

Comment From: sbrannen

See commit 7aae97bcd469105ac2f2aa76ce93f38df455e3b9.