Skip to content
Merged
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
27 changes: 24 additions & 3 deletions src/test/java/frc/robot/subsystems/VisionIITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -72,7 +75,7 @@ void setUp() {
* and returns the estimated poses.
*/
@Test
void getVisionUpdates_WhenCamerasHaveResults_ShouldReturnEstimatedPoses() {
void testGetVisionUpdatesWithResults() {
// --- ARRANGE ---
// In the "Arrange" phase, we set up the state of our mocks.
// We are defining what should happen when methods are called on our mock objects.
Expand Down Expand Up @@ -100,14 +103,22 @@ void getVisionUpdates_WhenCamerasHaveResults_ShouldReturnEstimatedPoses() {
// We also check that the results are the same mock objects we arranged to be returned.
assertEquals(mockEstimatedPose, results.get(0), "The first pose should be the one from the right camera");
assertEquals(mockEstimatedPose, results.get(1), "The second pose should be the one from the left camera");

// Mockito's verify() method is used to check if certain methods on mock objects
// were called during the test execution. This ensures that the interactions
// with the dependencies happened as expected.
verify(mockLeftCam).getAllUnreadResults();
verify(mockRightCam).getAllUnreadResults();
verify(mockLeftEstimator).estimateCoprocMultiTagPose(mockPipelineResult);
verify(mockRightEstimator).estimateCoprocMultiTagPose(mockPipelineResult);
}

/**
* This test case covers the scenario where the cameras have no new results.
* We expect the getVisionUpdates method to return an empty list.
*/
@Test
void getVisionUpdates_WhenCamerasHaveNoResults_ShouldReturnEmptyList() {
void testGetVisionUpdatesWithNoResults() {
// --- ARRANGE ---
// We configure the mock cameras to return an empty list.
when(mockLeftCam.getAllUnreadResults()).thenReturn(List.of());
Expand All @@ -119,6 +130,11 @@ void getVisionUpdates_WhenCamerasHaveNoResults_ShouldReturnEmptyList() {
// --- ASSERT ---
// We assert that the returned list is empty.
assertTrue(results.isEmpty(), "The list of poses should be empty");

verify(mockLeftCam).getAllUnreadResults();
verify(mockRightCam).getAllUnreadResults();
verify(mockLeftEstimator, never()).estimateCoprocMultiTagPose(any());
verify(mockRightEstimator, never()).estimateCoprocMultiTagPose(any());
}

/**
Expand All @@ -127,7 +143,7 @@ void getVisionUpdates_WhenCamerasHaveNoResults_ShouldReturnEmptyList() {
* In this case, the estimator returns an empty Optional.
*/
@Test
void getVisionUpdates_WhenEstimatorsReturnEmpty_ShouldReturnEmptyList() {
void testGetVisionUpdatesWithNoEstimates() {
// --- ARRANGE ---
// The cameras have results...
when(mockLeftCam.getAllUnreadResults()).thenReturn(List.of(mockPipelineResult));
Expand All @@ -143,5 +159,10 @@ void getVisionUpdates_WhenEstimatorsReturnEmpty_ShouldReturnEmptyList() {
// --- ASSERT ---
// We assert that the returned list is empty because no poses were added.
assertTrue(results.isEmpty(), "The list of poses should be empty");

verify(mockLeftCam).getAllUnreadResults();
verify(mockRightCam).getAllUnreadResults();
verify(mockLeftEstimator).estimateCoprocMultiTagPose(mockPipelineResult);
verify(mockRightEstimator).estimateCoprocMultiTagPose(mockPipelineResult);
}
}
Loading