Skip to content

Commit

Permalink
Merge pull request #252 from oss-slu/247-add-unit-tests-for-ledhelper…
Browse files Browse the repository at this point in the history
…-class

Resolved Issue #247: Added unit tests for LEDHelper functions
  • Loading branch information
yrlmanoharreddy authored Sep 22, 2024
2 parents 9f93843 + 32195aa commit 3221dc9
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
4 changes: 4 additions & 0 deletions pi4micronaut-utils/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.5.1'
testImplementation 'org.junit.jupiter:junit-jupiter'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
testImplementation("org.mockito:mockito-core:5.+")
testImplementation(platform("org.junit:junit-bom:5.11.0"))
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
testRuntimeOnly("org.junit.vintage:junit-vintage-engine")

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*
*/
public class LEDHelper {
private static final Logger log = LoggerFactory.getLogger(LEDHelper.class);
private static Logger log = LoggerFactory.getLogger(LEDHelper.class);

private final DigitalOutput ledOutput;

Expand Down Expand Up @@ -82,4 +82,13 @@ public void blink (int duration) {

ledOutput.blink(duration, TimeUnit.MILLISECONDS);
}

/**
* Sets the logger object.
*
* @param log Logger object to set the logger to.
*/
public void setLog(Logger log) {
this.log = log;
}
}
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");
}
}

0 comments on commit 3221dc9

Please sign in to comment.