-
Notifications
You must be signed in to change notification settings - Fork 7
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
Conversation
There was a problem hiding this 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."); |
There was a problem hiding this comment.
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?
There was a problem hiding this 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.
There was a problem hiding this 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!
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.