8000 Refactored tests for Bridge · prog012/java-design-patterns@6f1736d · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 6f1736d

Browse files
committed
Refactored tests for Bridge
1 parent a5ec376 commit 6f1736d

File tree

7 files changed

+46
-114
lines changed

7 files changed

+46
-114
lines changed

bridge/src/main/java/com/iluwatar/bridge/Hammer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,9 @@ public void unwield() {
5757
LOGGER.info("The hammer is unwielded.");
5858
enchantment.onDeactivate();
5959
}
60+
61+
@Override
62+
public Enchantment getEnchantment() {
63+
return enchantment;
64+
}
6065
}

bridge/src/main/java/com/iluwatar/bridge/Sword.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,9 @@ public void unwield() {
5757
LOGGER.info("The sword is unwielded.");
5858
enchantment.onDeactivate();
5959
}
60+
61+
@Override
62+
public Enchantment getEnchantment() {
63+
return enchantment;
64+
}
6065
}

bridge/src/main/java/com/iluwatar/bridge/Weapon.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,6 @@ public interface Weapon {
3434
void swing();
3535

3636
void unwield();
37+
38+
Enchantment getEnchantment();
3739
}

bridge/src/test/java/com/iluwatar/bridge/FlyingMagicWeaponTest.java renamed to bridge/src/test/java/com/iluwatar/bridge/HammerTest.java

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,22 @@
2424

2525
import org.junit.Test;
2626

27+
import static org.mockito.Mockito.mock;
2728
import static org.mockito.Mockito.spy;
2829
import static org.mockito.Mockito.verify;
29-
import static org.mockito.Mockito.verifyNoMoreInteractions;
30-
import static org.mockito.internal.verification.VerificationModeFactory.times;
3130

3231
/**
33-
* Date: 12/6/15 - 11:26 PM
34-
*
35-
* @author Jeroen Meulemeester
32+
* Tests for hammer
3633
*/
37-
public class FlyingMagicWeaponTest extends MagicWeaponTest {
34+
public class HammerTest extends WeaponTest {
3835

3936
/**
4037
* Invoke all possible actions on the weapon and check if the actions are executed on the actual
4138
* underlying weapon implementation.
4239
*/
4340
@Test
44-
public void testMjollnir() throws Exception {
45-
// final Mjollnir mjollnir = spy(new Mjollnir());
46-
// final FlyingMagicWeapon flyingMagicWeapon = new FlyingMagicWeapon(mjollnir);
47-
//
48-
// testBasicWeaponActions(flyingMagicWeapon, mjollnir);
49-
//
50-
// flyingMagicWeapon.fly();
51-
// verify(mjollnir, times(1)).flyImp();
52-
// verifyNoMoreInteractions(mjollnir);
41+
public void testHammer() throws Exception {
42+
final Hammer hammer = spy(new Hammer(mock(FlyingEnchantment.class)));
43+
testBasicWeaponActions(hammer);
5344
}
54-
5545
}

bridge/src/test/java/com/iluwatar/bridge/SoulEatingMagicWeaponTest.java

Lines changed: 0 additions & 55 deletions
This file was deleted.

bridge/src/test/java/com/iluwatar/bridge/BlindingMagicWeaponTest.java renamed to bridge/src/test/java/com/iluwatar/bridge/SwordTest.java

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,22 @@
2424

2525
import org.junit.Test;
2626

27+
import static org.mockito.Mockito.mock;
2728
import static org.mockito.Mockito.spy;
2829
import static org.mockito.Mockito.verify;
29-
import static org.mockito.Mockito.verifyNoMoreInteractions;
30-
import static org.mockito.internal.verification.VerificationModeFactory.times;
3130

3231
/**
33-
* Date: 12/6/15 - 11:15 PM
34-
*
35-
* @author Jeroen Meulemeester
32+
* Tests for sword
3633
*/
37-
public class BlindingMagicWeaponTest extends MagicWeaponTest {
34+
public class SwordTest extends WeaponTest {
3835

3936
/**
4037
* Invoke all possible actions on the weapon and check if the actions are executed on the actual
4138
* underlying weapon implementation.
4239
*/
4340
@Test
44-
public void testExcalibur() throws Exception {
45-
// final Excalibur excalibur = spy(new Excalibur());
46-
// final Hammer blindingMagicWeapon = new Hammer(excalibur);
47-
//
48-
// testBasicWeaponActions(blindingMagicWeapon, excalibur);
49-
//
50-
// blindingMagicWeapon.blind();
51-
// verify(excalibur, times(1)).blindImp();
52-
// verifyNoMoreInteractions(excalibur);
41+
public void testSword() throws Exception {
42+
final Sword sword = spy(new Sword(mock(FlyingEnchantment.class)));
43+
testBasicWeaponActions(sword);
5344
}
54-
55-
}
45+
}

bridge/src/test/java/com/iluwatar/bridge/MagicWeaponTest.java renamed to bridge/src/test/java/com/iluwatar/bridge/WeaponTest.java

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,37 +28,32 @@
2828
import static org.mockito.internal.verification.VerificationModeFactory.times;
2929

3030
/**
31-
* Date: 12/6/15 - 11:28 PM
32-
*
33-
* @author Jeroen Meulemeester
31+
* Base class for weapon tests
3432
*/
35-
public abstract class MagicWeaponTest {
33+
public abstract class WeaponTest {
3634

3735
/**
38-
* Invoke the basic actions of the given weapon, and test if the underlying weapon implementation
36+
* Invoke the basic actions of the given weapon, and test if the underlying enchantment implementation
3937
* is invoked
4038
*
41-
* @param weaponImpl The spied weapon implementation where actions are bridged to
42-
* @param weapon The weapon, handled by the app
4339
*/
44-
protected final void testBasicWeaponActions(final Weapon weapon,
45-
final Enchantment weaponImpl) {
46-
// assertNotNull(weapon);
47-
// assertNotNull(weaponImpl);
48-
// assertNotNull(weapon.getEnchantment());
49-
//
50-
// weapon.swing();
51-
// verify(weaponImpl, times(1)).swingImp();
52-
// verifyNoMoreInteractions(weaponImpl);
53-
//
54-
// weapon.wield();
55-
// verify(weaponImpl, times(1)).wieldImp();
56-
// verifyNoMoreInteractions(weaponImpl);
57-
//
58-
// weapon.unwield();
59-
// verify(weaponImpl, times(1)).unwieldImp();
60-
// verifyNoMoreInteractions(weaponImpl);
61-
//
62-
}
40+
protected final void testBasicWeaponActions(final Weapon weapon) {
41+
assertNotNull(weapon);
42+
Enchantment enchantment = weapon.getEnchantment();
43+
assertNotNull(enchantment);
44+
assertNotNull(weapon.getEnchantment());
45+
46+
weapon.swing();
47+
verify(enchantment, times(1)).apply();
48+
verifyNoMoreInteractions(enchantment);
6349

50+
weapon.wield();
51+
verify(enchantment, times(1)).onActivate();
52+
verifyNoMoreInteractions(enchantment);
53+
54+
weapon.unwield();
55+
verify(enchantment, times(1)).onDeactivate();
56+
verifyNoMoreInteractions(enchantment);
57+
58+
}
6459
}

0 commit comments

Comments
 (0)
0