在同一测试中模拟挂起意图实例和静态



我似乎想不通。我得到java.lang.IncompatibleClassChangeError: Implementing class

我正在尝试弄清楚如何通过将待处理的意图推送到组织的类中来发送待处理的意图。目前我有以下内容:

@RunWith(PowerMockRunner.class)
@PrepareForTest(PendingIntent.class)
public class TheTests{
    private Context _context;
    private AlarmManager _alarmManager;
    private Manager _manager;
    @Before
    public void setup() {
        _alarmManager = Mockito.mock(AlarmManager.class);
        _context = Mockito.mock(Context.class);
        Mockito.when(_context.getSystemService(anyString())).thenReturn(_alarmManager);
        _manager = new Manager(_context);
    }
    @Test
    public void example() {
        PendingIntent expected = PowerMockito.mock(PendingIntent.class);
        PowerMockito
                .when(PendingIntent.getBroadcast(any(Context.class), anyInt(), any(Intent.class), anyInt()))
                .thenReturn(expected);
        _manager.broadcast(_cal, new Schedule());
    }
}
public Manager class {
    public void broadcast(Thing thing) {
        if (thing == null) {
            return;
        }
        Intent sender = new Intent("broadcast.AlarmReceiverFilter");
        PendingIntent pendingIntent = PendingIntent.getBroadcast(_context, 0, sender, PendingIntent.FLAG_CANCEL_CURRENT);
        _alarm.set(AlarmManager.RTC_WAKEUP, 0, pendingIntent);
    }
}

在所有这些之后,我不知道如何让它们都玩得很好。我的目标是验证getBroadcast内部所谓的内容,以及Intent本身。

我应该采取什么方法?

谢谢凯利

更新:下面有人建议避免 powermock,所以我尝试了以下内容:

@Test
public void example2() {
    ShadowApplication shadowApp = shadowOf(RuntimeEnvironment.application);
    _manager = new TimeManager(shadowApp.getApplicationContext());
    _manager.broadcast(new Thing());
    List<Intent> intents = shadowApp.getBroadcastIntents();
    assertEquals(intents.size(), 1); // actual size is 0 (zero)
}

我不会在这里使用PowerMock

只需使用:

ShadowApplication shadowApp = shadowOf(RuntimeEnvironment.application);
List<Intent> intents = shadowApp.getBroadcastIntents();

然后在那里找到你的意图。

上。对不起,我没有注意到你嘲笑AlarmManager.您不应该这样做并使用:

AlarmManager alarmManager = (AlarmManager)RuntimeEnvironment.application.getSystemService(Context.ALARM_SERVICE);
ShadowAlarmManager shadowAlarmManager = shadowOf(alarmManager);
List<ShadowAlarmManager.ScheduledAlarm> alarms = shadowAlarmManager.getScheduledAlarms();

相关内容

  • 没有找到相关文章

最新更新