Skip to content

Dev Docs: Unit Testing

soir20 edited this page Jun 18, 2023 · 8 revisions

Most of MoreMcmeta's classes are fully unit tested. As much as possible, code is written to be fairly independent of Minecraft's code to improve testability and minimize changes between Minecraft versions. Testing includes exception testing and null conditions.

Some "unit tests" are closer to integration tests because the component is not useful by itself or contains little logic, such as the texture components.

It is not realistic to try and unit test 100% of MoreMcmeta's code since it uses Minecraft classes that are not easily testable, like the texture atlas. However, there are a few ways to improve code coverage in a modding environment.

Strategies to Improve Testability

  • Dependency injection. Most units accept other units in their constructor. Interfaces are often helpful.
  • Composition over inheritance. That's not to say inheritance is always bad, but composition allows you to mock units and reason about code more easily.
  • Adapters. The adapters package contains code that wraps difficult-to-test Minecraft code in an interface that MoreMcmeta needs.
  • Separation of concerns. You can often isolate difficult-to-test code from testable code.
  • Render system calls. RenderSystem.recordRenderCall() is useful to "ignore" code that must be on the render thread and will crash in tests.

Clone this wiki locally