Skip to content

test(inputdevices): add unit tests for UltraSonicSensorHelper - #362

Merged
leandrumartin merged 6 commits into
mainfrom
361-write-unit-tests-for-ultrasonicsensorhelper
Sep 19, 2025
Merged

test(inputdevices): add unit tests for UltraSonicSensorHelper#362
leandrumartin merged 6 commits into
mainfrom
361-write-unit-tests-for-ultrasonicsensorhelper

Conversation

@j-heitz

@j-heitz j-heitz commented Sep 10, 2025

Copy link
Copy Markdown
Collaborator

Resolves issue #361

Summary

Add comprehensive JUnit tests for com.opensourcewithslu.inputdevices.UltraSonicSensorHelper. Tests cover initialization, start/stop behavior, measurement flow, distance calculations (cm/m), and edge cases (negative duration, echo timeout). Logger is injected and assertions around logging are included where appropriate.

Motivation

Improve unit test coverage for sensor logic so changes are safer and hardware-independent. Tests mock GPIO interactions allowing CI to run without physical devices.

Changes

  • Added tests in:
    • pi4micronaut-utils/src/test/java/com/opensourcewithslu/inputdevices/UltraSonicSensorHelperTest.java
  • Tests included:
    • testInitialization — verifies trigger pin set low and initial distance is zero.
    • testStartMeasuring / testStartMeasuringWhenInactive — verify safe start behavior and inactivity handling.
    • testTriggerAndMeasureDistance / testTriggerAndMeasureDistanceTimeout — exercise measurement flow and timeout handling via reflection.
    • testCalculateDistance / testCalculateDistanceWithNegativeDuration — validate distance calculation and negative-duration handling.
    • testgetDistanceInCentemeter / testgetDistanceInMeters — unit conversion checks.
    • stopMeasuring / testStopMeasuringDeactivatesSensor — ensure stop is safe and deactivates measurements.
    • made changes to base class UltraSonicSensorHelper to allow testing of log messages and added log messages in startMeasuring and stopMeasuring

Files changed

  • pi4micronaut-utils/src/test/java/com/opensourcewithslu/inputdevices/UltraSonicSensorHelperTest.java
  • pi4micronaut-utils/src/main/com/opensourcewithslu/inputdevices/UltraSonicSensorHelper.java

How to test

  • Run locally: ./gradlew test
  • Confirm all unit tests pass in CI.

@j-heitz j-heitz linked an issue Sep 10, 2025 that may be closed by this pull request
9 tasks
@CLAassistant

CLAassistant commented Sep 10, 2025

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@leandrumartin leandrumartin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work on figuring out how to set up the testing and mocking! You still need to verify that appropriate log messages are being outputted and that errors are handled with appropriate error messages.

@j-heitz j-heitz changed the title 361 write unit tests for ultrasonicsensorhelper test(inputdevices): add unit tests for UltraSonicSensorHelper Sep 17, 2025
@j-heitz
j-heitz marked this pull request as ready for review September 18, 2025 20:43
@leandrumartin

Copy link
Copy Markdown
Collaborator

Maybe your PR description is outdated, but under "Files Changed" it only lists one file, when in fact two are changed as you write elsewhere in the description. Always check LLM output carefully.

@leandrumartin
leandrumartin merged commit 79ef1d4 into main Sep 19, 2025
8 of 9 checks passed
@leandrumartin
leandrumartin deleted the 361-write-unit-tests-for-ultrasonicsensorhelper branch September 19, 2025 22:36
@leandrumartin

Copy link
Copy Markdown
Collaborator

Congrats on your first PR merge! 🎉

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.

Write unit tests for UltraSonicSensorHelper

4 participants