Add proper unit tests for builder pattern #293

This commit is contained in:
Jeroen Meulemeester
2015-12-06 23:14:07 +01:00
parent 1fa617d08d
commit 875e5b872c

View File

@ -0,0 +1,56 @@
package com.iluwatar.builder;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/**
* Date: 12/6/15 - 11:01 PM
*
* @author Jeroen Meulemeester
*/
public class HeroTest {
/**
* Test if we get the expected exception when trying to create a hero without a profession
*/
@Test(expected = IllegalArgumentException.class)
public void testMissingProfession() throws Exception {
new Hero.HeroBuilder(null, "Sir without a job");
}
/**
* Test if we get the expected exception when trying to create a hero without a name
*/
@Test(expected = IllegalArgumentException.class)
public void testMissingName() throws Exception {
new Hero.HeroBuilder(Profession.THIEF, null);
}
/**
* Test if the hero build by the builder has the correct attributes, as requested
*/
@Test
public void testBuildHero() throws Exception {
final String heroName = "Sir Lancelot";
final Hero hero = new Hero.HeroBuilder(Profession.WARRIOR, heroName)
.withArmor(Armor.CHAIN_MAIL)
.withWeapon(Weapon.SWORD)
.withHairType(HairType.LONG_CURLY)
.withHairColor(HairColor.BLOND)
.build();
assertNotNull(hero);
assertNotNull(hero.toString());
assertEquals(Profession.WARRIOR, hero.getProfession());
assertEquals(heroName, hero.getName());
assertEquals(Armor.CHAIN_MAIL, hero.getArmor());
assertEquals(Weapon.SWORD, hero.getWeapon());
assertEquals(HairType.LONG_CURLY, hero.getHairType());
assertEquals(HairColor.BLOND, hero.getHairColor());
}
}