πŸ“Š JAVA
Q. Using which annotation non visible or private method can be tested?
  • (A) @NonVisible
  • (B) @Visible
  • (C) @VisibleForTesting
  • (D) @NonVisibleForTesting
πŸ’¬ Discuss
βœ… Correct Answer: (C) @VisibleForTesting

Explanation:

@VisibleForTesting is an annotation provided by Google's Guava library.

  • It is used to mark methods, fields, or classes that are more visible than otherwise necessary just to make them accessible for testing.
  • This annotation acts as documentation to indicate that something which is public or protected should not be used in production code, but is made accessible only for unit tests.

Example usage:

@VisibleForTesting
void someInternalMethod() {
// implementation
}

This helps maintain encapsulation while still allowing thorough testing.

Note: This annotation does not magically make private methods accessible to tests; you must change their visibility to package-private or protected and annotate them to indicate the reason.

Explanation by: Mr. Dubey

@VisibleForTesting is an annotation provided by Google's Guava library.

  • It is used to mark methods, fields, or classes that are more visible than otherwise necessary just to make them accessible for testing.
  • This annotation acts as documentation to indicate that something which is public or protected should not be used in production code, but is made accessible only for unit tests.

Example usage:

@VisibleForTesting
void someInternalMethod() {
// implementation
}

This helps maintain encapsulation while still allowing thorough testing.

Note: This annotation does not magically make private methods accessible to tests; you must change their visibility to package-private or protected and annotate them to indicate the reason.

πŸ’¬ Discussion


πŸ“Š Question Analytics

πŸ‘οΈ
331
Total Visits
πŸ“½οΈ
4 y ago
Published
πŸŽ–οΈ
Manisha Agrawal
Publisher
πŸ“ˆ
84%
Success Rate