-
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 MicroSwitchController class #304
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
*/ | ||
public class MicroSwitchHelper { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(MicroSwitchHelper.class); | ||
private static Logger log = LoggerFactory.getLogger(MicroSwitchHelper.class); | ||
|
||
private final DigitalInput microSwitchInput; | ||
|
||
|
@@ -64,4 +64,9 @@ public void removeEventListener() | |
microSwitchInputListener = null; | ||
} | ||
} | ||
|
||
public void setLog(Logger log) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great job on this! This will help when troubleshooting your code by setting up a logger. |
||
this.log = log; | ||
} | ||
|
||
} |
123 changes: 123 additions & 0 deletions
123
...ronaut-utils/src/test/java/com/opensourcewithslu/inputdevices/MicroSwitchHelperTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package com.opensourcewithslu.inputdevices; | ||
|
||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
|
||
import com.opensourcewithslu.mock.MockDigitalInput; | ||
import com.pi4j.io.gpio.digital.*; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.slf4j.Logger; | ||
|
||
|
||
/** | ||
* Unit tests for the micro switch helper class | ||
*/ | ||
public class MicroSwitchHelperTests { | ||
|
||
|
||
private MockDigitalInput mockDigitalInput; | ||
private MicroSwitchHelper microSwitchHelper; | ||
|
||
|
||
@BeforeEach | ||
public void setUp(){ | ||
mockDigitalInput = new MockDigitalInput(); | ||
microSwitchHelper = new MicroSwitchHelper(mockDigitalInput); | ||
} | ||
|
||
|
||
@Test | ||
public void testInitializationLogic(){ | ||
//verify isPressed is initialized based on the input state | ||
assertFalse(microSwitchHelper.isPressed); | ||
|
||
|
||
//simulate high state so isPressed is triggered | ||
mockDigitalInput.SetState(DigitalState.HIGH); | ||
MicroSwitchHelper newMicroSwitchHelper = new MicroSwitchHelper(mockDigitalInput); | ||
assertTrue(newMicroSwitchHelper.isPressed); | ||
|
||
|
||
} | ||
|
||
|
||
@Test | ||
public void testAddEventListener() { | ||
DigitalInput mockInput = mock(DigitalInput.class); | ||
SlideSwitchHelper slideSwitchHelper = new SlideSwitchHelper(mockInput); | ||
|
||
|
||
DigitalStateChangeListener dummyListener = mock(DigitalStateChangeListener.class); | ||
|
||
|
||
slideSwitchHelper.addEventListener(dummyListener); | ||
verify(mockInput).addListener(dummyListener); | ||
} | ||
|
||
|
||
@Test | ||
public void testRemoveEventListener(){ | ||
DigitalInput mockInput = mock(DigitalInput.class); | ||
MicroSwitchHelper helper = new MicroSwitchHelper(mockInput); | ||
|
||
|
||
DigitalStateChangeListener dummyListener = mock(DigitalStateChangeListener.class); | ||
helper.addEventListener(dummyListener); | ||
|
||
|
||
helper.removeEventListener(); | ||
verify(mockInput).removeListener(dummyListener); | ||
} | ||
|
||
|
||
@Test | ||
public void testStateUpdate(){ | ||
//starting with state at low | ||
mockDigitalInput.SetState(DigitalState.LOW); | ||
MicroSwitchHelper helperLow = new MicroSwitchHelper(mockDigitalInput); | ||
assertFalse(helperLow.isPressed); | ||
|
||
|
||
//switching to high | ||
mockDigitalInput.SetState(DigitalState.HIGH); | ||
MicroSwitchHelper helperHigh = new MicroSwitchHelper(mockDigitalInput); | ||
assertTrue(helperHigh.isPressed); | ||
|
||
|
||
//switching state back to low | ||
mockDigitalInput.SetState(DigitalState.LOW); | ||
MicroSwitchHelper helperLowAgain = new MicroSwitchHelper(mockDigitalInput); | ||
assertFalse(helperLowAgain.isPressed); | ||
} | ||
|
||
|
||
@Test | ||
void testLogsOnInitialize() { | ||
DigitalInput mockInput = mock(DigitalInput.class); | ||
Logger loggerMock = mock(Logger.class); | ||
|
||
|
||
MicroSwitchHelper helper = new MicroSwitchHelper(mockInput); | ||
helper.setLog(loggerMock); | ||
helper.initialize(); | ||
|
||
|
||
verify(loggerMock).info("Initializing Micro Switch"); | ||
} | ||
|
||
|
||
@Test | ||
public void testRemoveEventListenerNoEventListener(){ | ||
DigitalInput mockInput = mock(DigitalInput.class); | ||
MicroSwitchHelper helper = new MicroSwitchHelper(mockInput); | ||
// no call to add event listener | ||
|
||
|
||
//remove event listener and verify that it was called properly | ||
helper.removeEventListener(); | ||
verify(mockInput).removeListener(any(DigitalStateChangeListener.class)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This provides flexibility to modify logger messages.