From d122d31cc2f3e8cf120b1a80d8c83af677a31066 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B3bert=20Papp=20=28TWiStErRob=29?= Date: Fri, 29 Dec 2023 13:10:14 +0000 Subject: [PATCH 1/8] Migrate to JUnit 5 --- common/diff/build.gradle | 3 ++- common/test-helpers/build.gradle | 13 ++++++------- gradle/libs.versions.toml | 5 +++++ web/status-history/build.gradle | 2 +- .../controller/IndexControllerIntegrationTest.kt | 14 +++++++------- .../DatastoreStatusHistoryRepositoryUnitTest.kt | 4 ++-- .../travel/statushistory/test/HtmlValidator.kt | 4 ++-- .../travel/statushistory/viewmodel/GwenChange.kt | 2 +- .../statushistory/viewmodel/LineColorUnitTest.kt | 16 +++++++--------- .../viewmodel/ResultChangeUnitTest.kt | 4 ++-- .../viewmodel/ResultChangeUnitTest_Delays.kt | 6 +++--- .../ResultChangeUnitTest_Descriptions.kt | 6 +++--- .../viewmodel/ResultChangeUnitTest_Errors.kt | 4 ++-- .../statushistory/viewmodel/ResultUnitTest.kt | 4 ++-- 14 files changed, 45 insertions(+), 42 deletions(-) diff --git a/common/diff/build.gradle b/common/diff/build.gradle index 97535fef..50d4d00f 100644 --- a/common/diff/build.gradle +++ b/common/diff/build.gradle @@ -5,5 +5,6 @@ plugins { } dependencies { - testImplementation(libs.test.junit4) + testCompileOnly(libs.test.junit4) + testRuntimeOnly(libs.test.junit5.vintage) } diff --git a/common/test-helpers/build.gradle b/common/test-helpers/build.gradle index ee6c0816..936cdc3a 100644 --- a/common/test-helpers/build.gradle +++ b/common/test-helpers/build.gradle @@ -4,15 +4,14 @@ plugins { } dependencies { - api(libs.test.junit4) { - // we don't need this because we use org.hamcrest v2 - exclude module: "hamcrest-core" - } - api(libs.test.hamcrest){ - // this needs to be excluded because otherwise the hamcrest-core exclusion above doesn't work - exclude module: "junit" + api(libs.test.junit5) + runtimeOnly(libs.test.junit5.launcher) + api(libs.test.hamcrest) { + // Avoid leaking JUnit 4 into tests. + exclude group: "junit", module: "junit" } api(libs.test.mockito) + api(libs.test.mockito.junit5) api(libs.test.gwen) implementation(projects.common.logConsole) } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index d5cff408..a613a4df 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -41,6 +41,7 @@ micronaut-handlebars = "5.0.1" snakeyaml = "2.2" junit4 = "4.13.2" +junit5 = "5.10.1" mockito = "5.8.0" mockative = "2.0.1" # Use this instead of 1.3 @@ -87,9 +88,13 @@ gms-places = { module = "com.google.android.libraries.places:places", version.re gms-maps = { module = "com.google.android.gms:play-services-maps", version.ref = "google-maps" } test-junit4 = { module = "junit:junit", version.ref = "junit4" } +test-junit5 = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit5" } +test-junit5-vintage = { module = "org.junit.vintage:junit-vintage-engine", version.ref = "junit5" } +test-junit5-launcher = { module = "org.junit.platform:junit-platform-launcher" } test-mockative = { module = "io.mockative:mockative", version.ref = "mockative" } test-mockative-processor = { module = "io.mockative:mockative-processor", version.ref = "mockative" } test-mockito = { module = "org.mockito:mockito-core", version.ref = "mockito" } +test-mockito-junit5 = { module = "org.mockito:mockito-junit-jupiter", version.ref = "mockito" } test-hamcrest = { module = "org.hamcrest:hamcrest-junit", version.ref = "hamcrest" } test-gwen = { module = "com.shazam:gwen", version.ref = "gwen" } jtidy = { module = "com.github.jtidy:jtidy", version.ref = "jtidy" } diff --git a/web/status-history/build.gradle b/web/status-history/build.gradle index 1183bf5b..93d4b1e3 100644 --- a/web/status-history/build.gradle +++ b/web/status-history/build.gradle @@ -70,7 +70,7 @@ application { micronaut { version = libs.versions.micronaut.asProvider().get() runtime("jetty") - testRuntime("junit4") + testRuntime("junit5") processing { incremental(true) } diff --git a/web/status-history/src/test/java/net/twisterrob/travel/statushistory/controller/IndexControllerIntegrationTest.kt b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/controller/IndexControllerIntegrationTest.kt index 225998e3..74af6b34 100644 --- a/web/status-history/src/test/java/net/twisterrob/travel/statushistory/controller/IndexControllerIntegrationTest.kt +++ b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/controller/IndexControllerIntegrationTest.kt @@ -7,11 +7,11 @@ import io.micronaut.runtime.server.EmbeddedServer import net.twisterrob.travel.statushistory.test.HtmlValidator.assertValidHtml import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.containsString -import org.junit.AfterClass -import org.junit.Assert.assertArrayEquals -import org.junit.Assert.assertNotNull -import org.junit.BeforeClass -import org.junit.Test +import org.junit.jupiter.api.AfterAll +import org.junit.jupiter.api.Assertions.assertArrayEquals +import org.junit.jupiter.api.Assertions.assertNotNull +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Test // TODO use @MicronautTest in JUnit 5. class IndexControllerIntegrationTest { @@ -42,7 +42,7 @@ class IndexControllerIntegrationTest { private lateinit var client: HttpClient @JvmStatic - @BeforeClass fun setupServer() { + @BeforeAll fun setupServer() { server = ApplicationContext .run(EmbeddedServer::class.java) client = server @@ -51,7 +51,7 @@ class IndexControllerIntegrationTest { } @JvmStatic - @AfterClass fun stopServer() { + @AfterAll fun stopServer() { if (this::server.isInitialized) { server.stop() } diff --git a/web/status-history/src/test/java/net/twisterrob/travel/statushistory/infrastructure/DatastoreStatusHistoryRepositoryUnitTest.kt b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/infrastructure/DatastoreStatusHistoryRepositoryUnitTest.kt index c3c60c35..21cf7a00 100644 --- a/web/status-history/src/test/java/net/twisterrob/travel/statushistory/infrastructure/DatastoreStatusHistoryRepositoryUnitTest.kt +++ b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/infrastructure/DatastoreStatusHistoryRepositoryUnitTest.kt @@ -22,8 +22,8 @@ import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.hasEntry import org.hamcrest.Matchers.hasSize import org.hamcrest.Matchers.instanceOf -import org.junit.Assert.assertEquals -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test import org.mockito.ArgumentCaptor import org.mockito.Mockito.any import org.mockito.Mockito.doAnswer diff --git a/web/status-history/src/test/java/net/twisterrob/travel/statushistory/test/HtmlValidator.kt b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/test/HtmlValidator.kt index ce691d1c..ac682f09 100644 --- a/web/status-history/src/test/java/net/twisterrob/travel/statushistory/test/HtmlValidator.kt +++ b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/test/HtmlValidator.kt @@ -1,6 +1,6 @@ package net.twisterrob.travel.statushistory.test -import org.junit.Assert.assertEquals +import org.junit.jupiter.api.Assertions.assertEquals import org.w3c.tidy.Tidy import org.w3c.tidy.TidyMessage import java.io.Writer @@ -15,6 +15,6 @@ object HtmlValidator { val messages: MutableList = mutableListOf() tidy.setMessageListener(messages::add) tidy.parse(html.reader(), null as Writer?) - assertEquals("${html}\n${messages}", 0, messages.size) + assertEquals(0, messages.size, "${html}\n${messages}") } } diff --git a/web/status-history/src/test/java/net/twisterrob/travel/statushistory/viewmodel/GwenChange.kt b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/viewmodel/GwenChange.kt index b7dda979..b91a8587 100644 --- a/web/status-history/src/test/java/net/twisterrob/travel/statushistory/viewmodel/GwenChange.kt +++ b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/viewmodel/GwenChange.kt @@ -11,7 +11,7 @@ import org.hamcrest.Matchers.equalTo import org.hamcrest.Matchers.hasEntry import org.hamcrest.Matchers.hasKey import org.hamcrest.Matchers.not -import org.junit.Assert.assertEquals +import org.junit.jupiter.api.Assertions.assertEquals internal class GwenChange : Actor, Asserter { diff --git a/web/status-history/src/test/java/net/twisterrob/travel/statushistory/viewmodel/LineColorUnitTest.kt b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/viewmodel/LineColorUnitTest.kt index 8f873299..0bacd728 100644 --- a/web/status-history/src/test/java/net/twisterrob/travel/statushistory/viewmodel/LineColorUnitTest.kt +++ b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/viewmodel/LineColorUnitTest.kt @@ -7,23 +7,21 @@ import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.anyOf import org.hamcrest.Matchers.equalTo import org.hamcrest.Matchers.not -import org.junit.Assert.assertEquals -import org.junit.Assert.assertThrows -import org.junit.Rule -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows +import org.junit.jupiter.api.extension.ExtendWith import org.mockito.Mock import org.mockito.Mockito.atLeastOnce import org.mockito.Mockito.verify import org.mockito.Mockito.verifyNoInteractions import org.mockito.Mockito.verifyNoMoreInteractions import org.mockito.Mockito.`when` -import org.mockito.junit.MockitoJUnit -import org.mockito.junit.MockitoRule +import org.mockito.junit.jupiter.MockitoExtension +@ExtendWith(MockitoExtension::class) class LineColorUnitTest { - @get:Rule val mockito: MockitoRule = MockitoJUnit.rule() - @Mock lateinit var colors: LineColors @Test fun testShortColor() { @@ -67,7 +65,7 @@ class LineColorUnitTest { iterator.next() } - assertThrows(NoSuchElementException::class.java) { iterator.next() } + assertThrows { iterator.next() } } @Test fun testAllColorsConsistentProperties() { diff --git a/web/status-history/src/test/java/net/twisterrob/travel/statushistory/viewmodel/ResultChangeUnitTest.kt b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/viewmodel/ResultChangeUnitTest.kt index a9ba3a82..cb43b1f4 100644 --- a/web/status-history/src/test/java/net/twisterrob/travel/statushistory/viewmodel/ResultChangeUnitTest.kt +++ b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/viewmodel/ResultChangeUnitTest.kt @@ -7,8 +7,8 @@ import org.hamcrest.Matchers.emptyOrNullString import org.hamcrest.Matchers.`is` import org.hamcrest.Matchers.not import org.hamcrest.Matchers.notNullValue -import org.junit.Assert.assertEquals -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test import org.mockito.Mockito.mock class ResultChangeUnitTest { diff --git a/web/status-history/src/test/java/net/twisterrob/travel/statushistory/viewmodel/ResultChangeUnitTest_Delays.kt b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/viewmodel/ResultChangeUnitTest_Delays.kt index cc40ccd0..04851407 100644 --- a/web/status-history/src/test/java/net/twisterrob/travel/statushistory/viewmodel/ResultChangeUnitTest_Delays.kt +++ b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/viewmodel/ResultChangeUnitTest_Delays.kt @@ -6,8 +6,8 @@ import com.shazam.gwen.Gwen.`when` import net.twisterrob.blt.io.feeds.trackernet.model.DelayType import net.twisterrob.blt.model.Line import net.twisterrob.travel.statushistory.viewmodel.ResultChange.StatusChange -import org.junit.Before -import org.junit.Test +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test class ResultChangeUnitTest_Delays { @@ -15,7 +15,7 @@ class ResultChangeUnitTest_Delays { private lateinit var status2: GwenStatus private lateinit var change: GwenChange - @Before fun setUp() { + @BeforeEach fun setUp() { status1 = GwenStatus() status2 = GwenStatus() change = GwenChange() diff --git a/web/status-history/src/test/java/net/twisterrob/travel/statushistory/viewmodel/ResultChangeUnitTest_Descriptions.kt b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/viewmodel/ResultChangeUnitTest_Descriptions.kt index 67460a8f..45590ecc 100644 --- a/web/status-history/src/test/java/net/twisterrob/travel/statushistory/viewmodel/ResultChangeUnitTest_Descriptions.kt +++ b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/viewmodel/ResultChangeUnitTest_Descriptions.kt @@ -6,8 +6,8 @@ import com.shazam.gwen.Gwen.`when` import net.twisterrob.blt.io.feeds.trackernet.model.LineStatus.BranchStatus import net.twisterrob.blt.model.Line import net.twisterrob.travel.statushistory.viewmodel.ResultChange.StatusChange -import org.junit.Before -import org.junit.Test +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test private val MISSING_DESCRIPTION: String? = null @@ -17,7 +17,7 @@ class ResultChangeUnitTest_Descriptions { private lateinit var status2: GwenStatus private lateinit var change: GwenChange - @Before fun setUp() { + @BeforeEach fun setUp() { status1 = GwenStatus() status2 = GwenStatus() change = GwenChange() diff --git a/web/status-history/src/test/java/net/twisterrob/travel/statushistory/viewmodel/ResultChangeUnitTest_Errors.kt b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/viewmodel/ResultChangeUnitTest_Errors.kt index 5aada2ae..70dea805 100644 --- a/web/status-history/src/test/java/net/twisterrob/travel/statushistory/viewmodel/ResultChangeUnitTest_Errors.kt +++ b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/viewmodel/ResultChangeUnitTest_Errors.kt @@ -5,8 +5,8 @@ import net.twisterrob.travel.statushistory.viewmodel.ResultChange.ErrorChange import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.anEmptyMap import org.hamcrest.Matchers.`is` -import org.junit.Assert.assertEquals -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test import org.mockito.Mockito.mock import java.util.Date diff --git a/web/status-history/src/test/java/net/twisterrob/travel/statushistory/viewmodel/ResultUnitTest.kt b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/viewmodel/ResultUnitTest.kt index 4ce46b03..cc3dda8c 100644 --- a/web/status-history/src/test/java/net/twisterrob/travel/statushistory/viewmodel/ResultUnitTest.kt +++ b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/viewmodel/ResultUnitTest.kt @@ -1,8 +1,8 @@ package net.twisterrob.travel.statushistory.viewmodel import net.twisterrob.blt.io.feeds.trackernet.LineStatusFeed -import org.junit.Assert.assertEquals -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test import org.mockito.Mockito.mock import java.util.Date From 0be152d5c68f778f48d087a76b27d1ef61dca28e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B3bert=20Papp=20=28TWiStErRob=29?= Date: Fri, 29 Dec 2023 13:12:21 +0000 Subject: [PATCH 2/8] Migrate to JUnit 5 --- .../blt/io/feeds/trackernet/LineStatusFeedHandlerTest.java | 2 +- .../src/test/java/net/twisterrob/blt/model/LineTest.java | 6 ++++-- .../src/main/groovy/net.twisterrob.blt.convention.gradle | 4 ++++ 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/common/feed/trackernet/src/test/java/net/twisterrob/blt/io/feeds/trackernet/LineStatusFeedHandlerTest.java b/common/feed/trackernet/src/test/java/net/twisterrob/blt/io/feeds/trackernet/LineStatusFeedHandlerTest.java index 5055beac..cebc91b8 100644 --- a/common/feed/trackernet/src/test/java/net/twisterrob/blt/io/feeds/trackernet/LineStatusFeedHandlerTest.java +++ b/common/feed/trackernet/src/test/java/net/twisterrob/blt/io/feeds/trackernet/LineStatusFeedHandlerTest.java @@ -3,7 +3,7 @@ import java.io.IOException; import java.io.InputStream; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.xml.sax.SAXException; import static org.hamcrest.MatcherAssert.assertThat; diff --git a/common/model/src/test/java/net/twisterrob/blt/model/LineTest.java b/common/model/src/test/java/net/twisterrob/blt/model/LineTest.java index 0864de01..81c17b2b 100644 --- a/common/model/src/test/java/net/twisterrob/blt/model/LineTest.java +++ b/common/model/src/test/java/net/twisterrob/blt/model/LineTest.java @@ -3,9 +3,11 @@ import java.util.*; import java.util.Map.Entry; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertSame; public class LineTest { @Test diff --git a/gradle/plugins/src/main/groovy/net.twisterrob.blt.convention.gradle b/gradle/plugins/src/main/groovy/net.twisterrob.blt.convention.gradle index a1cbf12c..956b31e8 100644 --- a/gradle/plugins/src/main/groovy/net.twisterrob.blt.convention.gradle +++ b/gradle/plugins/src/main/groovy/net.twisterrob.blt.convention.gradle @@ -16,6 +16,10 @@ pluginManager.withPlugin("org.gradle.java-base") { } } +pluginManager.withPlugin("org.gradle.java") { + testing.suites.named("test", org.gradle.api.plugins.jvm.JvmTestSuite).configure { useJUnitJupiter() } +} + pluginManager.withPlugin("com.android.base") { android { compileOptions { From 84fd2f7dcb343a2ed4c07f93b2c760923daea646 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B3bert=20Papp=20=28TWiStErRob=29?= Date: Fri, 29 Dec 2023 13:25:53 +0000 Subject: [PATCH 3/8] Improve test --- .../net/twisterrob/blt/model/LineTest.java | 59 +++++++++++-------- 1 file changed, 36 insertions(+), 23 deletions(-) diff --git a/common/model/src/test/java/net/twisterrob/blt/model/LineTest.java b/common/model/src/test/java/net/twisterrob/blt/model/LineTest.java index 81c17b2b..683cfd42 100644 --- a/common/model/src/test/java/net/twisterrob/blt/model/LineTest.java +++ b/common/model/src/test/java/net/twisterrob/blt/model/LineTest.java @@ -1,127 +1,140 @@ package net.twisterrob.blt.model; import java.util.*; -import java.util.Map.Entry; + +import static java.util.Collections.emptyList; +import static java.util.Collections.emptyMap; +import static java.util.Collections.emptySet; import org.junit.jupiter.api.Test; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.anEmptyMap; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertSame; public class LineTest { + @Test public void testFixMapList() { Map> map = new TreeMap<>(); - assertEquals(0, map.size()); - Map> newMap = Line.fixMap(map, Collections.emptyList()); + assertThat(map, anEmptyMap()); + Map> newMap = Line.fixMap(map, emptyList()); assertSame(map, newMap); assertEquals(Line.values().length, map.size()); - for (Entry> element : map.entrySet()) { + for (Map.Entry> element : map.entrySet()) { assertNotNull(element.getKey()); List value = element.getValue(); // force casting assertNotNull(value); - assertSame(Collections.emptyList(), element.getValue()); + assertSame(emptyList(), element.getValue()); } } + @Test public void testFixMapListInstance() { Map> map = new TreeMap<>(); LinkedList empty = new LinkedList<>(); - assertEquals(0, map.size()); + assertThat(map, anEmptyMap()); Map> newMap = Line.fixMap(map, empty); assertSame(map, newMap); assertEquals(Line.values().length, map.size()); - for (Entry> element : map.entrySet()) { + for (Map.Entry> element : map.entrySet()) { assertNotNull(element.getKey()); List value = element.getValue(); // force casting assertNotNull(value); assertSame(empty, element.getValue()); } } + @Test public void testFixMapLinkedList() { HashMap> map = new HashMap<>(); LinkedList empty = new LinkedList<>(); - assertEquals(0, map.size()); + assertThat(map, anEmptyMap()); HashMap> newMap = Line.fixMap(map, empty); assertSame(map, newMap); assertEquals(Line.values().length, map.size()); - for (Entry> element : map.entrySet()) { + for (Map.Entry> element : map.entrySet()) { assertNotNull(element.getKey()); LinkedList value = element.getValue(); // force casting assertNotNull(value); assertSame(empty, value); } } + @Test public void testFixMapSet() { Map> map = new TreeMap<>(); - Set empty = Collections.emptySet(); - assertEquals(0, map.size()); + Set empty = emptySet(); + assertThat(map, anEmptyMap()); Map> newMap = Line.fixMap(map, empty); assertSame(map, newMap); assertEquals(Line.values().length, map.size()); - for (Entry> element : map.entrySet()) { + for (Map.Entry> element : map.entrySet()) { assertNotNull(element.getKey()); Set value = element.getValue(); // force casting assertNotNull(value); assertSame(empty, element.getValue()); } } + @Test public void testFixMapSetInstance() { Map> map = new TreeMap<>(); - Set empty = Collections.emptySet(); - assertEquals(0, map.size()); + Set empty = emptySet(); + assertThat(map, anEmptyMap()); Map> newMap = Line.fixMap(map, empty); assertSame(map, newMap); assertEquals(Line.values().length, map.size()); - for (Entry> element : map.entrySet()) { + for (Map.Entry> element : map.entrySet()) { assertNotNull(element.getKey()); Set value = element.getValue(); // force casting assertNotNull(value); assertSame(empty, element.getValue()); } } + @Test public void testFixMapHashSet() { HashMap> map = new HashMap<>(); HashSet empty = new HashSet<>(); - assertEquals(0, map.size()); + assertThat(map, anEmptyMap()); HashMap> newMap = Line.fixMap(map, empty); assertSame(map, newMap); assertEquals(Line.values().length, map.size()); - for (Entry> element : map.entrySet()) { + for (Map.Entry> element : map.entrySet()) { assertNotNull(element.getKey()); HashSet value = element.getValue(); // force casting assertNotNull(value); assertSame(empty, value); } } + @Test public void testFixMapMap() { Map> map = new TreeMap<>(); - assertEquals(0, map.size()); - Map> newMap = Line.fixMap(map, Collections.emptyMap()); + assertThat(map, anEmptyMap()); + Map> newMap = Line.fixMap(map, emptyMap()); assertSame(map, newMap); assertEquals(Line.values().length, map.size()); - for (Entry> element : map.entrySet()) { + for (Map.Entry> element : map.entrySet()) { assertNotNull(element.getKey()); Map value = element.getValue(); // force casting assertNotNull(value); - assertSame(Collections.emptyMap(), element.getValue()); + assertSame(emptyMap(), element.getValue()); } } + @Test public void testFixMapHashMap() { HashMap> map = new HashMap<>(); HashMap empty = new HashMap<>(); - assertEquals(0, map.size()); + assertThat(map, anEmptyMap()); HashMap> newMap = Line.fixMap(map, empty); assertSame(map, newMap); assertEquals(Line.values().length, map.size()); - for (Entry> element : map.entrySet()) { + for (Map.Entry> element : map.entrySet()) { assertNotNull(element.getKey()); HashMap value = element.getValue(); // force casting assertNotNull(value); From 22834d8487b5037379eb1d0dfdf2f6bc60aa504b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B3bert=20Papp=20=28TWiStErRob=29?= Date: Mon, 25 Dec 2023 15:55:42 +0000 Subject: [PATCH 4/8] Fix double-logger issue in unit tests --- web/status-history/build.gradle | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/web/status-history/build.gradle b/web/status-history/build.gradle index 93d4b1e3..f996fa97 100644 --- a/web/status-history/build.gradle +++ b/web/status-history/build.gradle @@ -33,7 +33,9 @@ dependencies { implementation(libs.slf4j.api) runtimeOnly(libs.log4j.slf4j2) - testImplementation(projects.common.testHelpers) + testImplementation(projects.common.testHelpers) { + exclude(group: "org.slf4j", module: "slf4j-simple") + } testImplementation(libs.micronaut.httpClient) testImplementation(libs.jtidy) } From ed5fa4ca153082417a8fba557b23043629c4e5a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B3bert=20Papp=20=28TWiStErRob=29?= Date: Mon, 25 Dec 2023 15:55:56 +0000 Subject: [PATCH 5/8] Log outgoing requests --- web/status-history/src/main/resources/log4j2.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/web/status-history/src/main/resources/log4j2.xml b/web/status-history/src/main/resources/log4j2.xml index f0e9fc35..73c74c18 100644 --- a/web/status-history/src/main/resources/log4j2.xml +++ b/web/status-history/src/main/resources/log4j2.xml @@ -36,6 +36,7 @@ + From e1994b7149f4f88bc064a9837fd52d079953451c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B3bert=20Papp=20=28TWiStErRob=29?= Date: Fri, 29 Dec 2023 13:36:19 +0000 Subject: [PATCH 6/8] Use @MicronautTest --- .../IndexControllerIntegrationTest.kt | 38 ++++--------------- 1 file changed, 8 insertions(+), 30 deletions(-) diff --git a/web/status-history/src/test/java/net/twisterrob/travel/statushistory/controller/IndexControllerIntegrationTest.kt b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/controller/IndexControllerIntegrationTest.kt index 74af6b34..f23a7b6b 100644 --- a/web/status-history/src/test/java/net/twisterrob/travel/statushistory/controller/IndexControllerIntegrationTest.kt +++ b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/controller/IndexControllerIntegrationTest.kt @@ -1,21 +1,24 @@ package net.twisterrob.travel.statushistory.controller -import io.micronaut.context.ApplicationContext import io.micronaut.http.HttpRequest import io.micronaut.http.client.HttpClient -import io.micronaut.runtime.server.EmbeddedServer +import io.micronaut.http.client.annotation.Client +import io.micronaut.test.extensions.junit5.annotation.MicronautTest +import jakarta.inject.Inject import net.twisterrob.travel.statushistory.test.HtmlValidator.assertValidHtml import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.containsString -import org.junit.jupiter.api.AfterAll import org.junit.jupiter.api.Assertions.assertArrayEquals import org.junit.jupiter.api.Assertions.assertNotNull -import org.junit.jupiter.api.BeforeAll import org.junit.jupiter.api.Test -// TODO use @MicronautTest in JUnit 5. +@MicronautTest class IndexControllerIntegrationTest { + @Inject + @Client("/") + lateinit var client: HttpClient + @Test fun testIndex() { val request: HttpRequest = HttpRequest.GET("/") @@ -35,29 +38,4 @@ class IndexControllerIntegrationTest { val expected = IndexController::class.java.getResourceAsStream("/public/favicon.ico")?.use { it.readBytes() } assertArrayEquals(expected, body) } - - companion object { - - private lateinit var server: EmbeddedServer - private lateinit var client: HttpClient - - @JvmStatic - @BeforeAll fun setupServer() { - server = ApplicationContext - .run(EmbeddedServer::class.java) - client = server - .applicationContext - .createBean(HttpClient::class.java, server.url) - } - - @JvmStatic - @AfterAll fun stopServer() { - if (this::server.isInitialized) { - server.stop() - } - if (this::client.isInitialized) { - client.stop() - } - } - } } From 7d442defcc07a5e4ea2b3aebd25faaf72c9649ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B3bert=20Papp=20=28TWiStErRob=29?= Date: Fri, 29 Dec 2023 16:14:53 +0000 Subject: [PATCH 7/8] Test controllers --- .../IndexControllerIntegrationTest.kt | 13 +-- ...eStatusHistoryControllerIntegrationTest.kt | 56 ++++++++++++ .../RefreshFeedControllerIntegrationTest.kt | 88 +++++++++++++++++++ ...atastoreStatusHistoryRepositoryUnitTest.kt | 72 ++------------- .../test/BlockingHttpClientFactory.kt | 24 +++++ .../travel/statushistory/test/Fixtures.kt | 68 ++++++++++++++ 6 files changed, 249 insertions(+), 72 deletions(-) create mode 100644 web/status-history/src/test/java/net/twisterrob/travel/statushistory/controller/LineStatusHistoryControllerIntegrationTest.kt create mode 100644 web/status-history/src/test/java/net/twisterrob/travel/statushistory/controller/RefreshFeedControllerIntegrationTest.kt create mode 100644 web/status-history/src/test/java/net/twisterrob/travel/statushistory/test/BlockingHttpClientFactory.kt create mode 100644 web/status-history/src/test/java/net/twisterrob/travel/statushistory/test/Fixtures.kt diff --git a/web/status-history/src/test/java/net/twisterrob/travel/statushistory/controller/IndexControllerIntegrationTest.kt b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/controller/IndexControllerIntegrationTest.kt index f23a7b6b..810827fd 100644 --- a/web/status-history/src/test/java/net/twisterrob/travel/statushistory/controller/IndexControllerIntegrationTest.kt +++ b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/controller/IndexControllerIntegrationTest.kt @@ -1,8 +1,7 @@ package net.twisterrob.travel.statushistory.controller import io.micronaut.http.HttpRequest -import io.micronaut.http.client.HttpClient -import io.micronaut.http.client.annotation.Client +import io.micronaut.http.client.BlockingHttpClient import io.micronaut.test.extensions.junit5.annotation.MicronautTest import jakarta.inject.Inject import net.twisterrob.travel.statushistory.test.HtmlValidator.assertValidHtml @@ -12,17 +11,19 @@ import org.junit.jupiter.api.Assertions.assertArrayEquals import org.junit.jupiter.api.Assertions.assertNotNull import org.junit.jupiter.api.Test +/** + * @see IndexController + */ @MicronautTest class IndexControllerIntegrationTest { @Inject - @Client("/") - lateinit var client: HttpClient + lateinit var client: BlockingHttpClient @Test fun testIndex() { val request: HttpRequest = HttpRequest.GET("/") - val body = client.toBlocking().retrieve(request) + val body = client.retrieve(request) assertNotNull(body) assertThat(body, containsString("Better London Travel")) @@ -32,7 +33,7 @@ class IndexControllerIntegrationTest { @Test fun testFavicon() { val request: HttpRequest = HttpRequest.GET("/favicon.ico") - val body = client.toBlocking().retrieve(request, ByteArray::class.java) + val body = client.retrieve(request, ByteArray::class.java) assertNotNull(body) val expected = IndexController::class.java.getResourceAsStream("/public/favicon.ico")?.use { it.readBytes() } diff --git a/web/status-history/src/test/java/net/twisterrob/travel/statushistory/controller/LineStatusHistoryControllerIntegrationTest.kt b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/controller/LineStatusHistoryControllerIntegrationTest.kt new file mode 100644 index 00000000..a2a57adf --- /dev/null +++ b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/controller/LineStatusHistoryControllerIntegrationTest.kt @@ -0,0 +1,56 @@ +package net.twisterrob.travel.statushistory.controller + +import io.micronaut.http.HttpRequest +import io.micronaut.http.client.BlockingHttpClient +import io.micronaut.test.annotation.MockBean +import io.micronaut.test.extensions.junit5.annotation.MicronautTest +import jakarta.inject.Inject +import net.twisterrob.blt.data.StaticData +import net.twisterrob.blt.model.LineColors +import net.twisterrob.travel.domain.london.status.api.HistoryUseCase +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.endsWith +import org.hamcrest.Matchers.startsWith +import org.junit.jupiter.api.Assertions.assertNotNull +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.mockito.Mockito.mock +import org.mockito.Mockito.`when` + +/** + * @see LineStatusHistoryController + */ +@MicronautTest +class LineStatusHistoryControllerIntegrationTest { + + @Inject + lateinit var client: BlockingHttpClient + + @MockBean(HistoryUseCase::class) + fun historyUseCase(): HistoryUseCase = mock() + + @Inject + lateinit var historyUseCase: HistoryUseCase + + @MockBean(StaticData::class) + fun staticData(): StaticData = mock() + + @Inject + lateinit var staticData: StaticData + + private val lineColors: LineColors = mock() + + @BeforeEach fun setUp() { + `when`(staticData.lineColors).thenReturn(lineColors) + } + + @Test fun testEmptyRequest() { + val request: HttpRequest = HttpRequest.GET("/LineStatusHistory") + + val body = client.retrieve(request) + + assertNotNull(body) + assertThat(body, startsWith("\n")) + assertThat(body, endsWith("\n")) + } +} diff --git a/web/status-history/src/test/java/net/twisterrob/travel/statushistory/controller/RefreshFeedControllerIntegrationTest.kt b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/controller/RefreshFeedControllerIntegrationTest.kt new file mode 100644 index 00000000..79a6ab0d --- /dev/null +++ b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/controller/RefreshFeedControllerIntegrationTest.kt @@ -0,0 +1,88 @@ +package net.twisterrob.travel.statushistory.controller + +import io.micronaut.http.HttpRequest +import io.micronaut.http.HttpResponse +import io.micronaut.http.HttpStatus +import io.micronaut.http.client.BlockingHttpClient +import io.micronaut.test.annotation.MockBean +import io.micronaut.test.extensions.junit5.annotation.MicronautTest +import jakarta.inject.Inject +import net.twisterrob.travel.domain.london.status.Feed +import net.twisterrob.travel.domain.london.status.api.RefreshResult +import net.twisterrob.travel.domain.london.status.api.RefreshUseCase +import net.twisterrob.travel.statushistory.test.FailedStatusItem +import net.twisterrob.travel.statushistory.test.SuccessfulStatusItem +import net.twisterrob.travel.statushistory.test.assertErrorStatus +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test +import org.mockito.Mockito.mock +import org.mockito.Mockito.`when` + +/** + * @see RefreshFeedController + */ +@MicronautTest +class RefreshFeedControllerIntegrationTest { + + @Inject + lateinit var client: BlockingHttpClient + + @MockBean(RefreshUseCase::class) + fun refreshUseCase(): RefreshUseCase = mock() + + @Inject + lateinit var refreshUseCase: RefreshUseCase + + @Test fun testNoFeedIsInvalidRequest() { + val request: HttpRequest = HttpRequest.GET("/refresh") + + client.assertErrorStatus(HttpStatus.BAD_REQUEST, request) + } + + @Test fun testEmptyFeedIsInvalidRequest() { + val request: HttpRequest = HttpRequest.GET("/refresh?feed=") + + client.assertErrorStatus(HttpStatus.BAD_REQUEST, request) + } + + @Test fun testRefresh() { + val result = RefreshResult.Refreshed(SuccessfulStatusItem(), SuccessfulStatusItem()) + `when`(refreshUseCase.refreshLatest(Feed.TubeDepartureBoardsLineStatus)).thenReturn(result) + val request: HttpRequest = HttpRequest.GET("/refresh?feed=TubeDepartureBoardsLineStatus") + + val response: HttpResponse = client.exchange(request) + + assertEquals(HttpStatus.ACCEPTED, response.status) + } + + @Test fun testFresh() { + val result = RefreshResult.Created(SuccessfulStatusItem()) + `when`(refreshUseCase.refreshLatest(Feed.TubeDepartureBoardsLineStatus)).thenReturn(result) + val request: HttpRequest = HttpRequest.GET("/refresh?feed=TubeDepartureBoardsLineStatus") + + val response: HttpResponse = client.exchange(request, String::class.java) + + assertEquals(HttpStatus.CREATED, response.status) + assertEquals(result.current.retrievedDate.toString(), response.body()) + } + + @Test fun testNoChangeInData() { + val result = RefreshResult.NoChange(SuccessfulStatusItem(), SuccessfulStatusItem()) + `when`(refreshUseCase.refreshLatest(Feed.TubeDepartureBoardsLineStatus)).thenReturn(result) + val request: HttpRequest = HttpRequest.GET("/refresh?feed=TubeDepartureBoardsLineStatus") + + val response: HttpResponse = client.exchange(request) + + assertEquals(HttpStatus.NO_CONTENT, response.status) + } + + @Test fun testNoChangeInError() { + val result = RefreshResult.NoChange(FailedStatusItem(), FailedStatusItem()) + `when`(refreshUseCase.refreshLatest(Feed.TubeDepartureBoardsLineStatus)).thenReturn(result) + val request: HttpRequest = HttpRequest.GET("/refresh?feed=TubeDepartureBoardsLineStatus") + + val response: HttpResponse = client.exchange(request) + + assertEquals(HttpStatus.NON_AUTHORITATIVE_INFORMATION, response.status) + } +} diff --git a/web/status-history/src/test/java/net/twisterrob/travel/statushistory/infrastructure/DatastoreStatusHistoryRepositoryUnitTest.kt b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/infrastructure/DatastoreStatusHistoryRepositoryUnitTest.kt index 21cf7a00..60e4197f 100644 --- a/web/status-history/src/test/java/net/twisterrob/travel/statushistory/infrastructure/DatastoreStatusHistoryRepositoryUnitTest.kt +++ b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/infrastructure/DatastoreStatusHistoryRepositoryUnitTest.kt @@ -5,19 +5,21 @@ import com.google.cloud.datastore.Datastore import com.google.cloud.datastore.Entity import com.google.cloud.datastore.EntityQuery import com.google.cloud.datastore.FullEntity -import com.google.cloud.datastore.Key import com.google.cloud.datastore.KeyFactory import com.google.cloud.datastore.QueryResults import com.google.cloud.datastore.StringValue import com.google.cloud.datastore.StructuredQuery import com.google.cloud.datastore.TimestampValue import kotlinx.datetime.Instant -import net.twisterrob.travel.domain.london.status.Feed -import net.twisterrob.travel.domain.london.status.Stacktrace -import net.twisterrob.travel.domain.london.status.StatusContent import net.twisterrob.travel.domain.london.status.StatusItem.FailedStatusItem import net.twisterrob.travel.domain.london.status.StatusItem.SuccessfulStatusItem import net.twisterrob.travel.domain.london.status.api.StatusHistoryRepository +import net.twisterrob.travel.statushistory.test.FailedEntity +import net.twisterrob.travel.statushistory.test.FailedStatusItem +import net.twisterrob.travel.statushistory.test.SuccessfulEntity +import net.twisterrob.travel.statushistory.test.SuccessfulStatusItem +import net.twisterrob.travel.statushistory.test.randomFeed +import net.twisterrob.travel.statushistory.test.randomKey import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.hasEntry import org.hamcrest.Matchers.hasSize @@ -33,8 +35,6 @@ import org.mockito.Mockito.times import org.mockito.Mockito.verify import org.mockito.Mockito.`when` import java.util.Collections.emptyIterator -import java.util.UUID -import kotlin.random.Random class DatastoreStatusHistoryRepositoryUnitTest { @@ -169,65 +169,5 @@ class DatastoreStatusHistoryRepositoryUnitTest { assertThat(captor.allValues, hasSize(1)) return captor.allValues.single() } - - @Suppress("TestFunctionName") - private fun SuccessfulEntity(feed: Feed = randomFeed()): Entity = - Entity - .newBuilder(randomKey(feed.name)) - .set("retrievedDate", randomTimestamp()) - .set("content", UUID.randomUUID().toString()) - .build() - - @Suppress("TestFunctionName") - private fun FailedEntity(feed: Feed): Entity = - Entity - .newBuilder(randomKey(feed.name)) - .set("retrievedDate", randomTimestamp()) - .set("error", UUID.randomUUID().toString()) - .build() - - @Suppress("TestFunctionName") - private fun SuccessfulStatusItem(): SuccessfulStatusItem = - SuccessfulStatusItem( - randomFeed(), - StatusContent(UUID.randomUUID().toString()), - randomInstant() - ) - - @Suppress("TestFunctionName") - private fun FailedStatusItem(): FailedStatusItem = - FailedStatusItem( - randomFeed(), - Stacktrace(UUID.randomUUID().toString()), - randomInstant() - ) - - private fun randomFeed(): Feed = - Feed.entries[Random.nextInt(Feed.entries.size)] - - private fun randomKey(kind: String): Key { - val keyFactory = KeyFactory("test-project-id").setKind(kind) - return keyFactory.newKey(UUID.randomUUID().toString()) - } - - private fun randomTimestamp(): Timestamp = - Timestamp.ofTimeSecondsAndNanos( - Random.nextLong(Timestamp.MIN_VALUE.seconds, Timestamp.MAX_VALUE.seconds), - Random.nextInt(Timestamp.MIN_VALUE.nanos, Timestamp.MAX_VALUE.nanos) - ) - - private fun randomInstant(): Instant { - println(Instant.DISTANT_PAST) - println(Instant.DISTANT_FUTURE) - println(Timestamp.MIN_VALUE) - println(Timestamp.MAX_VALUE) - // Note: Have to use the range from Google's Timestamp, because Instant.DISTANT_PAST/DISTANT_FUTURE - // are out of range (-100001-12-31T23:59:59.999999999Z - +100000-01-01T00:00:00Z), - // compared to Timestamp's 0001-01-01T00:00:00Z - 9999-12-31T23:59:59.999999999Z range. - return Instant.fromEpochSeconds( - Random.nextLong(Timestamp.MIN_VALUE.seconds, Timestamp.MAX_VALUE.seconds), - Random.nextInt(Timestamp.MIN_VALUE.nanos, Timestamp.MAX_VALUE.nanos) - ) - } } } diff --git a/web/status-history/src/test/java/net/twisterrob/travel/statushistory/test/BlockingHttpClientFactory.kt b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/test/BlockingHttpClientFactory.kt new file mode 100644 index 00000000..ad71423a --- /dev/null +++ b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/test/BlockingHttpClientFactory.kt @@ -0,0 +1,24 @@ +package net.twisterrob.travel.statushistory.test + +import io.micronaut.context.annotation.Factory +import io.micronaut.context.annotation.Prototype +import io.micronaut.http.HttpRequest +import io.micronaut.http.HttpStatus +import io.micronaut.http.client.BlockingHttpClient +import io.micronaut.http.client.HttpClient +import io.micronaut.http.client.annotation.Client +import io.micronaut.http.client.exceptions.HttpClientResponseException +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.assertThrows + +@Factory +class BlockingHttpClientFactory { + + @Prototype + fun client(@Client("/") client: HttpClient): BlockingHttpClient = client.toBlocking() +} + +fun BlockingHttpClient.assertErrorStatus(status: HttpStatus, request: HttpRequest<*>) { + val ex = assertThrows { retrieve(request) } + assertEquals(status, ex.status) +} diff --git a/web/status-history/src/test/java/net/twisterrob/travel/statushistory/test/Fixtures.kt b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/test/Fixtures.kt new file mode 100644 index 00000000..394aeeea --- /dev/null +++ b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/test/Fixtures.kt @@ -0,0 +1,68 @@ +@file:Suppress("TestFunctionName") + +package net.twisterrob.travel.statushistory.test + +import com.google.cloud.Timestamp +import com.google.cloud.datastore.Entity +import com.google.cloud.datastore.Key +import com.google.cloud.datastore.KeyFactory +import kotlinx.datetime.Instant +import net.twisterrob.travel.domain.london.status.Feed +import net.twisterrob.travel.domain.london.status.Stacktrace +import net.twisterrob.travel.domain.london.status.StatusContent +import net.twisterrob.travel.domain.london.status.StatusItem.FailedStatusItem +import net.twisterrob.travel.domain.london.status.StatusItem.SuccessfulStatusItem +import java.util.UUID +import kotlin.random.Random + +internal fun SuccessfulEntity(feed: Feed = randomFeed()): Entity = + Entity + .newBuilder(randomKey(feed.name)) + .set("retrievedDate", randomTimestamp()) + .set("content", UUID.randomUUID().toString()) + .build() + +internal fun FailedEntity(feed: Feed): Entity = + Entity + .newBuilder(randomKey(feed.name)) + .set("retrievedDate", randomTimestamp()) + .set("error", UUID.randomUUID().toString()) + .build() + +internal fun SuccessfulStatusItem(): SuccessfulStatusItem = + SuccessfulStatusItem( + randomFeed(), + StatusContent(UUID.randomUUID().toString()), + randomInstant() + ) + +internal fun FailedStatusItem(): FailedStatusItem = + FailedStatusItem( + randomFeed(), + Stacktrace(UUID.randomUUID().toString()), + randomInstant() + ) + +internal fun randomFeed(): Feed = + Feed.entries[Random.nextInt(Feed.entries.size)] + +internal fun randomKey(kind: String): Key { + val keyFactory = KeyFactory("test-project-id").setKind(kind) + return keyFactory.newKey(UUID.randomUUID().toString()) +} + +internal fun randomTimestamp(): Timestamp = + Timestamp.ofTimeSecondsAndNanos( + Random.nextLong(Timestamp.MIN_VALUE.seconds, Timestamp.MAX_VALUE.seconds), + Random.nextInt(Timestamp.MIN_VALUE.nanos, Timestamp.MAX_VALUE.nanos) + ) + +internal fun randomInstant(): Instant { + // Note: Have to use the range from Google's Timestamp, because Instant.DISTANT_PAST/DISTANT_FUTURE + // are out of range (-100001-12-31T23:59:59.999999999Z - +100000-01-01T00:00:00Z), + // compared to Timestamp's 0001-01-01T00:00:00Z - 9999-12-31T23:59:59.999999999Z range. + return Instant.fromEpochSeconds( + Random.nextLong(Timestamp.MIN_VALUE.seconds, Timestamp.MAX_VALUE.seconds), + Random.nextInt(Timestamp.MIN_VALUE.nanos, Timestamp.MAX_VALUE.nanos) + ) +} From 75aa6ff66c51d3d3d2023aa07fb4eb6f0c120ee9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B3bert=20Papp=20=28TWiStErRob=29?= Date: Fri, 29 Dec 2023 16:19:45 +0000 Subject: [PATCH 8/8] Simplify injection --- .../LineStatusHistoryControllerIntegrationTest.kt | 10 ++-------- .../controller/RefreshFeedControllerIntegrationTest.kt | 5 +---- 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/web/status-history/src/test/java/net/twisterrob/travel/statushistory/controller/LineStatusHistoryControllerIntegrationTest.kt b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/controller/LineStatusHistoryControllerIntegrationTest.kt index a2a57adf..92560852 100644 --- a/web/status-history/src/test/java/net/twisterrob/travel/statushistory/controller/LineStatusHistoryControllerIntegrationTest.kt +++ b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/controller/LineStatusHistoryControllerIntegrationTest.kt @@ -27,16 +27,10 @@ class LineStatusHistoryControllerIntegrationTest { lateinit var client: BlockingHttpClient @MockBean(HistoryUseCase::class) - fun historyUseCase(): HistoryUseCase = mock() - - @Inject - lateinit var historyUseCase: HistoryUseCase + val historyUseCase: HistoryUseCase = mock() @MockBean(StaticData::class) - fun staticData(): StaticData = mock() - - @Inject - lateinit var staticData: StaticData + val staticData: StaticData = mock() private val lineColors: LineColors = mock() diff --git a/web/status-history/src/test/java/net/twisterrob/travel/statushistory/controller/RefreshFeedControllerIntegrationTest.kt b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/controller/RefreshFeedControllerIntegrationTest.kt index 79a6ab0d..a91dbdbb 100644 --- a/web/status-history/src/test/java/net/twisterrob/travel/statushistory/controller/RefreshFeedControllerIntegrationTest.kt +++ b/web/status-history/src/test/java/net/twisterrob/travel/statushistory/controller/RefreshFeedControllerIntegrationTest.kt @@ -28,10 +28,7 @@ class RefreshFeedControllerIntegrationTest { lateinit var client: BlockingHttpClient @MockBean(RefreshUseCase::class) - fun refreshUseCase(): RefreshUseCase = mock() - - @Inject - lateinit var refreshUseCase: RefreshUseCase + val refreshUseCase: RefreshUseCase = mock() @Test fun testNoFeedIsInvalidRequest() { val request: HttpRequest = HttpRequest.GET("/refresh")