-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #252 from oss-slu/247-add-unit-tests-for-ledhelper…
…-class Resolved Issue #247: Added unit tests for LEDHelper functions
- Loading branch information
Showing
3 changed files
with
98 additions
and
1 deletion.
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
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
84 changes: 84 additions & 0 deletions
84
pi4micronaut-utils/src/test/java/com/opensourcewithslu/outputdevices/LEDHelperTest.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,84 @@ | ||
package com.opensourcewithslu.outputdevices; | ||
|
||
import com.pi4j.io.gpio.digital.DigitalOutput; | ||
import org.junit.jupiter.api.*; | ||
import org.slf4j.Logger; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.mockito.Mockito.*; | ||
|
||
class LEDHelperTest { | ||
DigitalOutput ledOutput = mock(DigitalOutput.class); | ||
LEDHelper ledHelper = new LEDHelper(ledOutput); | ||
Logger log = mock(Logger.class); | ||
|
||
@BeforeEach | ||
public void openMocks() { | ||
ledHelper.setLog(log); | ||
} | ||
|
||
@Test | ||
void ledOnTurnsOnWhenOff() { | ||
when(ledOutput.isLow()).thenReturn(true); | ||
ledHelper.ledOn(); | ||
verify(ledOutput).high(); | ||
} | ||
|
||
@Test | ||
void ledOnDoesNotTurnOnWhenOn() { | ||
when(ledOutput.isLow()).thenReturn(false); | ||
ledHelper.ledOn(); | ||
verify(ledOutput, never()).high(); | ||
} | ||
|
||
@Test | ||
void ledOffTurnsOffWhenOn() { | ||
when(ledOutput.isHigh()).thenReturn(true); | ||
ledHelper.ledOff(); | ||
verify(ledOutput).low(); | ||
} | ||
|
||
@Test | ||
void ledOffDoesNotTurnOffWhenOff() { | ||
when(ledOutput.isHigh()).thenReturn(false); | ||
ledHelper.ledOff(); | ||
verify(ledOutput, never()).low(); | ||
} | ||
|
||
@Test | ||
void switchStateTogglesStateWhenOn() { | ||
when(ledOutput.isHigh()).thenReturn(true); | ||
ledHelper.switchState(); | ||
verify(ledOutput).low(); | ||
} | ||
|
||
@Test | ||
void switchStateTogglesStateWhenOff() { | ||
when(ledOutput.isHigh()).thenReturn(false); | ||
when(ledOutput.isLow()).thenReturn(true); // Called when switchState() calls ledOn() | ||
ledHelper.switchState(); | ||
verify(ledOutput).high(); | ||
} | ||
|
||
@Test | ||
void blinkCallsWithCorrectParameters() { | ||
int duration = 1000; | ||
ledHelper.blink(duration); | ||
verify(ledOutput).blink(duration, TimeUnit.MILLISECONDS); | ||
} | ||
|
||
@Test | ||
void ledOnLogsWhenTurnedOn() { | ||
when(ledOutput.isLow()).thenReturn(true); | ||
ledHelper.ledOn(); | ||
verify(log).info("Turning on LED"); | ||
} | ||
|
||
@Test | ||
void ledOffLogsWhenTurnedOff() { | ||
when(ledOutput.isHigh()).thenReturn(true); | ||
ledHelper.ledOff(); | ||
verify(log).info("Turning off LED"); | ||
} | ||
} |