建成OS 2020-06-17 11:44 采纳率: 75%
浏览 435
已采纳

Android 单元测试内部类mock

报错:

java.lang.IncompatibleClassChangeError: android.media.SoundPool and android.media.SoundPool$Builder disagree on InnerClasses attribute

错误定在这一行

mSoundPoolBuilder = PowerMockito.mock(SoundPool.Builder.class);

被测目标代码

public static void playAlarmSound(Context context, int event, int loop) {
    if (abs == null) {
        abs = new AudioAttributes.Builder()
            .setUsage(AudioAttributes.USAGE_MEDIA)
            .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
            .build();
    }
    if (mSoundPool == null) {
        mSoundPool = new SoundPool.Builder()
                    .setMaxStreams(1)
                    .setAudioAttributes(abs)
                    .build();
    }

    ........
}

单元测试代码:

@RunWith(RobolectricTestRunner.class)
@Config(manifest=“../../app/src/main/AndroidManifest.xml”)
public class WarningManagerTest {
    private AudioAttributes.Builder absBuilder;
    private AudioAttributes abs;
    private SoundPool.Builder mSoundPoolBuilder;
    private SoundPool mSoundPool;

    @Test
    public void testPlayAlarmSound(){
        absBuilder = PowerMockito.mock(AudioAttributes.Builder.class);
        abs = PowerMockito.mock(AudioAttributes.class);

        mSoundPoolBuilder = PowerMockito.mock(SoundPool.Builder.class);
        mSoundPool = PowerMockito.mock(SoundPool.class);

        //AudioAttributes
        PowerMockito.whenNew(AudioAttributes.Builder.class).withAnyArguments().thenReturn(absBuilder);
        PowerMockito.when(absBuilder.setUsage(anyInt())).thenCallRealMethod();
        PowerMockito.when(absBuilder.setContentType(anyInt())).thenCallRealMethod();
        PowerMockito.when(absBuilder.setUsage(anyInt())).thenReturn(absBuilder);
        PowerMockito.when(absBuilder.setContentType(anyInt())).thenReturn(absBuilder);
        PowerMockito.when(absBuilder.build()).thenReturn(abs);

        //SoundPool
        PowerMockito.whenNew(SoundPool.Builder.class).withAnyArguments().thenReturn(mSoundPoolBuilder);
        PowerMockito.when(mSoundPoolBuilder.setMaxStreams(anyInt())).thenCallRealMethod();
        PowerMockito.when(mSoundPoolBuilder.setAudioAttributes(any(AudioAttributes.class))).thenCallRealMethod();
        PowerMockito.when(mSoundPoolBuilder.setMaxStreams(anyInt())).thenReturn(mSoundPoolBuilder);
        PowerMockito.when(mSoundPoolBuilder.setAudioAttributes(abs)).thenReturn(mSoundPoolBuilder);
        PowerMockito.when(mSoundPoolBuilder.build()).thenReturn(mSoundPool);

        WarningManager.playAlarmSound(context, 0, 0);

    }
}

请帮忙看看什么原因。

  • 写回答

1条回答 默认 最新

  • dabocaiqq 2020-06-17 11:48
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python使用pulp线性优化时报错
  • ¥15 开源或低价数据中台哪个最好
  • ¥15 arduino编程出现字符串疑似覆盖现象
  • ¥15 我的b站在没有碰到屏幕的情况下偶尔会自动跳出进度条,就像在屏幕上点了一下一样,但我并没有点。而且视频进度并没有变。这可能是什么原因造成的?
  • ¥30 STK matlab python仿真
  • ¥15 关于IMageEnView 图标定位问题
  • ¥20 求解答(matlab)
  • ¥30 ffmpeg库使用过程中遇到的问题
  • ¥15 pyqt5 中python如何通过Qtwebchannel主动发消息给web前端
  • ¥15 关于HTML中title获取xml内容的问题