Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f8d84683 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Ravenwood now uses Mockito 5" into main

parents 4bbf2f26 ba81b56f
Loading
Loading
Loading
Loading
+11 −14
Original line number Diff line number Diff line
@@ -348,26 +348,23 @@ java_library {
    visibility: ["//visibility:private"],
}

java_library {
// On Ravenwood, we now use Mockito 5 throughout.
// Mockito 5 doesn't have an "inline-*" module, which only contained a "marker" file
// which would signal Mockito 4 used inline-mockito.
// Mockito 5 uses the "inline" storategy by default, so we no longer need an "inline"
// module.
// However, because we can't delete an existing jar in the runtime directory
// for b/418236847, we still keep "inline-mockito-ravenwood-prebuilt" as an empty jar.
java_import {
    name: "mockito-ravenwood-prebuilt",
    installable: false,
    static_libs: [
        "mockito-robolectric-prebuilt",
    ],
    // TODO(b/412720779) Change this and all dependencies' sdk_version to "core_current",
    // after making sure none of them use Android APIs.
    sdk_version: "current",
    jars: [":mockito5-hostside-prebuilt"],
    sdk_version: "core_current",
}

java_library {
    name: "inline-mockito-ravenwood-prebuilt",
    installable: false,
    static_libs: [
        "inline-mockito-robolectric-prebuilt",
    ],
    // TODO(b/412720779) Change this and all dependencies' sdk_version to "core_current",
    // after making sure none of them use Android APIs.
    sdk_version: "current",
    sdk_version: "core_current",
}

// We define our own version of platform_compat_config's here, because:
+71 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.ravenwoodtest.mockito;

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;

import org.junit.Test;

/**
 * Mockito 5 has changed the way {@link ArgumentMatcher} matches varargs. (Release notes:
 * https://github.com/mockito/mockito/releases/tag/v5.0.0)
 *
 * Because we still use Mockito 2/3 on the device side but Mockito 5 on Ravenwood,
 * this could cause troubles. However, fortunately, because it's just a matcher, we can basically
 * just set up two matchers to work it around. This class is a bivalent test for the workaround.
 */
public class RavenwoodMockitoVarargTest {
    interface VarargTester {
        int foo(String... args);
    }

    private static VarargTester newVarargTesterForMultipleMockitoVersions() {
        var mock = mock(VarargTester.class);

        // This would match on Mockito 5 and above
        doReturn(1).when(mock).foo(any(String[].class));

        // This would match on Mockito 4 and below.
        // On Mockito 5 too, it'd match for the null argument case.
        doReturn(1).when(mock).foo(any());


        return mock;
    }

    @Test
    public void testMultipleMockitoVersions_varArg0() {
        assertEquals(1, newVarargTesterForMultipleMockitoVersions().foo());
    }

    @Test
    public void testMultipleMockitoVersions_varArg1() {
        assertEquals(1, newVarargTesterForMultipleMockitoVersions().foo("a"));
    }

    @Test
    public void testMultipleMockitoVersions_varArg2() {
        assertEquals(1, newVarargTesterForMultipleMockitoVersions().foo("a", "b"));
    }

    @Test
    public void testMultipleMockitoVersions_varArgNull() {
        assertEquals(1, newVarargTesterForMultipleMockitoVersions().foo((String[]) null));
    }
}