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

Commit a60c144b authored by Makoto Onuki's avatar Makoto Onuki Committed by Android (Google) Code Review
Browse files

Merge "Add tests for static mocking" into main

parents d7e5c037 eeaf655e
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -13,6 +13,9 @@ android_ravenwood_test {
    srcs: [
        "test/**/*.java",
    ],
    exclude_srcs: [
        "test/**/*DeviceOnly*.java",
    ],
    static_libs: [
        "junit",
        "truth",
@@ -31,6 +34,9 @@ android_test {
    srcs: [
        "test/**/*.java",
    ],
    exclude_srcs: [
        "test/**/*RavenwoodOnly*.java",
    ],
    static_libs: [
        "junit",
        "truth",
+46 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.ravenwood.mockito;

import static com.google.common.truth.Truth.assertThat;

import android.app.ActivityManager;
import android.platform.test.ravenwood.RavenwoodRule;

import com.android.dx.mockito.inline.extended.ExtendedMockito;

import org.junit.Rule;
import org.junit.Test;
import org.mockito.quality.Strictness;

public class RavenwoodMockitoDeviceOnlyTest {
    @Rule public final RavenwoodRule mRavenwood = new RavenwoodRule();

    @Test
    public void testStaticMockOnDevice() {
        var mockingSession = ExtendedMockito.mockitoSession()
                .strictness(Strictness.LENIENT)
                .mockStatic(ActivityManager.class)
                .startMocking();
        try {
            ExtendedMockito.doReturn(true).when(ActivityManager::isUserAMonkey);

            assertThat(ActivityManager.isUserAMonkey()).isEqualTo(true);
        } finally {
            mockingSession.finishMocking();
        }
    }
}
+38 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.ravenwood.mockito;

import static com.google.common.truth.Truth.assertThat;

import android.app.ActivityManager;
import android.platform.test.ravenwood.RavenwoodRule;

import org.junit.Rule;
import org.junit.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

public class RavenwoodMockitoRavenwoodOnlyTest {
    @Rule public final RavenwoodRule mRavenwood = new RavenwoodRule();

    @Test
    public void testStaticMockOnRavenwood() {
        try (MockedStatic<ActivityManager> am = Mockito.mockStatic(ActivityManager.class)) {
            am.when(ActivityManager::isUserAMonkey).thenReturn(true);
            assertThat(ActivityManager.isUserAMonkey()).isEqualTo(true);
        }
    }
}
+0 −22
Original line number Diff line number Diff line
@@ -31,28 +31,6 @@ import org.junit.Test;
public class RavenwoodMockitoTest {
    @Rule public final RavenwoodRule mRavenwood = new RavenwoodRule();


// Use this to mock static methods, which isn't supported by mockito 2.
// Mockito supports static mocking since 3.4.0:
// See: https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#48

//    private MockitoSession mMockingSession;
//
//    @Before
//    public void setUp() {
//        mMockingSession = mockitoSession()
//                .strictness(Strictness.LENIENT)
//                .mockStatic(RavenwoodMockitoTest.class)
//                .startMocking();
//    }
//
//    @After
//    public void tearDown() {
//        if (mMockingSession != null) {
//            mMockingSession.finishMocking();
//        }
//    }

    @Test
    public void testMockJdkClass() {
        Process object = mock(Process.class);