Repository: apache/dubbo
File: dubbo-registry/dubbo-registry-multiple/src/test/java/org/apache/dubbo/registry/multiple/MultipleRegistry2S2RTest.java (Java)

Code before:
        for (Registry registry : registries) {
            if (registry instanceof ZookeeperRegistry) {
                return (ZookeeperRegistry) registry;
            }
        }
        return null;
    }

    private static void setFieldValue(Object target, String fieldName, Object value) {
        try {
            Field field = target.getClass().getDeclaredField(fieldName);
            field.setAccessible(true);
            field.set(target, value);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            throw new RuntimeException("Failed to set field: " + fieldName, e);
        }
    }

    @BeforeEach
    void init() {
        MockitoAnnotations.openMocks(this);

        zookeeperConnectionAddress1 = MOCK_ZK_ADDR1;
        zookeeperConnectionAddress2 = MOCK_ZK_ADDR2;

        multipleRegistry = mock(MultipleRegistry.class);

        List<String> origRefUrls = new ArrayList<>();
        origRefUrls.add(MOCK_ZK_ADDR1);
        origRefUrls.add(MOCK_ZK_ADDR2);
        setFieldValue(multipleRegistry, "origReferenceRegistryURLs", origRefUrls);

        List<String> origServiceUrls = new ArrayList<>();
        origServiceUrls.add(MOCK_ZK_ADDR1);
        origServiceUrls.add(MOCK_ZK_ADDR2);
        setFieldValue(multipleRegistry, "origServiceRegistryURLs", origServiceUrls);

        List<String> effectRefUrls = new ArrayList<>();
        effectRefUrls.add(MOCK_ZK_ADDR1);
        effectRefUrls.add(MOCK_ZK_ADDR2);
        setFieldValue(multipleRegistry, "effectReferenceRegistryURLs", effectRefUrls);

        List<String> effectServiceUrls = new ArrayList<>();
        effectServiceUrls.add(MOCK_ZK_ADDR1);
        effectServiceUrls.add(MOCK_ZK_ADDR2);
        setFieldValue(multipleRegistry, "effectServiceRegistryURLs", effectServiceUrls);

        zookeeperClient = mock(Curator5ZookeeperClient.class);
        zookeeperClient2 = mock(Curator5ZookeeperClient.class);
        when(zookeeperClient.getChildren(any(String.class))).thenReturn(Collections.singletonList("mock-provider"));
        when(zookeeperClient2.getChildren(any(String.class))).thenReturn(Collections.singletonList("mock-provider"));

Review comment (bug):
This approach is incorrect. You should not mock MultipleRegistry—this is your test subject and should not be mocked.

Code after:
        mockZkClient1 = Mockito.mock(ZookeeperClient.class);
        mockZkClient2 = Mockito.mock(ZookeeperClient.class);
        mockZkRegistry1 = Mockito.mock(ZookeeperRegistry.class);
        mockZkRegistry2 = Mockito.mock(ZookeeperRegistry.class);

        try (MockedConstruction<ZookeeperRegistryFactory> zkFactoryConstruction =
                Mockito.mockConstruction(ZookeeperRegistryFactory.class, (mockFactory, context) -> {
                    Mockito.lenient()
                            .when(mockFactory.getRegistry(MOCK_ZK_URL_1))
                            .thenReturn(mockZkRegistry1);
                    Mockito.lenient()
                            .when(mockFactory.getRegistry(MOCK_ZK_URL_2))
                            .thenReturn(mockZkRegistry2);
                })) {
            Mockito.lenient().when(mockZkRegistry1.isAvailable()).thenReturn(true);
            Mockito.lenient().when(mockZkRegistry2.isAvailable()).thenReturn(true);
            Mockito.lenient().when(mockZkRegistry1.getUrl()).thenReturn(MOCK_ZK_URL_1);
            Mockito.lenient().when(mockZkRegistry2.getUrl()).thenReturn(MOCK_ZK_URL_2);

            URL multipleUrl =
                    URL.valueOf("multiple://127.0.0.1?application=vic&enable-empty-protection=false&check=false&"
                            + MultipleRegistry.REGISTRY_FOR_SERVICE + "=" + MOCK_ZK_ADDR_1 + "," + MOCK_ZK_ADDR_2 + "&"
                            + MultipleRegistry.REGISTRY_FOR_REFERENCE + "=" + MOCK_ZK_ADDR_1 + "," + MOCK_ZK_ADDR_2);

            multipleRegistry = (MultipleRegistry) new MultipleRegistryFactory().createRegistry(multipleUrl);

            Map<URL, Registry> serviceRegistries = new HashMap<>();
            serviceRegistries.put(MOCK_ZK_URL_1, mockZkRegistry1);
            serviceRegistries.put(MOCK_ZK_URL_2, mockZkRegistry2);
            setPrivateField(multipleRegistry, "serviceRegistries", serviceRegistries);
            setPrivateField(multipleRegistry, "referenceRegistries", serviceRegistries);
        } catch (Exception e) {
            throw new RuntimeException("Failed to initialize MultipleRegistry", e);
        }
    }

    private void setPrivateField(Object targetObj, String fieldName, Object fieldValue) {
        try {
            Field field = targetObj.getClass().getDeclaredField(fieldName);
            field.setAccessible(true);
            field.set(targetObj, fieldValue);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            throw new RuntimeException("Failed to set private field via Reflection:" + fieldName, e);
        }
    }

    @Test
    void testParamConfig() {
        Assertions.assertEquals(2, multipleRegistry.origReferenceRegistryURLs.size());
        Assertions.assertTrue(multipleRegistry.origReferenceRegistryURLs.contains(MOCK_ZK_ADDR_1));
        Assertions.assertTrue(multipleRegistry.origReferenceRegistryURLs.contains(MOCK_ZK_ADDR_2));