How am I supposed to unit-test methods, which return block types?

Hello!

Imagine, I have a piece of code, which returns different block types in different situations.

public class MyClass {
	public BlockType calculateBlockType(final String input) {
		BlockType result = null;

		// Here I determine the result

		return result;
	}	
}

Now I want to write an automated test, which verifies that this method returns the right block types.

I can’t write something like this:

@Test
public void calculateBlockType() {
	final MyClass sut = new MyClass();
	final BlockType blockType = sut.calculateBlockType("someInput");
	Assert.assertEquals(blockType, BlockTypes.WATER)
}

According to IntelliJ IDEA, all constants in BlockTypes are null.

package org.spongepowered.api.block;

/**
 * An enumeration of all possible {@link BlockType}s in vanilla minecraft.
 */
public final class BlockTypes {
	
	[...]

    public static final BlockType WALL_SIGN = null;
    public static final BlockType WATER = null;
    public static final BlockType WATERLILY = null;

	[...]
}

Therefore, I can’t tell WATER apart from WALL_SIGN, WATERLILY or any other block type.

What is the right way to automatically test code, which operates on block types (apart from wrapping BlockType into some class) ?

Thanks in advance

Dmitri Pisarenko

I believe Mixin populates that stuff so they are not null after pre initialization, but I’m no expert in that matter. Either way below snippet is what you’re looking for.

public void calculateBlockType() {
	final MyClass sut = new MyClass();
	final BlockType blockType = sut.calculateBlockType("someInput");
	if(blockType.equals(BlockTypes.WATER)) {
		// THEY ARE EQUAL
	}
}
1 Like

If you’re using 4.1, the block types are not null, they’re dummy generated objects that are semi unit testable. The other option is to use Mockito to mock the objects for each field and use reflection as a setup before testing.

1 Like