Add proper unit tests for builder pattern #293
This commit is contained in:
56
builder/src/test/java/com/iluwatar/builder/HeroTest.java
Normal file
56
builder/src/test/java/com/iluwatar/builder/HeroTest.java
Normal 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());
|
||||
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user