JobServiceConnection implements ServiceConnection { synchronized void unbind() { if (!wasUnbound()) { binder = null; wasUnbound = true; try { context.unbindService(this); } catch (IllegalArgumentException e) { Log.w(TAG, "Error unbinding service: " + e.getMessage()); } List<JobInvocation> jobsToFinish = new ArrayList<>(jobStatuses.size()); Iterator<JobInvocation> pendingJobsIterator = jobStatuses.keySet().iterator(); while (pendingJobsIterator.hasNext()) { jobsToFinish.add(pendingJobsIterator.next()); pendingJobsIterator.remove(); } for (JobInvocation job : jobsToFinish) { requestRetryForJob(job); } } } JobServiceConnection(IJobCallback callback, Context context); @Override synchronized void onServiceConnected(ComponentName name, IBinder service); @Override synchronized void onServiceDisconnected(ComponentName name);  }
@Test public void unbind() throws Exception { connection.onServiceConnected(null, binderMock); binderMock.verifyStartArguments(jobData, noopCallback); assertFalse(connection.wasUnbound()); connection.unbind(); assertTrue(connection.wasUnbound()); verify(contextMock).unbindService(connection); }