inventory_mock tests #17

Merged
erns6604 merged 16 commits from inventory_mocks into main 2025-10-30 07:52:55 +01:00
4 changed files with 30 additions and 5 deletions
Showing only changes of commit 89ea411493 - Show all commits

View File

@@ -33,6 +33,20 @@ public class Player extends Entity implements Movable, Actor, HasInventory, HasS
super(name); super(name);
} }
public Player(String name, Inventory inventory) {
super(name);
this.inventory = inventory;
this.position = new Position(0,0);
}
public Player(String name, Job job, Inventory inventory) {
super(name);
this.job = job;
this.inventory = inventory;
this.position = new Position(0,0);
}
@Override @Override
public void moveTo(Position position) { public void moveTo(Position position) {
if(canMoveTo(position)){ if(canMoveTo(position)){

View File

@@ -40,11 +40,12 @@ public class InventoryTest {
var item = new BasicItem("iron_sword", "Iron Sword", 10); var item = new BasicItem("iron_sword", "Iron Sword", 10);
var stack = new ItemStack(item, 1); var stack = new ItemStack(item, 1);
inventory.addItem(stack); inventory.addItem(stack);
assertThat(inventory.getItems().size(), is(0)); assertThat(inventory.getItems().size(), is(0));
} }
@Test @Test
void stack_can_be_added_up_to_capacity() { void add_substack_if_item_fits() {
var inventory = new Inventory(5); var inventory = new Inventory(5);
var item = new BasicItem("sand_grain", "Sand Grain", 1); var item = new BasicItem("sand_grain", "Sand Grain", 1);
var stack = new ItemStack(item, 8); var stack = new ItemStack(item, 8);
@@ -57,7 +58,5 @@ public class InventoryTest {
hasProperty("quantity", equalTo(5))) hasProperty("quantity", equalTo(5)))
)); ));
} }
} }

View File

@@ -1,4 +1,6 @@
import Action.DigAction;
import Entity.Player; import Entity.Player;
import Inventory.Inventory;
import Job.Miner; import Job.Miner;
import Shared.RandomProvider; import Shared.RandomProvider;
import Terrain.Biome; import Terrain.Biome;
@@ -7,9 +9,9 @@ import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.*;
import static org.mockito.Mockito.when;
public class MinerTest { public class MinerTest {
private Player defaultPlayer() {return new Player("John"); } private Player defaultPlayer() {return new Player("John"); }
@@ -65,6 +67,15 @@ public class MinerTest {
) )
); );
} }
@Test
void dig_use_workers_inventory() {
var mockInventory = mock(Inventory.class);
var miner = new Miner(new Player("John", mockInventory));
miner.dig(Biome.MOUNTAIN);
verify(mockInventory).addItem(any());
}
} }

View File

@@ -2,6 +2,7 @@ import Action.DigAction;
import Action.LearnSpellAction; import Action.LearnSpellAction;
import Combat.OffensiveDamageSpell; import Combat.OffensiveDamageSpell;
import Entity.Position; import Entity.Position;
import Inventory.Inventory;
import Job.Miner; import Job.Miner;
import Entity.Player; import Entity.Player;
import Terrain.Biome; import Terrain.Biome;