@Test
public void testInsertJobOrFetchDuplicateAlreadyExistsException() throws IOException {

  IOException fakeConflictException = new IOException("fake 409 conflict");
  when(mockBigqueryJobsInsert.execute()).thenThrow(fakeConflictException);
  when(mockErrorExtractor.itemAlreadyExists(any(IOException.class))).thenReturn(true);
  when(mockBigqueryJobsGet.execute()).thenReturn(jobHandle);
  assertThat(helper.insertJobOrFetchDuplicate(jobProjectId, jobHandle)).isEqualTo(jobHandle);
  verify(mockBigquery, times(2)).jobs();
  ArgumentCaptor<Job> jobCaptor = ArgumentCaptor.forClass(Job.class);
  verify(mockBigqueryJobs, times(1)).insert(eq(jobProjectId), jobCaptor.capture());
  Job job = jobCaptor.getValue();
  assertThat(job).isEqualTo(jobHandle);
  verify(mockBigqueryJobsInsert, times(1)).execute();
  verify(mockBigqueryJobs, times(1)).get(eq(jobProjectId), eq(jobId));
  verify(mockBigqueryJobsGet, times(1)).setLocation(eq(null));
  verify(mockBigqueryJobsGet, times(1)).execute();
  verify(mockErrorExtractor).itemAlreadyExists(eq(fakeConflictException));
}