Refactor: Replace Whitebox with standard reflection in Analyzer, Server-Cluster-Plugin and Receiver & Fetcher Plugins. by Hyeon-moGu · Pull Request #13718 · apache/skywalking
…er-Cluster-Plugin and Receiver & Fetcher Plugins. **General Refactoring (`agent-analyzer`):** - Systematically eliminated `Whitebox` usages across various analyzer tests. - Migrated internal state manipulations to standard Java Reflection API (`Field.setAccessible()` etc.), ensuring proper checked exception handling to prevent polluting test method signatures. - Replaced all `Whitebox.setInternalState()` and `getInternalState()` usages with standard `java.lang.reflect.Field` to inject internal dependencies (e.g., `client`, `config`, `loadedProvider`, `healthChecker`) into Coordinators and Providers across all 5 cluster plugins (Zookeeper, Nacos, Etcd, Kubernetes, Consul). - **Handled Checked Exceptions Safely:** Since standard Reflection APIs strictly throw checked exceptions (`NoSuchFieldException`, `IllegalAccessException`), explicitly wrapped them in `RuntimeException` within `try-catch` blocks. This successfully removed the PowerMock dependency while preserving the existing test lifecycle method signatures (like `@BeforeEach` or `@Test`) without adding unnecessary `throws` declarations. ** General Refactoring (`server-receiver-plugin`, `server-fetcher-plugin`):** - Systematically removed `Whitebox` dependencies and migrated to standard Java Reflection API. - Centralized reflection logic within `try-catch` blocks to wrap checked exceptions into `RuntimeException`, ensuring no changes were required to existing test method signatures. `ZabbixMetricsTest` & `ZabbixBaseTest`: - Static Mock Lifecycle Management: Replaced manual `Whitebox` field manipulation of the `MetricsStreamProcessor` singleton with `MockedStatic`. - Implemented strict `.close()` logic within the `@AfterEach` lifecycle to prevent memory leaks and resource conflicts, which is particularly critical for stability in newer JVM environments. - Resolved a legacy `NullPointerException` by combining `MockedStatic` with a `Mockito.spy()` of the actual processor instance, ensuring a safe and stateful mock return. `TelegrafMetricsTest`: - Clean Mocking: Eliminated redundant `CoreModule` mocks and unused stubbings, resolving `UnnecessaryStubbingException` and improving test performance - Modernization: Fixed anti-patterns (spying on class literals) and aligned with JUnit 5 best practices. `ClusterManagerMetricsAdapterTest`: - Static State Isolation: Addressed potential flaky test behavior by explicitly clearing shared static maps (e.g., `initialized` flag in `FieldsHelper`) during `@BeforeEach`. - This ensures complete test isolation by preventing state leakage between test cases, leading to a more deterministic and reliable test suite. `CiliumNodeManagerTest` & Others: - Successfully migrated internal state injections(remoteInstances and allNodes) to standard reflection, maintaining the original testing intent while removing the PowerMock requirement.