-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved tests to new LEDHelperTest class. Added log verification
- Loading branch information
1 parent
49fb5ed
commit 4a96fb4
Showing
3 changed files
with
104 additions
and
81 deletions.
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
101 changes: 101 additions & 0 deletions
101
pi4micronaut-utils/src/test/java/com/opensourcewithslu/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,101 @@ | ||
package com.opensourcewithslu; | ||
|
||
import com.pi4j.io.gpio.digital.DigitalOutput; | ||
import org.junit.jupiter.api.*; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.mockito.Spy; | ||
import org.slf4j.Logger; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.mockito.Mockito.*; | ||
|
||
import com.opensourcewithslu.outputdevices.LEDHelper; | ||
|
||
class LEDHelperTest { | ||
@Mock | ||
// @Spy | ||
Logger log; | ||
@Mock | ||
DigitalOutput ledOutput; | ||
@InjectMocks | ||
LEDHelper ledHelper; | ||
|
||
private AutoCloseable closeable; | ||
|
||
@BeforeEach | ||
public void openMocks() { | ||
closeable = MockitoAnnotations.openMocks(this); | ||
} | ||
|
||
@AfterEach | ||
public void releaseMocks() throws Exception { | ||
closeable.close(); | ||
} | ||
|
||
@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"); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,74 +0,0 @@ | ||
package com.opensourcewithslu; | ||
|
||
import com.pi4j.io.gpio.digital.DigitalOutput; | ||
import io.micronaut.runtime.EmbeddedApplication; | ||
//import io.micronaut.test.extensions.junit5.annotation.MicronautTest; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.Assertions; | ||
import jakarta.inject.Inject; | ||
import static org.mockito.Mockito.*; | ||
import java.util.concurrent.TimeUnit; | ||
import com.opensourcewithslu.outputdevices.LEDHelper; | ||
|
||
// @MicronautTest | ||
class Pi4MicronautUtilsTest { | ||
/* @Inject EmbeddedApplication<?> application; | ||
@Test | ||
void testItWorks() { | ||
Assertions.assertTrue(application.isRunning()); | ||
}*/ | ||
|
||
DigitalOutput ledOutput = mock(DigitalOutput.class); | ||
LEDHelper ledHelper = new LEDHelper(ledOutput); | ||
|
||
@Test | ||
void ledOn_turnsOnWhenCurrentlyOff() { | ||
when(ledOutput.isLow()).thenReturn(true); | ||
ledHelper.ledOn(); | ||
verify(ledOutput).high(); | ||
} | ||
|
||
@Test | ||
void ledOn_doesNotTurnOnWhenAlreadyOn() { | ||
when(ledOutput.isLow()).thenReturn(false); | ||
ledHelper.ledOn(); | ||
verify(ledOutput, never()).high(); | ||
} | ||
|
||
@Test | ||
void ledOff_turnsOffWhenCurrentlyOn() { | ||
when(ledOutput.isHigh()).thenReturn(true); | ||
ledHelper.ledOff(); | ||
verify(ledOutput).low(); | ||
} | ||
|
||
@Test | ||
void ledOff_doesNotTurnOffWhenAlreadyOff() { | ||
when(ledOutput.isHigh()).thenReturn(false); | ||
ledHelper.ledOff(); | ||
verify(ledOutput, never()).low(); | ||
} | ||
|
||
@Test | ||
void switchState_togglesStateWhenCurrentlyOn() { | ||
when(ledOutput.isHigh()).thenReturn(true); | ||
ledHelper.switchState(); | ||
verify(ledOutput).low(); | ||
} | ||
|
||
@Test | ||
void switchState_togglesStateWhenCurrentlyOff() { | ||
when(ledOutput.isHigh()).thenReturn(false); | ||
when(ledOutput.isLow()).thenReturn(true); // Called when switchState() calls ledOn() | ||
ledHelper.switchState(); | ||
verify(ledOutput).high(); | ||
} | ||
|
||
@Test | ||
void blink_callsBlinkWithCorrectParameters() { | ||
int duration = 1000; | ||
ledHelper.blink(duration); | ||
verify(ledOutput).blink(duration, TimeUnit.MILLISECONDS); | ||
} | ||
} | ||