Skip to content

Commit 8367c83

Browse files
committed
Add proper unit tests for bridge pattern iluwatar#293
1 parent 875e5b8 commit 8367c83

File tree

5 files changed

+145
-0
lines changed

5 files changed

+145
-0
lines changed

bridge/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,9 @@
1414
<artifactId>junit</artifactId>
1515
<scope>test</scope>
1616
</dependency>
17+
<dependency>
18+
<groupId>org.mockito</groupId>
19+
<artifactId>mockito-core</artifactId>
20+
</dependency>
1721
</dependencies>
1822
</project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.iluwatar.bridge;
2+
3+
import org.junit.Test;
4+
5+
import static org.mockito.Mockito.spy;
6+
import static org.mockito.Mockito.verify;
7+
import static org.mockito.Mockito.verifyNoMoreInteractions;
8+
import static org.mockito.internal.verification.VerificationModeFactory.times;
9+
10+
/**
11+
* Date: 12/6/15 - 11:15 PM
12+
*
13+
* @author Jeroen Meulemeester
14+
*/
15+
public class BlindingMagicWeaponTest extends MagicWeaponTest {
16+
17+
/**
18+
* Invoke all possible actions on the weapon and check if the actions are executed on the actual
19+
* underlying weapon implementation.
20+
*/
21+
@Test
22+
public void testExcalibur() throws Exception {
23+
final Excalibur excalibur = spy(new Excalibur());
24+
final BlindingMagicWeapon blindingMagicWeapon = new BlindingMagicWeapon(excalibur);
25+
26+
testBasicWeaponActions(blindingMagicWeapon, excalibur);
27+
28+
blindingMagicWeapon.blind();
29+
verify(excalibur, times(1)).blindImp();
30+
verifyNoMoreInteractions(excalibur);
31+
}
32+
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.iluwatar.bridge;
2+
3+
import org.junit.Test;
4+
5+
import static org.mockito.Mockito.spy;
6+
import static org.mockito.Mockito.verify;
7+
import static org.mockito.Mockito.verifyNoMoreInteractions;
8+
import static org.mockito.internal.verification.VerificationModeFactory.times;
9+
10+
/**
11+
* Date: 12/6/15 - 11:26 PM
12+
*
13+
* @author Jeroen Meulemeester
14+
*/
15+
public class FlyingMagicWeaponTest extends MagicWeaponTest {
16+
17+
/**
18+
* Invoke all possible actions on the weapon and check if the actions are executed on the actual
19+
* underlying weapon implementation.
20+
*/
21+
@Test
22+
public void testMjollnir() throws Exception {
23+
final Mjollnir mjollnir = spy(new Mjollnir());
24+
final FlyingMagicWeapon flyingMagicWeapon = new FlyingMagicWeapon(mjollnir);
25+
26+
testBasicWeaponActions(flyingMagicWeapon, mjollnir);
27+
28+
flyingMagicWeapon.fly();
29+
verify(mjollnir, times(1)).flyImp();
30+
verifyNoMoreInteractions(mjollnir);
31+
}
32+
33+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.iluwatar.bridge;
2+
3+
import static org.junit.Assert.assertNotNull;
4+
import static org.mockito.Mockito.verify;
5+
import static org.mockito.Mockito.verifyNoMoreInteractions;
6+
import static org.mockito.internal.verification.VerificationModeFactory.times;
7+
8+
/**
9+
* Date: 12/6/15 - 11:28 PM
10+
*
11+
* @author Jeroen Meulemeester
12+
*/
13+
public abstract class MagicWeaponTest {
14+
15+
/**
16+
* Invoke the basic actions of the given weapon, and test if the underlying weapon implementation
17+
* is invoked
18+
*
19+
* @param weaponImpl The spied weapon implementation where actions are bridged to
20+
* @param weapon The weapon, handled by the app
21+
*/
22+
protected final void testBasicWeaponActions(final MagicWeapon weapon,
23+
final MagicWeaponImpl weaponImpl) {
24+
assertNotNull(weapon);
25+
assertNotNull(weaponImpl);
26+
assertNotNull(weapon.getImp());
27+
28+
weapon.swing();
29+
verify(weaponImpl, times(1)).swingImp();
30+
verifyNoMoreInteractions(weaponImpl);
31+
32+
weapon.wield();
33+
verify(weaponImpl, times(1)).wieldImp();
34+
verifyNoMoreInteractions(weaponImpl);
35+
36+
weapon.unwield();
37+
verify(weaponImpl, times(1)).unwieldImp();
38+
verifyNoMoreInteractions(weaponImpl);
39+
40+
}
41+
42+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.iluwatar.bridge;
2+
3+
import org.junit.Test;
4+
5+
import static org.mockito.Mockito.spy;
6+
import static org.mockito.Mockito.verify;
7+
import static org.mockito.Mockito.verifyNoMoreInteractions;
8+
import static org.mockito.internal.verification.VerificationModeFactory.times;
9+
10+
/**
11+
* Date: 12/6/15 - 11:43 PM
12+
*
13+
* @author Jeroen Meulemeester
14+
*/
15+
public class SoulEatingMagicWeaponTest extends MagicWeaponTest {
16+
17+
/**
18+
* Invoke all possible actions on the weapon and check if the actions are executed on the actual
19+
* underlying weapon implementation.
20+
*/
21+
@Test
22+
public void testStormBringer() throws Exception {
23+
final Stormbringer stormbringer = spy(new Stormbringer());
24+
final SoulEatingMagicWeapon soulEatingMagicWeapon = new SoulEatingMagicWeapon(stormbringer);
25+
26+
testBasicWeaponActions(soulEatingMagicWeapon, stormbringer);
27+
28+
soulEatingMagicWeapon.eatSoul();
29+
verify(stormbringer, times(1)).eatSoulImp();
30+
verifyNoMoreInteractions(stormbringer);
31+
}
32+
33+
}

0 commit comments

Comments
 (0)