Skip to content

Commit 875e5b8

Browse files
committed
Add proper unit tests for builder pattern iluwatar#293
1 parent 1fa617d commit 875e5b8

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.iluwatar.builder;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
import static org.junit.Assert.assertNotNull;
7+
8+
/**
9+
* Date: 12/6/15 - 11:01 PM
10+
*
11+
* @author Jeroen Meulemeester
12+
*/
13+
public class HeroTest {
14+
15+
/**
16+
* Test if we get the expected exception when trying to create a hero without a profession
17+
*/
18+
@Test(expected = IllegalArgumentException.class)
19+
public void testMissingProfession() throws Exception {
20+
new Hero.HeroBuilder(null, "Sir without a job");
21+
}
22+
23+
/**
24+
* Test if we get the expected exception when trying to create a hero without a name
25+
*/
26+
@Test(expected = IllegalArgumentException.class)
27+
public void testMissingName() throws Exception {
28+
new Hero.HeroBuilder(Profession.THIEF, null);
29+
}
30+
31+
/**
32+
* Test if the hero build by the builder has the correct attributes, as requested
33+
*/
34+
@Test
35+
public void testBuildHero() throws Exception {
36+
final String heroName = "Sir Lancelot";
37+
38+
final Hero hero = new Hero.HeroBuilder(Profession.WARRIOR, heroName)
39+
.withArmor(Armor.CHAIN_MAIL)
40+
.withWeapon(Weapon.SWORD)
41+
.withHairType(HairType.LONG_CURLY)
42+
.withHairColor(HairColor.BLOND)
43+
.build();
44+
45+
assertNotNull(hero);
46+
assertNotNull(hero.toString());
47+
assertEquals(Profession.WARRIOR, hero.getProfession());
48+
assertEquals(heroName, hero.getName());
49+
assertEquals(Armor.CHAIN_MAIL, hero.getArmor());
50+
assertEquals(Weapon.SWORD, hero.getWeapon());
51+
assertEquals(HairType.LONG_CURLY, hero.getHairType());
52+
assertEquals(HairColor.BLOND, hero.getHairColor());
53+
54+
}
55+
56+
}

0 commit comments

Comments
 (0)