Skip to content
Snippets Groups Projects
Commit ad5a8180 authored by Joakim's avatar Joakim
Browse files

Added exception handling, and made tests accordingly

parent cbf6b451
No related branches found
No related tags found
No related merge requests found
......@@ -109,7 +109,7 @@ public class Battle{
}
}
}
}catch (InterruptedException e){
}catch (InterruptedException | IllegalArgumentException e){
throw new InterruptedException(e.getMessage());
}
}
......
......@@ -41,7 +41,13 @@ public abstract class Unit {
* @param hasBanner a boolean for whether the army the unit is in has a bannerUnit or not. If so, the damage to the
* unit is increased by 20%
*/
public void attack(Unit opponent, Terrain terrain, boolean hasBanner){
public void attack(Unit opponent, Terrain terrain, boolean hasBanner) throws IllegalArgumentException{
if (opponent == null){
throw new IllegalArgumentException("Opponent cannot be null");
}
else if (terrain == null){
throw new IllegalArgumentException("Terrain cannot be null");
}
int totalDamage = this.attack + this.getAttackBonus(UnitType.valueOf(opponent.getClass().getSimpleName()), terrain);
if (hasBanner){
totalDamage += Math.abs(totalDamage * 0.2);
......
src/main/resources/edu/ntnu/idatt2001/wargamesjfx/images/init.png

2.68 MiB

......@@ -56,4 +56,25 @@ class UnitTest {
assertEquals(2,rangedUnit.getResistBonus(terrain));
assertEquals(2,rangedUnit.getResistBonus(terrain));
}
@Test
@DisplayName("Terrain bonus test")
void getTerrainBonus(){
assertEquals(2, new CavalryUnit("").getTerrainAttackBonus(Terrain.Plains));
assertEquals(2, new InfantryUnit("").getTerrainDefenceBonus(Terrain.Forest));
}
@Test
@DisplayName("Situational bonus test")
void getSituationalBonus(){
assertEquals(-50, new MageUnit("").getSituationalAttackBonus(UnitType.DragonUnit));
}
@Test
@DisplayName("Set Terrain null attack test")
void setInvalidTerrainThrowsException(){
InfantryUnit testInf = new InfantryUnit("");
RangedUnit testRanged = new RangedUnit("");
assertThrows(IllegalArgumentException.class, () -> testInf.attack(testRanged,null, true));
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment