Moved Sponge tests to JUnit 5

This commit is contained in:
Rsl1122 2019-08-10 08:40:21 +03:00
parent c3f9d77698
commit cc8d7b8e9f
3 changed files with 43 additions and 48 deletions

View File

@ -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.config.PlanConfig;
import com.djrapitops.plan.system.settings.paths.WebserverSettings; import com.djrapitops.plan.system.settings.paths.WebserverSettings;
import com.djrapitops.plan.system.settings.paths.key.Setting; import com.djrapitops.plan.system.settings.paths.key.Setting;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.ClassRule; import org.junit.jupiter.api.Test;
import org.junit.Rule; import org.junit.jupiter.api.io.TempDir;
import org.junit.Test; import org.junit.platform.runner.JUnitPlatform;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import rules.ComponentMocker;
import rules.SpongeComponentMocker;
import utilities.RandomData; import utilities.RandomData;
import utilities.mocks.SpongeMockComponent;
import java.nio.file.Path;
import java.util.Collection; import java.util.Collection;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
/** /**
* Test for Sponge PlanSystem. * Test for Sponge PlanSystem.
* *
* @author Rsl1122 * @author Rsl1122
*/ */
@RunWith(MockitoJUnitRunner.Silent.class) @RunWith(JUnitPlatform.class)
public class SpongeSystemTest { 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 final int TEST_PORT_NUMBER = RandomData.randomInt(9005, 9500);
private PlanSystem system; private PlanSystem system;
@Before @BeforeEach
public void prepareSpongeSystem() { void prepareSpongeSystem(@TempDir Path temp) throws Exception {
system = component.getPlanSystem(); system = new SpongeMockComponent(temp).getPlanSystem();
system.getConfigSystem().getConfig() system.getConfigSystem().getConfig()
.set(WebserverSettings.PORT, TEST_PORT_NUMBER); .set(WebserverSettings.PORT, TEST_PORT_NUMBER);
} }
@Test @Test
public void spongeSystemEnables() throws EnableException { void spongeSystemEnables() throws EnableException {
try { try {
system.enable(); system.enable();
assertTrue(system.isEnabled()); assertTrue(system.isEnabled());
@ -73,7 +66,7 @@ public class SpongeSystemTest {
} }
@Test @Test
public void spongeSystemHasDefaultConfigValuesAfterEnable() throws EnableException, IllegalAccessException { void spongeSystemHasDefaultConfigValuesAfterEnable() throws EnableException, IllegalAccessException {
try { try {
system.enable(); system.enable();
PlanConfig config = system.getConfigSystem().getConfig(); PlanConfig config = system.getConfigSystem().getConfig();

View File

@ -86,12 +86,12 @@ public class PlanSpongeMocker extends Mocker {
return this; return this;
} }
public PlanSpongeMocker withDataFolder(File tempFolder) { PlanSpongeMocker withDataFolder(File tempFolder) {
when(planMock.getDataFolder()).thenReturn(tempFolder); when(planMock.getDataFolder()).thenReturn(tempFolder);
return this; return this;
} }
public PlanSpongeMocker withGame() { PlanSpongeMocker withGame() {
Game game = Mockito.mock(Game.class); Game game = Mockito.mock(Game.class);
Platform platform = mockPlatform(); Platform platform = mockPlatform();
@ -130,12 +130,12 @@ public class PlanSpongeMocker extends Mocker {
return server; return server;
} }
public PlanSpongeMocker withResourceFetchingFromJar() throws Exception { PlanSpongeMocker withResourceFetchingFromJar() throws Exception {
withPluginFiles(); withPluginFiles();
return this; return this;
} }
public PlanSponge getPlanMock() { PlanSponge getPlanMock() {
return planMock; return planMock;
} }
} }

View File

@ -14,44 +14,46 @@
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>. * along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/ */
package rules; package utilities.mocks;
import com.djrapitops.plan.DaggerPlanSpongeComponent; import com.djrapitops.plan.DaggerPlanSpongeComponent;
import com.djrapitops.plan.PlanPlugin;
import com.djrapitops.plan.PlanSponge; import com.djrapitops.plan.PlanSponge;
import com.djrapitops.plan.PlanSpongeComponent; import com.djrapitops.plan.PlanSpongeComponent;
import com.djrapitops.plan.system.PlanSystem; 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 PlanSponge planMock;
private PlanSpongeComponent component; private PlanSpongeComponent component;
public SpongeComponentMocker(TemporaryFolder testFolder) { public SpongeMockComponent(Path tempDir) {
this.testFolder = testFolder; this.tempDir = tempDir;
} }
@Override public PlanSponge getPlanMock() throws Exception {
protected void before() throws Throwable { if (planMock == null) {
PlanSpongeMocker mocker = PlanSpongeMocker.setUp() planMock = PlanSpongeMocker.setUp()
.withDataFolder(testFolder.newFolder()) .withDataFolder(tempDir.toFile())
.withResourceFetchingFromJar() .withResourceFetchingFromJar()
.withGame(); .withGame()
planMock = mocker.getPlanMock(); .getPlanMock();
component = DaggerPlanSpongeComponent.builder().plan(planMock).build(); }
}
public PlanPlugin getPlanMock() {
return planMock; return planMock;
} }
public PlanSystem getPlanSystem() { public PlanSystem getPlanSystem() throws Exception {
if (component == null) {
component = DaggerPlanSpongeComponent.builder().plan(getPlanMock()).build();
}
return component.system(); return component.system();
} }
}
}