Initial unit tests
Beginning work on unit tests, opening a draft PR for early feedback.
The key breakthrough was calling Bootstrap.bootStrap() before running tests; without bootstrapping minecraft, registries (etc) are not initialized and essentially no MC classes can be constructed. This apparently affects 1.17 & newer.
My solution is based on this GPL3 code, see comments below.
The goal of this PR is not to provide full test coverage, nor is this PR attempting to add integration tests with minecraft dependencies. That can all come in the future.
Instead this PR aims to enable basic unit testing functionality and provide one or two examples as a starting point.
Integration tests will likely require a test mod/run configured in loom, since the game will actually have to run to test integration. We might be able to do something with Mojang's GameTest system 🤔. That's all out of scope for this PR though.
JUnit 5 is chosen as the testing toolkit to run the tests. Upstream docs.
Mockito is chosen as a mocking framework to mock class dependencies. Upstream docs.
fabric-loader-junit tells JUnit to use fabric's Knot classloader. This enables things like mixin & Environment stripping.
See also
Writing good tests, in particular: don't mock a type you don't own; a long term goal should be to reduce hard dependencies on mc classes & also write integration tests.
AssertJ (alternative assertions syntax we could use instead).
Fixes #151
Pinging @shartte, the original author of @BootstrapMinecraft & BootstrapMinecraftExtension, to ask whether we can include them under the MIT license.
You can
Putting this comment from the Architectury discord here for my own reference...
My repository might help you.
It has both JUnit tests and
GameTest, but no Mixin support as I don't use Mixin. This file also enables you to run JUnit test with Forge and NeoForge. I usefabric-loader-junitfor fabric. It depends on Mockito to inject hacky codes. - Kotori316
Looks like https://github.com/neoforged/FancyModLoader/pull/54 will provide a Neoforge equivalent of fabric-loader-junit