-
-
Save karol-lotkowski/b8519081409b602c5f92 to your computer and use it in GitHub Desktop.
Abstract class test with Mockito and JUnit 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.karollotkowski.game; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.mockito.Answers; | |
import org.mockito.Mock; | |
import org.mockito.runners.MockitoJUnitRunner; | |
import static org.hamcrest.MatcherAssert.assertThat; | |
import static org.hamcrest.core.Is.is; | |
import static org.mockito.Mockito.doReturn; | |
import static org.mockito.Mockito.times; | |
import static org.mockito.Mockito.verify; | |
@RunWith(MockitoJUnitRunner.class) | |
public class CombatSkillsTest { | |
@Mock(answer = Answers.CALLS_REAL_METHODS) | |
private CombatSkills combatSkills; | |
private final Fixtures fixtures = new Fixtures(); | |
@Test | |
public void willFightWhenAttackIsPhysical() { | |
// given | |
doReturn(fixtures.fightDamage).when(combatSkills).fight(); | |
// when | |
final int damage = combatSkills.attack(AttackType.PHYSICAL); | |
// then | |
assertThat("Fight damage should match", damage, is(fixtures.fightDamage)); | |
verify(combatSkills).fight(); | |
verify(combatSkills, times(0)).spell(); | |
} | |
@Test | |
public void willSpellWhenAttackIsMagical() { | |
// given | |
doReturn(fixtures.spellDamage).when(combatSkills).spell(); | |
// when | |
final int damage = combatSkills.attack(AttackType.MAGICAL); | |
// then | |
assertThat("Spell damage should match", damage, is(fixtures.spellDamage)); | |
verify(combatSkills, times(0)).fight(); | |
verify(combatSkills).spell(); | |
} | |
@Test | |
public void willSpellWhenAttackIsNotDefined() { | |
// given | |
doReturn(fixtures.fightDamage).when(combatSkills).fight(); | |
doReturn(fixtures.spellDamage).when(combatSkills).spell(); | |
// when | |
final int damage = combatSkills.attack(fixtures.attackTypeNotDefined); | |
// then | |
assertThat("Spell damage should match", damage, is(fixtures.spellDamage)); | |
verify(combatSkills, times(0)).fight(); | |
verify(combatSkills).spell(); | |
} | |
private class Fixtures { | |
private final int spellDamage = 2; | |
private final int fightDamage = 7; | |
private final AttackType attackTypeNotDefined = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment