@EcsTest
Annotate a test method with@EcsTest to receive an EcsTestContext and automatically get the "ecs" tag. Tests marked with @EcsTest are scheduled on the world thread and have full access to the ECS store and command buffer.
EcsTestContext Methods
| Method | Description |
|---|---|
getStore() | Get the ECS store |
getCommandBuffer() | Get a command buffer for deferred operations |
createEntity() | Create a new empty entity, returns a reference |
flush() | Execute all deferred command buffer operations |
putComponent(ref, type, component) | Attach a component to an entity |
removeComponent(ref, type) | Remove a component from an entity |
getComponent(ref, type) | Get a component (returns null if absent) |
hasComponent(ref, type) | Check if entity has a component |
waitTicks(n) | Wait for N world ticks |
awaitCondition(supplier, maxTicks) | Poll each tick until non-null or timeout |
findEntities(componentType) | Find all entities with a given component |
countEntities(componentType) | Count entities with a given component |
EcsAssert Methods
| Method | Description |
|---|---|
assertHasComponent(store, ref, type) | Assert entity has the component |
assertNotHasComponent(store, ref, type) | Assert entity lacks the component |
assertGetComponent(store, ref, type) | Assert component exists and return it |
assertRefValid(ref) | Assert entity reference is non-null |
assertRefInvalid(ref) | Assert entity reference is null |
Examples
Create entity and attach component
Create entity and attach component
Assert and retrieve a component
Assert and retrieve a component
Remove component and verify absence
Remove component and verify absence
Query entities by component type
Query entities by component type
How EcsAssert Works Internally
Becausehrtk-api cannot compile against Hytale server classes directly, all ECS assertions use reflection. EcsAssert.assertHasComponent() calls store.getComponent(ref, componentType) via Method.invoke(). This means:
- The
storeparameter is actually aStore<EntityStore>at runtime - The
refparameter is aRef<EntityStore> - The
componentTypeis aComponentType<EntityStore, T>
Object in the API, but they must be the correct Hytale types at runtime.
The reflection-based approach is an intentional design choice. It allows
hrtk-api to remain a compile-only dependency with no runtime coupling to HytaleServer.jar.Next Steps
- World Testing — block operations and entity spawning
- Stats & Combat — health, damage, and alive/dead assertions
- Isolation: SNAPSHOT — protect live ECS state during tests