docs(android): add Unit Testing guide [SDC-31985]#377
Conversation
Let the Kotlin snippet validator compile examples that use test/mocking libraries: - add MockK, Mockito (core + kotlin), kotlin-test, JUnit and androidx.test as compileOnly deps in the snippet test-bed - pass -jvm-target 11 to kotlinc (MockK's inline functions require it) - pin CI kotlinc to 2.4.0 so it matches the kotlin-test version Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Document how to unit-test scanner listener/callback logic without a camera by mocking the SDK's final, native-backed classes with MockK or Mockito (inline), covering all scan modes plus FrameData/Feedback. Includes instrumented real-object tests and a BitmapFrameSource real-decode pattern. Linked in the Android sidebar next to Agent Skills. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
- group the on-device sections under a single "Instrumented Tests" heading (real objects, real view, real decoding) with the setup and one-framework caution up front - add a real BarcodeCountView example (built via ActivityScenario) - lead with the capability instead of "This page describes…" - order the mock reasons native-construction first - trim the license note Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Interfaces can't be declared locally in Kotlin, so a snippet that defines its own interface (e.g. an app-facing abstraction) failed to compile when wrapped in the generated validate() method. Hoist top-level `interface` declarations to package scope, the same way standalone `object`s are handled. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
- present two approaches up front (isolate behind your own abstraction, or mock the SDK's types), matching the iOS guide - add an "Isolate the SDK Behind Your Own Abstraction" section: a thin adapter over your own interface, tested with no SDK objects - add MockK + Mockito examples for MatrixScan (BarcodeBatch) and SparkScan so every mode the ticket covers has a snippet - note that nested SDK-produced results (e.g. a CapturedId's documents) must be mocked too, and add a threading tip for background-thread callbacks - reorganize the on-device sections under one "Instrumented Tests" heading Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
raffaelefarinaro
left a comment
There was a problem hiding this comment.
Two small things on the "Verifying Real Decoding" snippet, both reader-facing (CI passes because the validator supplies an ambient binding the reader won't have). Otherwise this looks great.
| }) | ||
|
|
||
| // A bitmap containing a known barcode, e.g. loaded from your test assets. | ||
| val bitmap = BitmapFactory.decodeStream(context.assets.open("test-qr.png")) |
There was a problem hiding this comment.
context is undefined here. The snippet validator injects an ambient context, so this compiles in CI, but a reader pasting this into a real androidTest has nothing in scope. Suggest grabbing it explicitly first, e.g.
val context = InstrumentationRegistry.getInstrumentation().targetContextbefore the assets.open(...) call (and add the androidx.test.platform.app.InstrumentationRegistry import to the hidden block).
| dataCaptureContext.setFrameSource(frameSource) | ||
| frameSource.switchToDesiredState(FrameSourceState.ON) | ||
|
|
||
| latch.await(10, TimeUnit.SECONDS) |
There was a problem hiding this comment.
The latch.await(...) return value is ignored. If decoding never fires (invalid/missing license, no match in the image), the latch just times out and the test then asserts on null rather than failing on the timeout. Suggest modelling a correct test:
assertTrue(latch.await(10, TimeUnit.SECONDS), "decoding timed out")
Adds an Android Unit Testing page (
docs/sdks/android/unit-testing.mdx), linked in the Android sidebar next to Agent Skills. Companion to the iOS guide (SDC-31976).What it covers
finaland call native code on construction, so real construction throwsUnsatisfiedLinkErroron the host JVM.BarcodeBatch), ID Capture, SparkScan — plus BarcodePick,FrameData, andFeedback.BarcodeCountView, and real decoding viaBitmapFrameSource.relaxed = trueon native-backed mocks; background-thread callbacks).Validation tooling
The snippet validator gained two changes so these examples compile against the real SDK in CI:
kotlincpinned to 2.4.0 (matchingkotlin-test);-jvm-target 11for MockK's inline functions.interfacedeclarations are hoisted to package scope (Kotlin forbids local interfaces).All snippets compile:
validate-code-snippets.py kotlin→ 161 passed, 0 failed. Verified locally and underactwith the CI toolchain.🤖 Generated with Claude Code