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

Added Unit Tests for TiltSwitchHelper Class #305

Merged
merged 1 commit into from
Feb 10, 2025

Conversation

RalphTan37
Copy link
Collaborator

Fixes #301

What was changed?

Added unit tests for the TiltSwitchHelper class using JUnit 5 and Mockito. The unit tests include checking the initialization logic, the event listener logic, the state of the mockDigitalInput when an update occurs, and the edge cases. There is no unit test for logging the initialization methods yet.

Why was it changed?

The unit tests were implemented to check the initialization logic, state updates, and event listener management. The TiltSwitchHelper class is used to handle the tilt switch sensor using the Pi4J Library. The unit tests ensure that the TiltSwitchHelper class functions correctly, reliably, and handles edge cases. These unit tests strengthen assurance in the codebase before hardware integration on the Raspberry Pi.

How was it changed?

The unit tests are implemented using JUnit 5 and Mockito to simulate the different states of the DigitalState and to check that the isTilted field is correctly initialized. Unit tests for the addEventListener and removeEventListener methods to handle a custom listener so that they can allow external components to react dynamically to tilt sensor changes. Whenever the state of mockDigitalInput changes, the unit tests validate and ensure that the TiltSwitchHelper class correctly interprets the sensor's state.

Tests

@CLAassistant
Copy link

CLAassistant commented Feb 3, 2025

CLA assistant check
All committers have signed the CLA.

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.

Good job, I just had one comment I was hoping you could go over.

TiltSwitchHelper tiltSwitchHelper = new TiltSwitchHelper(mockDigitalInput);

//Verify that isTilted is initialized to false
assertFalse(tiltSwitchHelper.isTilted, "isTilted should be false when the input is low.");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are you able to make this a variable since you use it in initialize high and low states?

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 line verify(mockDigitalInput, times(1)).addListener(any(DigitalStateChangeListener.class)); appears in multiple functions, which is redundant. It would be more efficient to refactor this line into a single method to avoid repetition. Additionally, you're calling reset(mockDigitalInput) in several tests, but since you're already creating a fresh mock in @beforeeach, resetting it may not be necessary. Removing this will simplify the code and eliminate redundancy, making it more concise and easier to maintain. Great work overall—just these small changes will improve code clarity and efficiency.

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 implementation of TiltSwitchHelperTest looks good overall. The test cases cover the expected functionality, and the mocking is properly handled, this PR is approved. Nice work!

@yrlmanoharreddy yrlmanoharreddy merged commit 268b5e7 into main Feb 10, 2025
6 checks passed
@yrlmanoharreddy yrlmanoharreddy deleted the TiltSwitchHelperTestsAdded branch February 10, 2025 02:38
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 TiltSwitchHelper Class
4 participants