RuleDatabaseItemUpdateRunnable implements Runnable { @NonNull HttpURLConnection getHttpURLConnection(File file, SingleWriterMultipleReaderFile singleWriterMultipleReaderFile, URL url) throws IOException { HttpURLConnection connection = internalOpenHttpConnection(url); connection.setInstanceFollowRedirects(true); connection.setConnectTimeout(CONNECT_TIMEOUT_MILLIS); connection.setReadTimeout(READ_TIMEOUT_MILLIS); try { singleWriterMultipleReaderFile.openRead().close(); connection.setIfModifiedSince(file.lastModified()); } catch (IOException e) { } connection.connect(); return connection; } RuleDatabaseItemUpdateRunnable(@NonNull RuleDatabaseUpdateTask parentTask, @NonNull Context context, @NonNull Configuration.Item item); @Override void run();  }
@Test @PrepareForTest({Log.class}) public void testGetHttpURLConnection() throws Exception { RuleDatabaseItemUpdateRunnable itemUpdateRunnable = mock(RuleDatabaseItemUpdateRunnable.class); when(itemUpdateRunnable.getHttpURLConnection(any(File.class), any(SingleWriterMultipleReaderFile.class), any(URL.class))).thenCallRealMethod(); when(itemUpdateRunnable.internalOpenHttpConnection(any(URL.class))).thenReturn(connection); when(singleWriterMultipleReaderFile.openRead()).thenReturn(mock(FileInputStream.class)); assertSame(connection, itemUpdateRunnable.getHttpURLConnection(file, singleWriterMultipleReaderFile, url)); CountingAnswer setIfModifiedAnswer = new CountingAnswer(null); when(file.lastModified()).thenReturn(42L); when(connection, "setIfModifiedSince", eq(42L)).then(setIfModifiedAnswer); assertSame(connection, itemUpdateRunnable.getHttpURLConnection(file, singleWriterMultipleReaderFile, url)); assertEquals(1, setIfModifiedAnswer.numCalls); setIfModifiedAnswer.numCalls = 0; when(file.lastModified()).thenReturn(0L); assertSame(connection, itemUpdateRunnable.getHttpURLConnection(file, singleWriterMultipleReaderFile, url)); assertEquals(0, setIfModifiedAnswer.numCalls); }