Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add comprehensive unit tests for PIRSensorHelper #312

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

Ed0827
Copy link
Collaborator

@Ed0827 Ed0827 commented Feb 15, 2025

Fixes #309

What was changed:

  1. Added unit tests for the PIRSensorHelper in PIRSensorHelperTest.
  2. Tests cover initialization logic, motion detection, and event listener management.
  3. Edge cases such as an initially low sensor state, overriding event listeners, and multiple calls to removeEventListener have been verified.

Why was it changed:

  1. To improve the reliability and maintainability of the PIRSensorHelper functionality.
  2. Enhanced test coverage ensures that the sensor behavior is validated without needing actual hardware.
  3. The changes help prevent regressions and simplify future development.

How was it changed:

  1. Implemented tests using JUnit and Mockito to mock dependencies like DigitalInput and DigitalStateChangeListener.
  2. The tests simulate sensor state changes to validate that 'isMoving' updates correctly.
  3. Added tests to ensure that addEventListener properly overrides existing listeners and that multiple calls to removeEventListener are handled gracefully.

@Ed0827 Ed0827 assigned Ed0827 and unassigned Ed0827 Feb 15, 2025
@yrlmanoharreddy yrlmanoharreddy self-requested a review February 17, 2025 01:03
Copy link
Collaborator

@yrlmanoharreddy yrlmanoharreddy left a comment

Choose a reason for hiding this comment

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

The unit tests in PIRSensorHelperTest are well-structured, but the assertion messages can be improved for better clarity. For instance, instead of "Expected isMoving to be true when DigitalInput is high", use "Expected isMoving to be true when PIR sensor detects motion (DigitalInput is high)". This makes the test output more informative. Please update the assertion messages accordingly and push the changes. The rest of the implementation looks great! 👍

Copy link
Collaborator

@avaenk avaenk left a comment

Choose a reason for hiding this comment

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

Great job!

@BeforeEach
void setUp() {
pirSensorInput = mock(DigitalInput.class);
when(pirSensorInput.isHigh()).thenReturn(true);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Be sure this line does not cause any undefined behavior in your other tests, I tried a similar thing in my PR and noticed some strange behavior in my tests and ended up removing it. If it doesn't cause any issues feel free to keep.

pirSensorHelper = new PIRSensorHelper(pirSensorInput);
}

@Test
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe you could create one function for the test is moving instead of re-declaring the pirSensorHelper multiple times. It's possible to find a way to combine these functions into one, have the same functionality, and only have a few more lines of code.


@Test
void testMultipleRemoveEventListenerCalls() {
pirSensorHelper.removeEventListener();
Copy link
Collaborator

Choose a reason for hiding this comment

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

For our testing purposes I see why you use only two calls to the event listener, however, on your own time before re-committing the code ensure that 4-5 calls to the event listener causes no undefined behavior, two calls may not be enough to be sure.

@Test
void testInitializationLogsCaptureSystemOut() {
PrintStream originalOut = System.out;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Love the use of ByteArrayOutputStream, awesome!

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 PIRSensorHelper
3 participants