Reputation: 901
I cannot seem to figure this out. I get java.lang.IncompatibleClassChangeError: Implementing class
I'm trying to figure out how to send a pending intent, by pushing it into a class for organization. Currently I have the following:
@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);
}
}
After all of this, I cannot figure how to make them all play nicely. My goal is to verify what is called within getBroadcast
and equally the Intent
itself.
What's the approach I should take?
Thanks, Kelly
Update: Talking below it was suggested to avoid powermock, so I tried the following:
@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)
}
Upvotes: 1
Views: 1496
Reputation: 20130
I would not use PowerMock
here.
And just use:
ShadowApplication shadowApp = shadowOf(RuntimeEnvironment.application);
List<Intent> intents = shadowApp.getBroadcastIntents();
And then find your intent there.
UPD. I'm sorry, I didn't notice that you mocked AlarmManager
. You should not do it and use:
AlarmManager alarmManager = (AlarmManager)RuntimeEnvironment.application.getSystemService(Context.ALARM_SERVICE);
ShadowAlarmManager shadowAlarmManager = shadowOf(alarmManager);
List<ShadowAlarmManager.ScheduledAlarm> alarms = shadowAlarmManager.getScheduledAlarms();
Upvotes: 2