Test your integration without Minecraft
Test formats, fixes, mergers and your ModSettings in plain JUnit, because the Settings Saver API is pure Java with no Minecraft classes.
Steps
Formats, fixes and mergers are plain functions over strings and maps. The API has no Minecraft classes, so they run in ordinary unit tests. This guide gives you a test for each part.
Prerequisites#
- JUnit in your build. The examples use JUnit 5.
- The API on the test class path:
dependencies {
testImplementation("dev.xichil.settingssaver:settingssaver-api:1.0.0")
}
What to test#
| Part | The property that matters |
|---|---|
| a format | reading what it wrote gives the same map, in the same order; broken input doesn't throw |
| a fix | upgrade converts old values; converted and unrelated maps are left alone; downgrade reverses it |
| a merger | dependent keys are cleaned up; other changes still arrive |
| the registration | the builder accepts your paths and ids |
Steps#
1. Test a format's round trip#
package com.example.mymod.compat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Map;
import org.junit.jupiter.api.Test;
class SectionedFormatTest {
private static final String FILE = "top = 1\n[video]\nfov = 90\nzoom = 1.5\n";
@Test
void readsSectionsAsDottedKeys() throws Exception {
Map<String, String> values = SectionedFormat.INSTANCE.read(new StringReader(FILE));
assertEquals("90", values.get("video.fov"));
assertEquals("1", values.get("top"));
}
@Test
void roundTripKeepsValuesAndOrder() throws Exception {
Map<String, String> first = SectionedFormat.INSTANCE.read(new StringReader(FILE));
StringWriter out = new StringWriter();
SectionedFormat.INSTANCE.write(first, out);
Map<String, String> second = SectionedFormat.INSTANCE.read(new StringReader(out.toString()));
assertEquals(first, second);
assertEquals(new ArrayList<>(first.keySet()), new ArrayList<>(second.keySet()));
}
@Test
void skipsLinesItCannotParse() throws Exception {
Map<String, String> values = SectionedFormat.INSTANCE.read(new StringReader("garbage\n=x\nfov = 90\n"));
assertEquals(1, values.size());
}
}
Map.equals ignores order, so compare the key lists as well.
2. Test a fix in both directions#
package com.example.mymod.compat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.jupiter.api.Test;
class ZoomPercentFixTest {
private final ZoomPercentFix fix = new ZoomPercentFix();
private static Map<String, String> map(String key, String value) {
Map<String, String> map = new LinkedHashMap<>();
map.put(key, value);
return map;
}
@Test
void upgradeTurnsFactorIntoPercent() {
Map<String, String> settings = map("zoom", "1.5");
fix.upgrade("config/mymod.properties", settings);
assertEquals("150", settings.get("zoom"));
}
@Test
void upgradeLeavesConvertedValuesAlone() {
Map<String, String> settings = map("zoom", "150");
fix.upgrade("config/mymod.properties", settings);
assertEquals("150", settings.get("zoom"));
}
@Test
void upgradeLeavesUnrelatedMapsAlone() {
Map<String, String> settings = map("showHud", "true");
fix.upgrade("config/mymod.properties", settings);
assertEquals(map("showHud", "true"), settings);
}
@Test
void downgradeReversesUpgrade() {
Map<String, String> settings = map("zoom", "150");
fix.downgrade("config/mymod.properties", settings);
assertEquals("1.5", settings.get("zoom"));
}
@Test
void brokenValuesDoNotThrow() {
Map<String, String> settings = map("zoom", "1.x");
fix.upgrade("config/mymod.properties", settings); // must not throw
}
}
The three "leaves alone" and "does not throw" tests cover the rules that are easiest to break. See the rules for a fix.
3. Test a merger#
package com.example.mymod.compat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.jupiter.api.Test;
class PresetMergerTest {
@Test
void newPresetDropsTheOldPresetsValues() {
Map<String, String> stored = new LinkedHashMap<>();
stored.put("preset", "cinematic");
stored.put("preset.fov", "30");
stored.put("showHud", "true");
Map<String, String> changed = new LinkedHashMap<>();
changed.put("preset", "default");
new PresetMerger().merge("config/mymod.properties", stored, changed);
assertEquals("default", stored.get("preset"));
assertFalse(stored.containsKey("preset.fov"));
assertEquals("true", stored.get("showHud"));
}
}
4. Test the registration itself#
ModSettings.builder checks its arguments at once and throws IllegalArgumentException on a mistake,
so building your settings in a test catches bad paths and ids before a player does. The getters of
ModSettings exist for exactly this.
Give your plugin a method that returns the ModSettings, and call it from both register and the test:
@Override
public void register(SettingsSaverRegistrar registrar) {
registrar.register(describe());
}
static ModSettings describe() {
return ModSettings.builder("mymod", "My Mod", "2.1.0")
.file("config/mymod.properties", SettingsFileFormats.PROPERTIES)
.excludedByDefault("window.*")
.build();
}
package com.example.mymod.compat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import dev.xichil.settingssaver.api.v1.ModSettings;
import org.junit.jupiter.api.Test;
class MyModSettingsSaverTest {
@Test
void describesTheConfigFile() {
ModSettings settings = MyModSettingsSaver.describe();
assertEquals("mymod", settings.modId());
assertEquals("config/mymod.properties", settings.entries().get(0).path());
assertFalse(settings.savedByDefault());
}
}
To check that the plugin hands its settings over, pass register a registrar of your own.
SettingsSaverRegistrar has a single method, so a lambda works:
@Test
void registersOnce() {
List<ModSettings> seen = new ArrayList<>();
new MyModSettingsSaver().register(seen::add);
assertEquals(1, seen.size());
}
What unit tests can't cover#
These need a real instance with Settings Saver installed:
- that the service file is in each jar and the plugin is found;
- that your mod reads its config after the restore on each loader;
- checks that depend on other mods, such as a path another mod already registered.
Follow Check it worked once per loader family.
Next steps#
- Fix an integration that doesn't sync
- Build one integration for every loader: the checks per jar.