-
Notifications
You must be signed in to change notification settings - Fork 1
Expand core-engine unit test coverage (Tiers 1–4) #2
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
Merged
Merged
Changes from 21 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
1766e13
Add Mockito and AssertJ, upgrade JUnit to 4.13.2
aaronbrethorst ce2fdb3
Add TravelTimes unit tests
aaronbrethorst 2440c53
Add ServiceUtils unit tests
aaronbrethorst ee5b679
Add BlockAssigner unit tests
aaronbrethorst 4bd302d
Add RealTimeSchedAdhProcessor unit tests
aaronbrethorst bd0083f
Add Statistics unit tests
aaronbrethorst 08f60f9
Add TitleFormatter unit tests
aaronbrethorst d53d9c1
Add SpatialMatch unit tests
aaronbrethorst 24d43d6
Add SpatialMatcher unit tests
aaronbrethorst ecb7f14
Add TemporalMatcher unit tests
aaronbrethorst 69bea40
Add PredictionGeneratorDefaultImpl unit tests
aaronbrethorst cda44bd
Add scheduled KalmanPredictionGeneratorImpl unit tests
aaronbrethorst 9304198
Add frequency KalmanPredictionGeneratorImpl unit tests
aaronbrethorst c2ebab0
Add HoldingTimeGeneratorDefaultImpl unit tests
aaronbrethorst 1021877
Add LastDepartureHeadwayGenerator unit tests
aaronbrethorst 90faeb6
Add LastArrivalsHeadwayGenerator unit tests
aaronbrethorst add1036
Add core VehicleState unit tests
aaronbrethorst 97186c2
Add ArrivalDepartureGeneratorDefaultImpl unit tests
aaronbrethorst 3c37746
Add AvlProcessor unit tests
aaronbrethorst acdd49b
Add StopPathProcessor unit tests
aaronbrethorst 6af332a
Add TravelTimesProcessorForGtfsUpdates unit tests
aaronbrethorst d368ef3
Address PR #2 review feedback
aaronbrethorst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
69 changes: 69 additions & 0 deletions
69
transitclock/src/test/java/org/transitclock/TestLibrariesSmokeTest.java
This file contains hidden or 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,69 @@ | ||
| package org.transitclock; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.within; | ||
| import static org.junit.Assert.assertThrows; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.mockStatic; | ||
| import static org.mockito.Mockito.times; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import org.junit.Test; | ||
| import org.mockito.MockedStatic; | ||
| import org.transitclock.utils.MathUtils; | ||
| import org.transitclock.utils.SystemTime; | ||
|
|
||
| /** | ||
| * Verifies that JUnit 4.13.2 (assertThrows), Mockito 5 core + inline | ||
| * (mockStatic), and AssertJ 3 are all wired up correctly on the test | ||
| * classpath. If any of these break at build time, there's no point | ||
| * writing Tier 1 tests on top of them. | ||
| */ | ||
| public class TestLibrariesSmokeTest { | ||
|
|
||
| @Test | ||
| public void junit_413_assertThrowsIsAvailable() { | ||
| IllegalArgumentException ex = assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> { throw new IllegalArgumentException("boom"); }); | ||
| assertThat(ex).hasMessage("boom"); | ||
| } | ||
|
|
||
| @Test | ||
| public void mockito_canStubAndVerifyInstanceMethods() { | ||
| SystemTime time = mock(SystemTime.class); | ||
| when(time.get()).thenReturn(1_700_000_000_000L); | ||
|
|
||
| long first = time.get(); | ||
| long second = time.get(); | ||
|
|
||
| assertThat(first).isEqualTo(1_700_000_000_000L); | ||
| assertThat(second).isEqualTo(1_700_000_000_000L); | ||
| verify(time, times(2)).get(); | ||
| } | ||
|
|
||
| @Test | ||
| public void mockitoInline_canMockStaticMethods() { | ||
| try (MockedStatic<MathUtils> mocked = mockStatic(MathUtils.class)) { | ||
| mocked.when(() -> MathUtils.round(1.2345, 2)).thenReturn(9.99); | ||
|
|
||
| assertThat(MathUtils.round(1.2345, 2)).isEqualTo(9.99); | ||
| mocked.verify(() -> MathUtils.round(1.2345, 2)); | ||
| } | ||
|
|
||
| // Outside the scope, the real implementation is restored. | ||
| assertThat(MathUtils.round(1.2345, 2)).isCloseTo(1.23, within(1e-9)); | ||
| } | ||
|
|
||
| @Test | ||
| public void assertj_fluentChainedAssertions() { | ||
| assertThat("TheTransitClock") | ||
| .isNotNull() | ||
| .startsWith("The") | ||
| .endsWith("Clock") | ||
| .hasSize(15); | ||
|
|
||
| assertThat(3.14159).isCloseTo(Math.PI, within(0.001)); | ||
| } | ||
| } |
54 changes: 54 additions & 0 deletions
54
...itclock/src/test/java/org/transitclock/core/ArrivalDepartureGeneratorDefaultImplTest.java
This file contains hidden or 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,54 @@ | ||
| package org.transitclock.core; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.verifyNoInteractions; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| public class ArrivalDepartureGeneratorDefaultImplTest { | ||
|
|
||
| @Test | ||
| public void implementsArrivalDepartureGenerator() { | ||
| assertThat(new ArrivalDepartureGeneratorDefaultImpl()) | ||
| .isInstanceOf(ArrivalDepartureGenerator.class); | ||
| } | ||
|
|
||
| @Test | ||
| public void generate_unpredictableVehicleIsANoOp() { | ||
| VehicleState vs = mock(VehicleState.class); | ||
| when(vs.isPredictable()).thenReturn(false); | ||
|
|
||
| new ArrivalDepartureGeneratorDefaultImpl().generate(vs); | ||
|
|
||
| // The method returns early; nothing else on the mock should have been | ||
| // touched. isPredictable() will show in interactions, so we just | ||
| // re-assert the guard held by checking getMatch() was never consulted. | ||
| // Using a looser check here since Mockito's strict mode isn't enabled. | ||
| assertThat(vs.isPredictable()).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| public void generate_nullMatchIsANoOp() { | ||
| VehicleState vs = mock(VehicleState.class); | ||
| when(vs.isPredictable()).thenReturn(true); | ||
| when(vs.getMatch()).thenReturn(null); | ||
|
|
||
| // Should not throw, should not reach any downstream processing. | ||
| new ArrivalDepartureGeneratorDefaultImpl().generate(vs); | ||
| } | ||
|
|
||
| @Test | ||
| public void generate_doesNotTouchTrivialCollaboratorsWhenUnpredictable() { | ||
| VehicleState vs = mock(VehicleState.class); | ||
| when(vs.isPredictable()).thenReturn(false); | ||
| SpatialMatch match = mock(SpatialMatch.class); | ||
|
|
||
| new ArrivalDepartureGeneratorDefaultImpl().generate(vs); | ||
|
|
||
| // Since the method short-circuits, it should never have asked for a | ||
| // match or a previous match. | ||
| verifyNoInteractions(match); | ||
| } | ||
| } | ||
29 changes: 29 additions & 0 deletions
29
transitclock/src/test/java/org/transitclock/core/AvlProcessorTest.java
This file contains hidden or 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,29 @@ | ||
| package org.transitclock.core; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| public class AvlProcessorTest { | ||
|
|
||
| @Test | ||
| public void getInstance_returnsSingleton() { | ||
| assertThat(AvlProcessor.getInstance()) | ||
| .isNotNull() | ||
| .isSameAs(AvlProcessor.getInstance()); | ||
| } | ||
|
|
||
| @Test | ||
| public void lastAvlReportTime_isZeroBeforeAnyReportIsProcessed() { | ||
| // The singleton may have been touched by other tests that preceded this | ||
| // one, so we don't assume a "clean" state — we only assert the | ||
| // contract: if no regular report has been stored yet, the method | ||
| // returns 0, otherwise it returns the epoch time of the stored report. | ||
| AvlProcessor p = AvlProcessor.getInstance(); | ||
| if (p.getLastAvlReport() == null) { | ||
| assertThat(p.lastAvlReportTime()).isEqualTo(0L); | ||
| } else { | ||
| assertThat(p.lastAvlReportTime()).isEqualTo(p.getLastAvlReport().getTime()); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.