简体   繁体   中英

verification for unit tests failed

I am trying to test a class for a valet alert service but I am having a problem in verification of the test and getting this error whenever I try to verify my method

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 test:

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());
    }

I get an error that OnSpeedAlertActivationJobTest Failed

The expected behaviour should verify the code


 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());
     }
}

But the unit testing is still failing and not verifying the code

You need to Spy on your SUT in this case. Otherwise, Mockito will not track/verify anything:

@Spy
private ValetAlert mValetAlert;

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

The MockitoAnnotations.initMocks(this); is needed only if you do not use @RunWith(MockitoJunitRunner.class) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM