我正在尝试为其中一个静态方法编写单元测试用例。
protected static ScheduleCheck createScheduleCheck(Bundle extra){
long assetId = extra.getLong(ScheduleCheckRecommendationService.EXTRA_ASSET_ID);
int hour = extra.getInt(ScheduleCheckRecommendationService.EXTRA_SCHEDULE_CHECK_HOUR);
int minute = extra.getInt(ScheduleCheckRecommendationService.EXTRA_SCHEDULE_CHECK_MINUTE);
int ampm = extra.getInt(ScheduleCheckRecommendationService.EXTRA_SCHEDULE_CHECK_AMPM);
long landmarkId = extra.getLong(ScheduleCheckRecommendationService.EXTRA_LANDMARK_ID);
final ArrayList<Integer> weekday =
extra.getIntegerArrayList(ScheduleCheckRecommendationService.EXTRA_WEEK_DAY_LIST);
final Set<Weekday> weekdaySet = convertWeedays(weekday);
// Adjust hour.
hour = hour + 12 * ampm;
ScheduleCheck scheduleCheck = new ScheduleCheck();
scheduleCheck.setAssetId(assetId);
scheduleCheck.setTimeToFireInDisplayTimezone((int) FinderUtils.getTimeInSeconds(hour, minute));
scheduleCheck.setLandmarkId(landmarkId);
scheduleCheck.setWeekdaysToFireInDisplayTimezone(weekdaySet);
return scheduleCheck;
}
单元测试案例:
public class ScheduleCheckRecommendationReceiverTest {
private static final long ASSET_ID = 1L;
private static final int HOUR = 10;
private static final int MINUTE = 10;
private static final int AM = 0;
private static final int PM = 1;
private static final long LANDMARK_ID = 10L;
private static final ArrayList<Integer> calendarWeekday = new ArrayList(Arrays.asList(1, 2, 3, 4, 5, 6, 7));
private static final Set<Weekday> weekdaySet = new HashSet<>(Arrays.asList(Weekday.SUNDAY,
Weekday.MONDAY,
Weekday.TUESDAY,
Weekday.WEDNESDAY,
Weekday.THURSDAY,
Weekday.FRIDAY,
Weekday.SATURDAY));
private Bundle extra;
private ScheduleCheck scheduleCheck;
@Before
public void setup(){
extra = new Bundle();
extra.putLong(ScheduleCheckRecommendationService.EXTRA_ASSET_ID, ASSET_ID);
extra.putInt(ScheduleCheckRecommendationService.EXTRA_SCHEDULE_CHECK_HOUR, HOUR);
extra.putInt(ScheduleCheckRecommendationService.EXTRA_SCHEDULE_CHECK_MINUTE, MINUTE);
extra.putInt(ScheduleCheckRecommendationService.EXTRA_SCHEDULE_CHECK_AMPM, PM);
extra.putLong(ScheduleCheckRecommendationService.EXTRA_LANDMARK_ID, LANDMARK_ID);
extra.putIntegerArrayList(ScheduleCheckRecommendationService.EXTRA_WEEK_DAY_LIST, calendarWeekday);
// Schedule check
scheduleCheck = new ScheduleCheck();
scheduleCheck.setAssetId(ASSET_ID);
scheduleCheck.setTimeToFireInDisplayTimezone((int) FinderUtils.getTimeInSeconds(HOUR, MINUTE));
scheduleCheck.setLandmarkId(LANDMARK_ID);
scheduleCheck.setWeekdaysToFireInDisplayTimezone(weekdaySet);
}
@After
public void tearDown(){
}
@Test
public void testCreateScheduleCheckOk(){
ScheduleCheck actualScheduleCheck = ScheduleCheckRecommendationReceiver.createScheduleCheck(extra);
// verification steps ...
...
...
}
}
但是当我尝试使用gradle命令./gradlew test
运行测试用例时,我收到以下错误。
com.locationlabs.finder.android.core.services.ScheduleCheckRecommendationReceiverTest > testCreateScheduleCheckOk FAILED
java.lang.RuntimeException: Method putLong in android.os.BaseBundle not mocked. See http://g.co/androidstudio/not-mocked for details.
at android.os.BaseBundle.putLong(BaseBundle.java)
at com.locationlabs.finder.android.core.services.ScheduleCheckRecommendationReceiverTest.setup(ScheduleCheckRecommendationReceiverTest.java:39)
1 test completed, 1 failed
我尝试访问错误消息中提供的链接(http://g.co/androidstudio/not-mocked)并包含建议的解决方案,但它根本不起作用,我得到了完全不同的错误。
com.locationlabs.finder.android.core.services.ScheduleCheckRecommendationReceiverTest > testCreateScheduleCheckOk FAILED
java.lang.UnsatisfiedLinkError: android.os.SystemClock.elapsedRealtime()J
at android.os.SystemClock.elapsedRealtime(Native Method)
at com.locationlabs.util.debug.FifoLog.log(FifoLog.java:97)
at com.locationlabs.util.debug.Log.delegatePrintln(Log.java:238)
at com.locationlabs.util.debug.Log.doLog(Log.java:198)
at com.locationlabs.util.debug.Log.i(Log.java:268)
at com.locationlabs.finder.android.core.util.WMCache.getInstance(WMCache.java:52)
at com.locationlabs.finder.android.core.model.persister.AbstractPersister.load(AbstractPersister.java:31)
at com.locationlabs.finder.android.core.manager.AccountDataManager.cachedAccountData(AccountDataManager.java:32)
at com.locationlabs.finder.android.core.util.DateUtil.getFinderTimezone(DateUtil.java:52)
at com.locationlabs.finder.android.core.util.DateUtil.getFinderOffset(DateUtil.java:72)
at com.locationlabs.finder.android.core.model.ScheduleCheck.setTimeToFireInDisplayTimezone(ScheduleCheck.java:295)
at com.locationlabs.finder.android.core.services.ScheduleCheckRecommendationReceiverTest.setup(ScheduleCheckRecommendationReceiverTest.java:49)
我不确定如何解决此问题以及我收到此错误的原因。