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

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

Merge "Ravenwood: Support mockito" into main

parents f0b5393e 2488dbee
Loading
Loading
Loading
Loading
+38 −0
Original line number Original line Diff line number Diff line
package {
    // See: http://go/android-license-faq
    // A large-scale-change added 'default_applicable_licenses' to import
    // all of the 'license_kinds' from "frameworks_base_license"
    // to get the below license kinds:
    //   SPDX-license-identifier-Apache-2.0
    default_applicable_licenses: ["frameworks_base_license"],
}

// Ravenwood tests run on the hostside, so we need mockit of the host variant.
// But we need to use it in modules of the android variant, so we "wash" the variant with it.
java_host_for_device {
    name: "mockito_ravenwood",
    libs: [
        "mockito",
        "objenesis",
    ],
}

android_ravenwood_test {
    name: "RavenwoodMockitoTest",

    srcs: [
        "test/**/*.java",
    ],
    static_libs: [
        "junit",
        "truth",

        "mockito_ravenwood",
    ],
    libs: [
        "android.test.mock",
        "android.test.base",
        "android.test.runner",
    ],
    auto_gen_config: true,
}
+67 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2023 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 static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import android.content.Context;
import android.content.Intent;

import org.junit.Test;

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

        when(object.exitValue()).thenReturn(42);

        assertThat(object.exitValue()).isEqualTo(42);
    }

    /* It still doesn't work...
STACKTRACE:
org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: class android.content.Intent.

Mockito can only mock non-private & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.


... But Intent public, non-final.

     */
    // @Test
    private void testMockAndroidClass1() {
        Intent object = mock(Intent.class);

        when(object.getAction()).thenReturn("ACTION_RAVENWOOD");

        assertThat(object.getAction()).isEqualTo("ACTION_RAVENWOOD");
    }

    @Test
    public void testMockAndroidClass2() {
        Context object = mock(Context.class);

        when(object.getPackageName()).thenReturn("android");

        assertThat(object.getPackageName()).isEqualTo("android");
    }
}