Skip to content

ADFA-4312 Mock NetworkUtils in CloneRepositoryViewModelTest#1400

Open
hal-eisen-adfa wants to merge 1 commit into
stagefrom
ADFA-4312-mock-log-clone-repo-test
Open

ADFA-4312 Mock NetworkUtils in CloneRepositoryViewModelTest#1400
hal-eisen-adfa wants to merge 1 commit into
stagefrom
ADFA-4312-mock-log-clone-repo-test

Conversation

@hal-eisen-adfa

@hal-eisen-adfa hal-eisen-adfa commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator

Summary

Fixes ADFA-4312 — four :app testV8DebugUnitTest cases in CloneRepositoryViewModelTest fail under the nightly analyze.yml Jacoco/SonarQube run with RuntimeException: Method e in android.util.Log not mocked.

  • CloneRepositoryViewModel.cloneRepository() calls blankj.utilcode.NetworkUtils.isConnected(), which lazily invokes Utils.init()android.util.Log.e(). The plain JVM test runner has no Log stub, so the call throws before any assertion runs.
  • Fix is purely test-side: mockkStatic(NetworkUtils::class) and stub isConnected() to return true. Matches the semantic intent of these tests (they exercise the clone-attempt path, which presumes connectivity) and avoids depending on blankj-utilcode internals — unlike the mockkStatic(Log::class) approach suggested in the ticket, which only patches the transitive symptom and would still leave NetworkUtils.getActiveNetworkInfo() reaching for an Application context.
  • Existing @After unmockkAll() already covers cleanup; no other test changes.

Heads-up filed on the ticket: GitBottomSheetViewModel.kt lines 180 and 250 also call NetworkUtils.isConnected(). No JVM test exercises those paths today, so analyze.yml is not failing on them — but any future test will need the same treatment.

Test plan

  • :app:testV8DebugUnitTest --tests "com.itsaky.androidide.viewmodel.CloneRepositoryViewModelTest" — 9 tests pass, 0 failures, 0 errors (the 4 previously-failing cases now pass).
  • Re-ran the exact analyze.yml gradle command locally (:testing:tooling:assemble :testing:common:assemble sonarqube --info --no-build-cache -x lint --continue). CloneRepositoryViewModelTest reports tests=9 failures=0 errors=0. Other unrelated failures in that run (CompilerTest, IndexingServiceManagerTest, and the SonarCloud upload itself failing for lack of a local SONAR_TOKEN) are pre-existing and outside the scope of this ticket.
  • Nightly analyze.yml run on stage after merge no longer reports the four CloneRepositoryViewModelTest failures.

Surfaced by the nightly Jacoco/SonarQube analyze.yml run after the
ADFA-4306 (#1385) fixes landed.  Four :app testV8DebugUnitTest cases
failed:

  * cloneRepository fails if destination directory does not exist after clone
  * cloneRepository failure updates UI with error message
  * test clone repository with auth is successful
  * cloneRepository success updates UI state correctly

CloneRepositoryViewModel.cloneRepository() calls
blankj.utilcode.NetworkUtils.isConnected(), which lazily fires
Utils.init() -> android.util.Log.e().  Under the plain JVM test runner
Log is not stubbed, so the call throws RuntimeException("Method e in
android.util.Log not mocked.") before any assertion runs.

ADFA-4306 suggested mockkStatic(Log::class) (mirroring the
FileDeleteUtilsTest fix), but for this VM that only patches the
transitive symptom and still leaves NetworkUtils.getActiveNetworkInfo()
walking into Utils.getApp().getSystemService(...) with no Application
context.  The more surgical fix is to mock NetworkUtils directly:

  mockkStatic(NetworkUtils::class)
  every { NetworkUtils.isConnected() } returns true

That matches the semantic intent of these tests (they exercise the clone
attempt path, which presumes connectivity) without depending on blankj
internals.  The existing @after unmockkAll() already covers cleanup.

Verified locally by re-running the analyze.yml gradle command
(:testing:tooling:assemble :testing:common:assemble sonarqube --info
--no-build-cache -x lint --continue):
CloneRepositoryViewModelTest now reports tests=9 failures=0 errors=0.
@coderabbitai

coderabbitai Bot commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: fe9a510e-8db8-439e-8afb-239abad5c96b

📥 Commits

Reviewing files that changed from the base of the PR and between 8082c92 and 6a0e56c.

📒 Files selected for processing (1)
  • app/src/test/java/com/itsaky/androidide/viewmodel/CloneRepositoryViewModelTest.kt

📝 Walkthrough

Release Notes

  • Fixed ADFA-4312: Resolved four failing unit test cases in CloneRepositoryViewModelTest that were throwing RuntimeException: Method e in android.util.Log not mocked during nightly CI/CD runs
  • Root cause: NetworkUtils.isConnected() from blankj-utilcode library lazily initializes internal utilities that invoke android.util.Log.e(), which is not available in plain JVM unit tests
  • Solution: Added static mock for NetworkUtils.isConnected() returning true in test setup, allowing tests to proceed to the actual code paths being exercised
  • Impact: All 9 tests in CloneRepositoryViewModelTest now pass with zero failures or errors
  • Test cleanup: Automatic cleanup via existing @After unmockkAll() ensures no test pollution between test runs

Notes and Considerations

  • ⚠️ Best Practice Note: The mock assumes connectivity is available for all test scenarios; if any future tests require disconnected-state behavior, additional setup will be needed
  • Related code: Similar NetworkUtils.isConnected() calls exist in GitBottomSheetViewModel.kt, which may require identical mocking treatment if JVM unit tests are added in the future
  • Pragmatic approach: Static mocking of external library methods avoids dependency injection refactoring while addressing the core issue without masking it with Log class mocks

Walkthrough

Adds static mocking of NetworkUtils.isConnected() in CloneRepositoryViewModelTest setup, forcing it to return true to prevent Utils.init() Android-logging side effects from failing under plain JVM unit test conditions. Two new imports support the mock.

Changes

NetworkUtils Static Mock in CloneRepositoryViewModelTest

Layer / File(s) Summary
Static mock NetworkUtils.isConnected() in test setup
app/src/test/java/com/itsaky/androidide/viewmodel/CloneRepositoryViewModelTest.kt
Imports NetworkUtils and mockkStatic, then stubs NetworkUtils.isConnected() to return true in @Before setup() so the ViewModel's cloneRepository() call proceeds without Android-only side effects.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~2 minutes

Poem

🐇 Hopping through the JVM land,
No network needed, mocks at hand!
isConnected() returns true with glee,
No Android logs to bother me.
The tests now run, fast and free! 🎉

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely identifies the main change: mocking NetworkUtils in a specific test class.
Description check ✅ Passed The description is comprehensive and directly related to the changeset, explaining the root cause, the fix, and the testing performed.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch ADFA-4312-mock-log-clone-repo-test

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@hal-eisen-adfa hal-eisen-adfa requested a review from a team June 17, 2026 03:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant