Skip to content
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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
public class MicroSwitchHelper {

private static final Logger log = LoggerFactory.getLogger(MicroSwitchHelper.class);
Copy link
Collaborator

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.

private static Logger log = LoggerFactory.getLogger(MicroSwitchHelper.class);

private final DigitalInput microSwitchInput;

Expand Down Expand Up @@ -64,4 +64,9 @@ public void removeEventListener()
microSwitchInputListener = null;
}
}

public void setLog(Logger log) {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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;
}

}
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));
}
}