|
5 | 5 | import org.junit.jupiter.api.BeforeEach; |
6 | 6 | import org.junit.jupiter.api.Test; |
7 | 7 | import org.mockito.Mockito; |
| 8 | +import org.slf4j.Logger; |
| 9 | + |
| 10 | +import java.lang.reflect.Method; |
| 11 | + |
8 | 12 |
|
9 | 13 | import static org.junit.jupiter.api.Assertions.*; |
10 | 14 | import static org.mockito.Mockito.*; |
11 | 15 |
|
| 16 | + |
12 | 17 | class UltraSonicSensorHelperTest { |
13 | 18 |
|
14 | 19 | private DigitalOutput mockTriggerPin; |
15 | 20 | private DigitalInput mockEchoPin; |
16 | 21 | private UltraSonicSensorHelper sensorHelper; |
17 | 22 |
|
| 23 | + Logger log = mock(Logger.class); |
| 24 | + |
18 | 25 | @BeforeEach |
19 | 26 | void setUp() { |
20 | | - // Create "fake" pins using Mockito |
21 | 27 | mockTriggerPin = Mockito.mock(DigitalOutput.class); |
22 | 28 | mockEchoPin = Mockito.mock(DigitalInput.class); |
23 | 29 |
|
24 | | - // Pass them into the helper |
25 | 30 | sensorHelper = new UltraSonicSensorHelper(mockTriggerPin, mockEchoPin); |
26 | 31 | } |
27 | 32 |
|
28 | 33 | @Test |
| 34 | + void testInitialization() { |
| 35 | + verify(mockTriggerPin).low(); |
| 36 | + assertEquals(0.0, sensorHelper.getDistanceInCentimeter()); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void testStartMeasuring() throws InterruptedException { |
| 41 | + sensorHelper.stopMeasuring(); |
| 42 | + sensorHelper.startMeasuring(); |
| 43 | + |
| 44 | + assertDoesNotThrow(sensorHelper::startMeasuring); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void testTriggerAndMeasureDistance() throws Exception { |
| 49 | + Method method = UltraSonicSensorHelper.class.getDeclaredMethod("triggerAndMeasureDistance"); |
| 50 | + method.setAccessible(true); |
| 51 | + |
| 52 | + when(mockTriggerPin.isHigh()).thenReturn(true); |
| 53 | + when(mockEchoPin.isHigh()).thenReturn(true).thenReturn(false); |
| 54 | + |
| 55 | + method.invoke(sensorHelper); |
| 56 | + |
| 57 | + assertTrue(sensorHelper.getDistanceInCentimeter() >= 0); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void testCalculateDistance() {} |
| 62 | + |
| 63 | + @Test |
| 64 | + void testgetDistanceInCentemeter() {} |
| 65 | + |
| 66 | + @Test |
| 67 | + void testgetDistanceInMeters() {} |
| 68 | + |
| 69 | + @Test |
| 70 | + void stopMeasuring() {} |
29 | 71 | } |
0 commit comments