gpt4 book ai didi

java - 单元测试验证失败

转载 作者:太空宇宙 更新时间:2023-11-04 09:42:46 24 4
gpt4 key购买 nike

我正在尝试为代客警报服务测试一个类,但是在验证测试时遇到问题,并且每当尝试验证我的方法时遇到此错误

public void onSpeedAlertActivationJob(ActivateSpeedAlert job) {
List definitions = job.getActivateSpeedAlertType().getSpeedAlertDefinition();
if (!JobVerifier.containsValetAlert(definitions)) {
return;

}

if (definitions.size() > 1) {
Log.w(LOG_TAG, "Received ValetAlert AND other definitions in one job");


sendSpeedAlertJobAcknowledgement(monitorings.speedalert.ErrorCodes.OTHER_ERROR, job.getId());
return;
}


考试:

This is the uni tests for the method
public void onSpeedAlertActivationJobTest() throws Exception{



/*Execution*/



ActivateSpeedAlert speedalert=new ActivateSpeedAlert();
speedalert.setId(80);
ActivateSpeedAlertType speedalertType=new ActivateSpeedAlertType();
speedalertType.setSpeedAlertDefinition(new ArrayList();
speedalert.setActivateSpeedAlertType(speedalertType);
mValetAlertMock.onSpeedAlertActivationJob(speedalert);

/*Verification //Error in this part
verify(mValetAlertMock).sendSpeedAlertJobAcknowledgement(
monitorings.speedalert.ErrorCodes.OTHER_ERROR, job.getId());
}


我收到一个 OnSpeedAlertActivationJobTest失败的错误

预期的行为应验证代码



 public class ValetAlert extends Service {
public static final String LOG_TAG = "ValetAlert";
public final static String PREFS_TAG = "SpeedAlert"; // ValetAlert has no own dataset
public final static OnlineServiceId ONLINE_SERVICE_ID = OnlineServiceId.VALET_ALERT;

private static final int VALET_ALERT_ID = 0;
private static final String PREF_ENABLED = "ValetAlertEnabled";
private static final String PREF_DEBUG = "ValetAlertDebug";
private static final String PREF_PRIVACY_MODE = "ValetAlertPrivacyMode";
public final static String PREFS_LOCATION_MANAGER = "LocationManager";
public final static String PREF_LOCATION_TRACING = "locationTracingAllowed";

private Messenger mMessenger;
private Subscription mSubscription;
private AlarmManager mAlarmManager;
private ConfigurationManager mConfigurationManager;
private MonitoringStateReporter mMonitoringStateReporter;
private SharedPreferences mPreferences;
private SpeedAlertPreferences mSpeedAlertPreferences;
private GeoFencingPreferences mGeoFencingPreferences;

private GeoFenceObserver mGeoFenceObserver;
private SpeedAlertObserver mSpeedAlertObserver;
private boolean mActive;
private boolean mSpeedAlertActive;
private boolean mGeoFenceActive;
private boolean mLocationTracing;
private boolean mDebug;

public void onCreate() {
mPreferences = getSharedPreferences(PREFS_TAG, 0);
mSpeedAlertPreferences = new SpeedAlertPreferences(getSharedPreferences(SpeedAlert.PREFS_TAG, 0));
mGeoFencingPreferences = new GeoFencingPreferences(getSharedPreferences(GeoFencing.PREFS_TAG, 0));
if (!isEnabled()) {
Log.d(LOG_TAG, "Disabled by configuration");
return;
}

mLocationTracing = getSharedPreferences(PREFS_LOCATION_MANAGER, 0).getBoolean(PREF_LOCATION_TRACING, true);
mConfigurationManager = CoreServices.getConfigurationManager(this);
mAlarmManager = new AlarmManager(Clock.getRealtimeClock());
mAlarmManager.onCreate();

mConfigurationManager.addSubscriptionListener(ONLINE_SERVICE_ID, mSubscriptionListener);
mSubscription = mConfigurationManager.getSubscription(ONLINE_SERVICE_ID);

mMessenger = CoreServices.getMessenger(this);
mMonitoringStateReporter = new MonitoringStateReporter(mMessenger, PubSubId.Monitorings.VALET_ALERT);
mMonitoringStateReporter.onCreate();
mDebug = mPreferences.getBoolean(PREF_DEBUG, false);

if (mSubscription.isEnabled()) {
addListeners();
restoreDefinitionsFromPreferences();
Log.d(LOG_TAG, "Subscribed");
} else {
Log.d(LOG_TAG, "Unsubscribed");
}
}

public void onDestroy() {
if (!isEnabled()) {
return;
}
deactivate();
mMonitoringStateReporter.onDestroy();
mAlarmManager.onDestroy();
removeListeners();
}

public IBinder onBind(Intent intent) {
return null;
}

private SubscriptionListener mSubscriptionListener = new SubscriptionListener() {
public void onSubscriptionChanged(OnlineServiceId onlineService, Subscription subscription) {
if (onlineService.equals(ONLINE_SERVICE_ID)) {
if (!mSubscription.isEnabled() && subscription.isEnabled()) {
Log.d(LOG_TAG, "Updating subscription: subscribed");
addListeners();
restoreDefinitionsFromPreferences();
} else if (mSubscription.isEnabled() && !subscription.isEnabled()) {
Log.d(LOG_TAG, "Updating subscription: unsubscribed");
removeListeners();
deactivate();
}
mSubscription = subscription;
}
}
};

private MessageListener mCloudMessageListener = new MessageListener() {
public void onMessage(Channel channel, PubSubId id, Object message, Bundle extras) {
if (!mSubscription.isLicenseValid() || !mSubscription.isEnabled()) {
Log.w(LOG_TAG, "No valid subscription, ignoring backend job");
return;
}

if (id.equals(PubSubId.SpeedAlert.ACTIVATION)) {
ActivateSpeedAlert activationMessage = (ActivateSpeedAlert) message;
onSpeedAlertActivationJob(activationMessage);
} else if (id.equals(PubSubId.SpeedAlert.DEACTIVATION)) {
DeactivateSpeedAlert deactivationMessage = (DeactivateSpeedAlert) message;
onSpeedAlertDeactivationJob(deactivationMessage);
} else if (id.equals(PubSubId.Geofencing.ACTIVATION)) {
ActivateGeoFencing activationMessage = (ActivateGeoFencing) message;
onGeoFencingActivationJob(activationMessage);
} else if (id.equals(PubSubId.Geofencing.DEACTIVATION)) {
DeactivateGeoFencing deactivationMessage = (DeactivateGeoFencing) message;
onGeoFencingDeactivationJob(deactivationMessage);
}
}
};

private void addListeners() {
mMessenger.subscribe(Channel.CLOUD_CHANNEL, PubSubId.Geofencing.ACTIVATION, mCloudMessageListener);
mMessenger.subscribe(Channel.CLOUD_CHANNEL, PubSubId.Geofencing.DEACTIVATION, mCloudMessageListener);
mMessenger.subscribe(Channel.CLOUD_CHANNEL, PubSubId.SpeedAlert.ACTIVATION, mCloudMessageListener);
mMessenger.subscribe(Channel.CLOUD_CHANNEL, PubSubId.SpeedAlert.DEACTIVATION, mCloudMessageListener);
}

private void removeListeners() {
mMessenger.unsubscribe(Channel.CLOUD_CHANNEL, PubSubId.Geofencing.ACTIVATION, mCloudMessageListener);
mMessenger.unsubscribe(Channel.CLOUD_CHANNEL, PubSubId.Geofencing.DEACTIVATION, mCloudMessageListener);
mMessenger.unsubscribe(Channel.CLOUD_CHANNEL, PubSubId.SpeedAlert.ACTIVATION, mCloudMessageListener);
mMessenger.unsubscribe(Channel.CLOUD_CHANNEL, PubSubId.SpeedAlert.DEACTIVATION, mCloudMessageListener);
}

public boolean isEnabled() {
return mPreferences.getBoolean(PREF_ENABLED, true);
}

public boolean isDebug() {
return mPreferences.getBoolean(PREF_DEBUG, false);
}

public void onSpeedAlertActivationJob(ActivateSpeedAlert job) {
List definitions = job.getActivateSpeedAlertType().getSpeedAlertDefinition();
if (!JobVerifier.containsValetAlert(definitions)) {
return;
}
if (definitions.size() > 1) {
Log.w(LOG_TAG, "Received ValetAlert AND other definitions in one job");
sendSpeedAlertJobAcknowledgement(monitorings.speedalert.ErrorCodes.OTHER_ERROR, job.getId());
return;
}

The unit testing for the service is

public class ValetAlertTest {
// <editor-fold defaultstate="collapsed" desc="Test Data">
public static final String LOG_TAG="ValetAlert";
public final static String CONFIG_LOG_TAG="SpeedAlert";
public final static OnlineServiceId ONLINE_SERVICE_ID=OnlineServiceId.VALET_ALERT;

private static final int VALET_ALERT_ID=0;
private static final String PREF_ENABLED="ValetAlertEnabled";
private static final String PREF_DEBUG="ValetAlertDebug";
private static final String PREF_PRIVACY_MODE="ValetAlertPrivacyMode";
public final static String PREFS_LOCATION_MANAGER="LocationManager";
public final static String PREF_LOCATION_TRACING="locationTracingAllowed";

private ValetAlert mValetAlert;

@AfterClass
public static void tearDownClass() {
}

@Before
public void setUp() {
mValetAlert = new ValetAlert();
}

@After
public void tearDown() {
}

// TODO add test methods here.
// The methods must be annotated with annotation @Test. For example:
//
@Test
public void onSpeedAlertActivationJobTest() {
ActivateSpeedAlert jobTest = new ActivateSpeedAlert();
jobTest.setId(123);
ActivateSpeedAlertType actType = new ActivateSpeedAlertType();
actType.setSpeedAlertDefinition(new ArrayList());

jobTest.setActivateSpeedAlertType(actType);


mValetAlert.onSpeedAlertActivationJob(jobTest);

verify(mValetAlert).sendSpeedAlertJobAcknowledgement(monitorings.speedalert.ErrorCodes.OTHER_ERROR, jobTest.getId());
}
}


但是单元测试仍然失败并且没有验证代码

最佳答案

在这种情况下,您需要在SUT上输入Spy。否则,Mockito将不会跟踪/验证任何内容:

@Spy
private ValetAlert mValetAlert;

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}


仅当不使用 MockitoAnnotations.initMocks(this);时才需要 @RunWith(MockitoJunitRunner.class)

关于java - 单元测试验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55775341/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com