Details
-
Improvement
-
Status: Closed
-
Minor
-
Resolution: Not A Problem
-
None
-
None
-
None
Description
Description
I noticed that there is a test class AtomicSafeInitializerTestImpl extends production class AtomicSafeInitializer to assist testing method AtomicSafeInitializer.get(). This might not be the best priactice in unit testing and can be improved by leveraging mocking frameworks.
Current Implementation
- AtomicSafeInitializerTestImpl extends AtomicSafeInitializer and creates a new variable to keep tracking of the method invocation status for initialize().
- In test cases, after executing AtomicSafeInitializer.get(), the new variable will be used in assertion statement to check the execution status of initialize().
Proposed Implementation
- Replace AtomicSafeInitializerTestImpl with a mocking object created by Mockito.
- Extract the AtomicLong attribute and use the extracted attribute in test case to check method invocation status.
- Use method stub to control the behavior of the mocking object.
Motivation
- Decouple test class AtomicSafeInitializerTestImpl from production interface AtomicSafeInitializer.
- Make test logic more clear by using method stub instead of method overriding.