SHARE
TWEET

Untitled

a guest Aug 18th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package heroRepository;
  2.  
  3. import org.junit.Assert;
  4. import org.junit.Before;
  5. import org.junit.Test;
  6.  
  7. import java.util.ArrayList;
  8. import java.util.Collection;
  9. import java.util.List;
  10.  
  11. public class HeroRepositoryTests {
  12.     private HeroRepository heroRepository;
  13.     private Hero testHero;
  14.  
  15.     @Before
  16.     public  void initialize(){
  17.         this.heroRepository = new HeroRepository();
  18.         this.testHero = new Hero("Test",0);
  19.     }
  20.  
  21.     @Test (expected = NullPointerException.class)
  22.     public void createShouldThrowAnExceptionWhenNullArgumentGiven(){
  23.         this.heroRepository.create(null);
  24.     }
  25.  
  26.     @Test (expected = IllegalArgumentException.class)
  27.     public void createShouldThrowAnExceptionIfHeroAlreadyExists(){
  28.         this.heroRepository.create(testHero);
  29.         this.heroRepository.create(testHero);
  30.     }
  31.  
  32.     @Test
  33.     public void createShouldCreateHeroProperly(){
  34.         Assert.assertEquals(this.heroRepository.create(testHero),
  35.                 String.format("Successfully added hero %s with level %d", this.testHero.getName(), this.testHero.getLevel()));
  36.  
  37.     }
  38.  
  39.     @Test
  40.     public void getCountShouldWorkProperly(){
  41.         this.heroRepository.create(testHero);
  42.         Assert.assertEquals(this.heroRepository.getCount(),1);
  43.     }
  44.  
  45.     @Test(expected = NullPointerException.class)
  46.     public void removeShouldThrowAnExceptionIfNullArgumentGiven(){
  47.         this.heroRepository.remove(null);
  48.     }
  49.  
  50.     @Test(expected = NullPointerException.class)
  51.     public void removeShouldThrowAnExceptionIfEmptyStringGiven(){
  52.         this.heroRepository.remove("  ");
  53.     }
  54.  
  55.     @Test
  56.     public void removeShouldReturnTrueIfHeroRemoved(){
  57.         this.heroRepository.create(testHero);
  58.         Assert.assertTrue(this.heroRepository.remove(this.testHero.getName()));
  59.     }
  60.  
  61.     @Test
  62.     public void removeShouldReturnFalseIfHeroIsNotRemoved(){
  63.         this.heroRepository.create(testHero);
  64.         Assert.assertFalse(this.heroRepository.remove(this.testHero.getName()+"false"));
  65.     }
  66.  
  67.  
  68.     @Test
  69.     public void getHeroWithHighestLevelShouldWorkCorrectly(){
  70.         Hero betterHero= new Hero("Test2",100);
  71.         this.heroRepository.create(testHero);
  72.         this.heroRepository.create(betterHero);
  73.  
  74.         Assert.assertEquals(this.heroRepository.getHeroWithHighestLevel(),betterHero);
  75.     }
  76.  
  77.     @Test
  78.     public void getHeroWithHighestLevelShouldWorkReturnNullIfHeroAbsent(){
  79.  
  80.         Assert.assertNull(this.heroRepository.getHeroWithHighestLevel());
  81.     }
  82.  
  83.  
  84.     @Test
  85.     public void getHeroShouldReturnNullIfFalseArgumentGiven(){
  86.         Assert.assertNull(this.heroRepository.getHero("Fake"));
  87.     }
  88.  
  89.     @Test
  90.     public void getHeroShouldWorkCorrectly(){
  91.         this.heroRepository.create(testHero);
  92.         Assert.assertEquals(this.testHero,this.heroRepository.getHero(this.testHero.getName()));
  93.     }
  94.  
  95.     @Test
  96.     public void getHeroesShouldWorkCorrectly(){
  97.         Collection<Hero> testHeroes = new ArrayList<>();
  98.         testHeroes.add(testHero);
  99.         this.heroRepository.create(testHero);
  100.  
  101.         Assert.assertArrayEquals(testHeroes.toArray(),this.heroRepository.getHeroes().toArray());
  102.     }
  103.  
  104. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top