From cc8d7b8e9fb934df9568ceb17af41abf659101f4 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Sat, 10 Aug 2019 08:40:21 +0300 Subject: [PATCH] Moved Sponge tests to JUnit 5 --- .../com/djrapitops/plan/SpongeSystemTest.java | 35 ++++++-------- .../utilities/mocks/PlanSpongeMocker.java | 8 ++-- .../mocks/SpongeMockComponent.java} | 48 ++++++++++--------- 3 files changed, 43 insertions(+), 48 deletions(-) rename Plan/sponge/src/test/java/{rules/SpongeComponentMocker.java => utilities/mocks/SpongeMockComponent.java} (54%) diff --git a/Plan/sponge/src/test/java/com/djrapitops/plan/SpongeSystemTest.java b/Plan/sponge/src/test/java/com/djrapitops/plan/SpongeSystemTest.java index 2963184bd..dcbcf1d97 100644 --- a/Plan/sponge/src/test/java/com/djrapitops/plan/SpongeSystemTest.java +++ b/Plan/sponge/src/test/java/com/djrapitops/plan/SpongeSystemTest.java @@ -22,48 +22,41 @@ import com.djrapitops.plan.system.settings.ConfigSettingKeyTest; import com.djrapitops.plan.system.settings.config.PlanConfig; import com.djrapitops.plan.system.settings.paths.WebserverSettings; import com.djrapitops.plan.system.settings.paths.key.Setting; -import org.junit.Before; -import org.junit.ClassRule; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.junit.platform.runner.JUnitPlatform; import org.junit.runner.RunWith; -import org.mockito.junit.MockitoJUnitRunner; -import rules.ComponentMocker; -import rules.SpongeComponentMocker; import utilities.RandomData; +import utilities.mocks.SpongeMockComponent; +import java.nio.file.Path; import java.util.Collection; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertTrue; + /** * Test for Sponge PlanSystem. * * @author Rsl1122 */ -@RunWith(MockitoJUnitRunner.Silent.class) +@RunWith(JUnitPlatform.class) public class SpongeSystemTest { - @ClassRule - public static TemporaryFolder temporaryFolder = new TemporaryFolder(); - - @Rule - public ComponentMocker component = new SpongeComponentMocker(temporaryFolder); - private final int TEST_PORT_NUMBER = RandomData.randomInt(9005, 9500); private PlanSystem system; - @Before - public void prepareSpongeSystem() { - system = component.getPlanSystem(); + @BeforeEach + void prepareSpongeSystem(@TempDir Path temp) throws Exception { + system = new SpongeMockComponent(temp).getPlanSystem(); system.getConfigSystem().getConfig() .set(WebserverSettings.PORT, TEST_PORT_NUMBER); } @Test - public void spongeSystemEnables() throws EnableException { + void spongeSystemEnables() throws EnableException { try { system.enable(); assertTrue(system.isEnabled()); @@ -73,7 +66,7 @@ public class SpongeSystemTest { } @Test - public void spongeSystemHasDefaultConfigValuesAfterEnable() throws EnableException, IllegalAccessException { + void spongeSystemHasDefaultConfigValuesAfterEnable() throws EnableException, IllegalAccessException { try { system.enable(); PlanConfig config = system.getConfigSystem().getConfig(); diff --git a/Plan/sponge/src/test/java/utilities/mocks/PlanSpongeMocker.java b/Plan/sponge/src/test/java/utilities/mocks/PlanSpongeMocker.java index c06920025..5963827f7 100644 --- a/Plan/sponge/src/test/java/utilities/mocks/PlanSpongeMocker.java +++ b/Plan/sponge/src/test/java/utilities/mocks/PlanSpongeMocker.java @@ -86,12 +86,12 @@ public class PlanSpongeMocker extends Mocker { return this; } - public PlanSpongeMocker withDataFolder(File tempFolder) { + PlanSpongeMocker withDataFolder(File tempFolder) { when(planMock.getDataFolder()).thenReturn(tempFolder); return this; } - public PlanSpongeMocker withGame() { + PlanSpongeMocker withGame() { Game game = Mockito.mock(Game.class); Platform platform = mockPlatform(); @@ -130,12 +130,12 @@ public class PlanSpongeMocker extends Mocker { return server; } - public PlanSpongeMocker withResourceFetchingFromJar() throws Exception { + PlanSpongeMocker withResourceFetchingFromJar() throws Exception { withPluginFiles(); return this; } - public PlanSponge getPlanMock() { + PlanSponge getPlanMock() { return planMock; } } diff --git a/Plan/sponge/src/test/java/rules/SpongeComponentMocker.java b/Plan/sponge/src/test/java/utilities/mocks/SpongeMockComponent.java similarity index 54% rename from Plan/sponge/src/test/java/rules/SpongeComponentMocker.java rename to Plan/sponge/src/test/java/utilities/mocks/SpongeMockComponent.java index 0d661cc59..0cc59e31e 100644 --- a/Plan/sponge/src/test/java/rules/SpongeComponentMocker.java +++ b/Plan/sponge/src/test/java/utilities/mocks/SpongeMockComponent.java @@ -14,44 +14,46 @@ * You should have received a copy of the GNU Lesser General Public License * along with Plan. If not, see . */ -package rules; +package utilities.mocks; import com.djrapitops.plan.DaggerPlanSpongeComponent; -import com.djrapitops.plan.PlanPlugin; import com.djrapitops.plan.PlanSponge; import com.djrapitops.plan.PlanSpongeComponent; import com.djrapitops.plan.system.PlanSystem; -import org.junit.rules.ExternalResource; -import org.junit.rules.TemporaryFolder; -import utilities.mocks.PlanSpongeMocker; -public class SpongeComponentMocker extends ExternalResource implements ComponentMocker { +import java.nio.file.Path; - private final TemporaryFolder testFolder; +/** + * Test utility for creating a dagger PlanComponent using a mocked PlanSponge. + * + * @author Rsl1122 + */ +public class SpongeMockComponent { + + private final Path tempDir; private PlanSponge planMock; private PlanSpongeComponent component; - public SpongeComponentMocker(TemporaryFolder testFolder) { - this.testFolder = testFolder; + public SpongeMockComponent(Path tempDir) { + this.tempDir = tempDir; } - @Override - protected void before() throws Throwable { - PlanSpongeMocker mocker = PlanSpongeMocker.setUp() - .withDataFolder(testFolder.newFolder()) - .withResourceFetchingFromJar() - .withGame(); - planMock = mocker.getPlanMock(); - component = DaggerPlanSpongeComponent.builder().plan(planMock).build(); - } - - public PlanPlugin getPlanMock() { + public PlanSponge getPlanMock() throws Exception { + if (planMock == null) { + planMock = PlanSpongeMocker.setUp() + .withDataFolder(tempDir.toFile()) + .withResourceFetchingFromJar() + .withGame() + .getPlanMock(); + } return planMock; } - public PlanSystem getPlanSystem() { + public PlanSystem getPlanSystem() throws Exception { + if (component == null) { + component = DaggerPlanSpongeComponent.builder().plan(getPlanMock()).build(); + } return component.system(); } - -} +} \ No newline at end of file