Skip to content

Commit

Permalink
Migrate tests to use JUnit 5
Browse files Browse the repository at this point in the history
  • Loading branch information
Exaxxion committed Dec 19, 2024
1 parent 8e22b15 commit 26616f1
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 101 deletions.
3 changes: 0 additions & 3 deletions dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@
* For more details, see https://docs.gradle.org/8.4/userguide/java_library_plugin.html#sec:java_library_configurations_graph
*/
dependencies {
// Existing tests are written in JUnit 4
testImplementation("junit:junit:4.13.1")

compileOnly rfg.deobf("curse.maven:forestry-59751:2684780") // Forestry 5.8.2.387
api rfg.deobf("codechicken:ChickenASM:1.12-1.0.2.9")
api("codechicken:codechickenlib:3.2.3.358")
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ enableSpotless = false

# Enable JUnit testing platform used for testing your code.
# Uses JUnit 5. See guide and documentation here: https://junit.org/junit5/docs/current/user-guide/
enableJUnit = false
enableJUnit = true

# Deployment debug setting
# Uncomment this to test deployments to CurseForge and Modrinth
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.*;
import net.minecraft.world.World;
import org.junit.*;
import org.junit.jupiter.api.*;

import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;

public class AbstractRecipeLogicTest {

@BeforeClass
@BeforeAll
public static void init() {
Bootstrap.register();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,22 @@
import gregtech.common.metatileentities.electric.multiblockpart.MetaTileEntityItemBus;
import gregtech.common.metatileentities.electric.multiblockpart.MetaTileEntityMultiblockPart;
import gregtech.common.metatileentities.multi.electric.MetaTileEntityElectricBlastFurnace;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.init.Bootstrap;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraftforge.items.IItemHandlerModifiable;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.*;

import java.lang.reflect.Field;

import static org.junit.Assert.*;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

public class MultiblockRecipeLogicTest {

@BeforeClass
@BeforeAll
public static void init() {
Bootstrap.register();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
import com.google.common.collect.ImmutableMap;
import gregtech.api.util.GTLog;
import org.apache.logging.log4j.LogManager;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.*;

import java.util.*;

import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;

public class RecipePropertyStorageTest {
private static final String propInt1Key = "propInt1";
Expand All @@ -21,12 +19,12 @@ public class RecipePropertyStorageTest {

private RecipePropertyStorage storage;

@BeforeClass
@BeforeAll
public static void initTestClassStub() {
GTLog.init(LogManager.getLogger(RecipePropertyStorageTest.class));
}

@Before
@BeforeEach
public void initTestStub() {
this.storage = new RecipePropertyStorage();
}
Expand Down
110 changes: 53 additions & 57 deletions src/test/java/gregtech/api/util/InventoryUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,23 @@
import net.minecraft.item.*;
import net.minecraft.nbt.*;
import net.minecraftforge.items.*;
import org.junit.*;
import org.junit.rules.*;
import org.junit.jupiter.api.*;

import java.util.*;

import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;

public class InventoryUtilsTest
{
/**
* Required. Without this all item-related operations will fail because registries haven't been initialized.
*/
@BeforeClass
@BeforeAll
public static void bootStrap()
{
Bootstrap.register();
}

/**
* Used by tests where exception properties need to be verified.
*/
@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void simulateItemStackMerge_succeeds_for_inserting_single_stack_into_empty_one_slot_inventory()
{
Expand All @@ -41,8 +34,8 @@ public void simulateItemStackMerge_succeeds_for_inserting_single_stack_into_empt
);

assertTrue(
"Merging one full stack into a single empty slot failed.",
result
result,
"Merging one full stack into a single empty slot failed."
);
}

Expand All @@ -59,8 +52,8 @@ public void simulateItemStackMerge_succeeds_for_inserting_two_half_stacks_into_e
);

assertTrue(
"Merging two half-stacks into an empty inventory with one slot failed.",
result
result,
"Merging two half-stacks into an empty inventory with one slot failed."
);
}

Expand All @@ -79,8 +72,8 @@ public void simulateItemStackMerge_succeeds_for_inserting_one_half_stack_into_in
);

assertTrue(
"Merging half a stack into an inventory with one slot containing half a stack of the same item failed.",
result
result,
"Merging half a stack into an inventory with one slot containing half a stack of the same item failed."
);
}

Expand All @@ -101,8 +94,8 @@ public void simulateItemStackMerge_succeeds_for_inserting_one_half_stack_into_in
);

assertTrue(
"Merging half a stack into an inventory with two three-quarter stacks of the same item failed.",
result
result,
"Merging half a stack into an inventory with two three-quarter stacks of the same item failed."
);
}

Expand All @@ -123,8 +116,8 @@ public void simulateItemStackMerge_succeeds_for_inserting_one_half_stack_into_in
);

assertTrue(
"Merging half a stack into an inventory with one three-quarter stack of the same item and one empty slot failed.",
result
result,
"Merging half a stack into an inventory with one three-quarter stack of the same item and one empty slot failed."
);
}

Expand All @@ -144,8 +137,8 @@ public void simulateItemStackMerge_fails_to_insert_items_into_a_full_inventory_w
);

assertFalse(
"Unexpectedly succeeded at merging an arrow into an inventory full of feathers.",
result
result,
"Unexpectedly succeeded at merging an arrow into an inventory full of feathers."
);
}

Expand All @@ -165,8 +158,8 @@ public void simulateItemStackMerge_fails_to_insert_items_into_a_full_inventory_w
);

