Add initial unit test coverage for Metrics event persistence#1439
Add initial unit test coverage for Metrics event persistence#1439
Conversation
|
| <Logger name="org.togetherjava.tjbot" level="debug"/> | ||
|
|
| implementation 'org.apache.commons:commons-text:1.15.0' | ||
| implementation 'com.apptasticsoftware:rssreader:3.12.0' | ||
|
|
||
| testImplementation 'org.assertj:assertj-core:3.27.7' |
There was a problem hiding this comment.
undo. we want to use raw junit in this project for now as it is easier for beginners and reflects more what they will see in the wild and might already be familiar with from their own projects and university etc
| public ExecutorService getExecutorService() { | ||
| return service; | ||
| } |
There was a problem hiding this comment.
undo, this won't be needed (see other comment why)
| import static org.assertj.core.api.Assertions.within; | ||
| import static org.junit.jupiter.api.Assertions.fail; | ||
|
|
||
| final class MetricsTests { |
There was a problem hiding this comment.
this test is too much in terms of complexity for no tangible benefit.
lets step back and create something simpler that beginners can understand and maintain while providing pretty much the same test-safety in terms of relevant coverage.
- create a package private method in Metrics called
countBlocking. its identical tocountbut it won't use the executor service. so it creates the instant and then callsprocessEventdirectly - in ur unit test this will be the method ur testing against now. without the async stuff the test will simplify a lot
- the test should use a GWT structure (see other examples in the codebase, alternative patterns that share the same idea: 4-phases SEVT or AAA). try to aim for something like this:
@Test
void basicEventCount() {
// GIVEN a test event
String expectedEvent = "test";
Instant expectedHappenedAt = Instant.now();
// WHEN counting the event
metrics.countBlocking(expectedEvent);
// THEN the event was saved in the DB
Foo entry = database...
String actualEvent = entry.event();
Instant actualHappenedAt = entry.happenedAt();
assertEquals(expectedEvent, actualEvent);
assertCloseEnough(expectedHappenedAt, actualHappenedAt);
}
private static void assertCloseEnough(Instant expected, Instant actual) {
... // assert that its within 1min difference
} (double check the correct order of expected vs actual in assert, i never can remember it, lol)



What
Adds the first unit test for the analytics system.
MetricsTeststo verifyMetrics#count(...)persists a metric event;Metricsexecutor service so tests can shut it down cleanly;org.togetherjava.tjbotin test runs;Why
Metricsperforms asynchronous database writes, so having direct test coverage here improves confidence in analytics persistence and makes future refactors safer.Validation
:application:test --tests org.togetherjava.tjbot.features.MetricsTests