Skip to content

Instantly share code, notes, and snippets.

@Seggan
Last active May 28, 2021 20:40
Show Gist options
  • Save Seggan/52e3a5896f68b223cbd049b84c147f9b to your computer and use it in GitHub Desktop.
Save Seggan/52e3a5896f68b223cbd049b84c147f9b to your computer and use it in GitHub Desktop.
BlockStorage, mocked
Map<Location, Map<String, String>> storage = new HashMap<>();
try (MockedStatic<BlockStorage> bsMock = Mockito.mockStatic(BlockStorage.class)) {
bsMock.when(() -> BlockStorage.addBlockInfo(any(Block.class), anyString(), anyString()))
.then((Answer<Void>) a -> {
Block b = a.getArgument(0, Block.class);
storage.computeIfAbsent(b.getLocation(), k -> new HashMap<>())
.put(a.getArgument(1), a.getArgument(2));
return null;
}
);
bsMock.when(() -> BlockStorage.getLocationInfo(any(Location.class), anyString()))
.then((Answer<String>) a -> {
Map<String, String> info = storage.get(a.getArgument(0, Location.class));
if (info == null) return null;
return info.get(a.getArgument(1, String.class));
}
);
bsMock.when(() -> BlockStorage.check(any(Block.class))).then((Answer<SlimefunItem>) a -> {
Location l = a.getArgument(0, Block.class).getLocation();
String id = BlockStorage.getLocationInfo(l, "id");
if (id == null) return null;
return SlimefunItem.getByID(id);
}
);
// code goes here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment