From: David ‘Bombe’ Roden Date: Thu, 2 Feb 2017 20:57:26 +0000 (+0100) Subject: Create injector in test base class X-Git-Tag: 0.9.7^2~309 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=05cb6903f37f8166eab0ca2244b2cac7f06a6a52;p=Sone.git Create injector in test base class --- diff --git a/src/test/java/net/pterodactylus/sone/web/WebPageTest.java b/src/test/java/net/pterodactylus/sone/web/WebPageTest.java index bd6ff9d..a1af8cc 100644 --- a/src/test/java/net/pterodactylus/sone/web/WebPageTest.java +++ b/src/test/java/net/pterodactylus/sone/web/WebPageTest.java @@ -1,5 +1,6 @@ package net.pterodactylus.sone.web; +import static net.pterodactylus.sone.test.GuiceKt.supply; import static net.pterodactylus.sone.web.WebTestUtils.redirectsTo; import static org.junit.Assert.fail; import static org.mockito.ArgumentMatchers.anyBoolean; @@ -60,6 +61,8 @@ import com.google.common.collect.ListMultimap; import com.google.common.collect.Multimap; import com.google.common.eventbus.EventBus; import com.google.common.io.ByteStreams; +import com.google.inject.Guice; +import com.google.inject.Injector; import org.junit.Before; import org.junit.Rule; import org.junit.rules.ExpectedException; @@ -103,6 +106,11 @@ public abstract class WebPageTest { protected final List localSones = new ArrayList<>(); private final ListMultimap postReplies = ArrayListMultimap.create(); + protected final Injector injector = Guice.createInjector( + supply(WebInterface.class).byInstance(webInterface), + supply(Template.class).byInstance(template) + ); + protected WebPageTest() { try { responseInputStream = new PipedInputStream(responseOutputStream); diff --git a/src/test/kotlin/net/pterodactylus/sone/web/AboutPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/AboutPageTest.kt index a0a9014..0e8e6b3 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/AboutPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/AboutPageTest.kt @@ -1,12 +1,10 @@ package net.pterodactylus.sone.web -import com.google.inject.Guice import net.pterodactylus.sone.main.SonePlugin.PluginHomepage import net.pterodactylus.sone.main.SonePlugin.PluginVersion import net.pterodactylus.sone.main.SonePlugin.PluginYear import net.pterodactylus.sone.test.getInstance import net.pterodactylus.sone.test.isProvidedByMock -import net.pterodactylus.util.template.Template import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.equalTo import org.hamcrest.Matchers.notNullValue @@ -22,9 +20,7 @@ class AboutPageTest : WebPageTest() { private val year = 1234 private val homepage = "home://page" private val page = AboutPage(template, webInterface, PluginVersion(version), PluginYear(year), PluginHomepage(homepage)) - private val injector = Guice.createInjector( - Template::class.isProvidedByMock(), - WebInterface::class.isProvidedByMock(), + private val childInjector = injector.createChildInjector( PluginVersion::class.isProvidedByMock(), PluginYear::class.isProvidedByMock(), PluginHomepage::class.isProvidedByMock() @@ -60,13 +56,13 @@ class AboutPageTest : WebPageTest() { @Test fun `page can be created by guice`() { - assertThat(injector.getInstance(), notNullValue()) + assertThat(childInjector.getInstance(), notNullValue()) } @Test fun `page is created as singleton`() { - val firstInstance = injector.getInstance() - val secondInstance = injector.getInstance() + val firstInstance = childInjector.getInstance() + val secondInstance = childInjector.getInstance() assertThat(firstInstance, sameInstance(secondInstance)) }