Add initial unit test coverage for Metrics event persistence by firasrg · Pull Request #1439 · Together-Java/TJ-Bot
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)