assertFalse(
"Unexpectedly succeeded at merging feathers into an inventory full of feathers.",
result
result,
"Unexpectedly succeeded at merging feathers into an inventory full of feathers."
);
}

Expand All @@ -187,8 +180,8 @@ public void simulateItemStackMerge_respects_different_NBT_tags_as_different_item
);

assertFalse(
"Unexpectedly succeeded at merging feathers with NBT tags into a stack of plain feathers.",
result
result,
"Unexpectedly succeeded at merging feathers with NBT tags into a stack of plain feathers."
);
}

Expand All @@ -209,8 +202,8 @@ public void simulateItemStackMerge_respects_different_damage_values_as_different
);

assertFalse(
"Unexpectedly succeeded at merging damaged feathers into a stack of plain feathers.",
result
result,
"Unexpectedly succeeded at merging damaged feathers into a stack of plain feathers."
);
}

Expand All @@ -232,8 +225,8 @@ public void simulateItemStackMerge_respects_unstackable_but_otherwise_identical_
);

assertFalse(
"Unexpectedly succeeded at merging a pickaxe into another one.",
result
result,
"Unexpectedly succeeded at merging a pickaxe into another one."
);
}

Expand All @@ -242,8 +235,8 @@ public void normalizeItemStack_returns_empty_list_for_single_empty_stack()
{
List<ItemStack> result = InventoryUtils.normalizeItemStack(ItemStack.EMPTY);
assertTrue(
"Unexpectedly got results when normalizing an empty ItemStack",
result.isEmpty()
result.isEmpty(),
"Unexpectedly got results when normalizing an empty ItemStack"
);
}

Expand All @@ -254,16 +247,16 @@ public void normalizeItemStack_returns_single_element_list_for_a_single_already_
List<ItemStack> result = InventoryUtils.normalizeItemStack(stack);

assertFalse(
"Unexpectedly got no results when normalizing an already normal ItemStack",
result.isEmpty()
result.isEmpty(),
"Unexpectedly got no results when normalizing an already normal ItemStack"
);
assertEquals(
"Unexpectedly got wrong number of resulting stacks when normalizing an already normal ItemStack",
1, result.size()
1, result.size(),
"Unexpectedly got wrong number of resulting stacks when normalizing an already normal ItemStack"
);
assertTrue(
"ItemStack was modified when it didn't need to be",
ItemStack.areItemStacksEqual(stack, result.get(0))
ItemStack.areItemStacksEqual(stack, result.get(0)),
"ItemStack was modified when it didn't need to be"
);
}

Expand All @@ -274,46 +267,49 @@ public void normalizeItemStack_returns_normalized_stacks_for_an_abnormal_stack()
List<ItemStack> result = InventoryUtils.normalizeItemStack(stack);

assertFalse(
"Unexpectedly got no results when normalizing an abnormal stack",
result.isEmpty()
result.isEmpty(),
"Unexpectedly got no results when normalizing an abnormal stack"
);
assertEquals(
"Unexpectedly got wrong number of resulting stacks when normalizing an abnormal ItemStack",
3, result.size()
3, result.size(),
"Unexpectedly got wrong number of resulting stacks when normalizing an abnormal ItemStack"
);

ItemStack expectedFull = new ItemStack(Items.ENDER_PEARL, 16);
ItemStack expectedPartial = new ItemStack(Items.ENDER_PEARL, 13);
assertTrue("First item stack does not match expected full stack",
ItemStack.areItemStacksEqual(expectedFull, result.get(0)));
assertTrue("Second item stack does not match expected full stack",
ItemStack.areItemStacksEqual(expectedFull, result.get(1)));
assertTrue("Third item stack does not match expected partial stack",
ItemStack.areItemStacksEqual(expectedPartial, result.get(2)));
assertTrue(ItemStack.areItemStacksEqual(expectedFull, result.get(0)),
"First item stack does not match expected full stack");
assertTrue(ItemStack.areItemStacksEqual(expectedFull, result.get(1)),
"Second item stack does not match expected full stack");
assertTrue(ItemStack.areItemStacksEqual(expectedPartial, result.get(2)),
"Third item stack does not match expected partial stack");
}

@Test
public void apportionStack_throws_AssertionError_when_supplied_stack_is_empty()
{
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Cannot apportion an empty stack.");
InventoryUtils.apportionStack(ItemStack.EMPTY, 64);
IllegalArgumentException e =
assertThrows(IllegalArgumentException.class,
() -> InventoryUtils.apportionStack(ItemStack.EMPTY, 64));
assertEquals("Cannot apportion an empty stack.", e.getMessage());
}

@Test
public void apportionStack_throws_AssertionError_when_maxCount_is_zero()
{
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Count must be non-zero and positive.");
InventoryUtils.apportionStack(new ItemStack(Items.ARROW, 1), 0);
IllegalArgumentException e =
assertThrows(IllegalArgumentException.class,
() -> InventoryUtils.apportionStack(new ItemStack(Items.ARROW, 1), 0));
assertEquals("Count must be non-zero and positive.", e.getMessage());
}

@Test
public void apportionStack_throws_AssertionError_when_maxCount_is_negative()
{
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Count must be non-zero and positive.");
InventoryUtils.apportionStack(new ItemStack(Items.ARROW, 1), -1);
IllegalArgumentException e =
assertThrows(IllegalArgumentException.class,
() -> InventoryUtils.apportionStack(new ItemStack(Items.ARROW, 1), -1));
assertEquals("Count must be non-zero and positive.", e.getMessage());
}

@Test
Expand Down
Loading

0 comments on commit 26616f1

Please sign in to comment.