Package cz.muni.fi.pa165.ddtroops.business

Source Code of cz.muni.fi.pa165.ddtroops.business.HeroTest

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package cz.muni.fi.pa165.ddtroops.business;

import cz.muni.fi.pa165.ddtroops.daointerfaces.HeroDAO;
import cz.muni.fi.pa165.ddtroops.dto.DTOFactory;
import cz.muni.fi.pa165.ddtroops.dto.HeroDTO;
import cz.muni.fi.pa165.ddtroops.dto.RaceDTO;
import cz.muni.fi.pa165.ddtroops.entities.Gender;
import cz.muni.fi.pa165.ddtroops.entities.Hero;
import cz.muni.fi.pa165.ddtroops.entities.Race;
import cz.muni.fi.pa165.ddtroops.entities.User;
import cz.muni.fi.pa165.ddtroops.serviceclasses.HeroServiceImpl;
import cz.muni.fi.pa165.ddtroops.serviceinterfaces.HeroService;
import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import static org.mockito.Mockito.*;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.util.ReflectionTestUtils;

/**
*
* @author Erik
*/
@RunWith(MockitoJUnitRunner.class)
@ContextConfiguration(locations = { "classpath:testAppContext.xml" })
public class HeroTest extends TestCase{
    private Hero hero;
    private HeroDTO heroDTO;
    @Mock HeroDAO heroDao;
    private HeroService service;

    @Before
    public void init(){
        service = new HeroServiceImpl();
        ReflectionTestUtils.setField(service, "dao", heroDao);
        hero = this.createHero();
        heroDTO = DTOFactory.createHeroDTO(hero);
    }

    private Hero createHero(){
        hero = new Hero();
        hero.setGender(Gender.MALE);
        hero.setBelief("atheist");
        hero.setProfession("miner");
        Race race = mock(Race.class);
        hero.setRace(race);
        User user = mock(User.class);
        hero.setUser(user);
        hero.setStrength(Byte.MIN_VALUE);
        hero.setDexterity(Byte.MIN_VALUE);
        hero.setConstitution(Byte.MIN_VALUE);
        hero.setIntelligence(Byte.MIN_VALUE);
        hero.setWisdom(Byte.MIN_VALUE);
        hero.setCharisma(Byte.MIN_VALUE);
        return hero;
    }
    @Test
    public void testCreateHero(){
        service.create(heroDTO);
        verify(heroDao).create(hero);
    }
    @Test
    public void testUpdateHero(){
        service.update(heroDTO);
        verify(heroDao).update(hero);
    }
    @Test
    public void testDeleteHero(){
        service.create(heroDTO);
        verify(heroDao).create(hero);
        service.delete(heroDTO);
        verify(heroDao).delete(hero);
    }
    @Test
    public void testGetById(){
        service.create(heroDTO);
        verify(heroDao).create(hero);
        service.getById(hero.getId());
        verify(heroDao).getById(hero.getId());
    }
    @Test
    public void testGetAll(){
        Hero hero2 = this.createHero();
        HeroDTO hero2DTO = DTOFactory.createHeroDTO(hero2);
        service.create(heroDTO);
        service.create(hero2DTO);
        service.getAll();
        verify(heroDao).getAll();
    }

    @Test
    public void testCreateNewHero(){
        HeroDTO newHero = service.createNewHero(Gender.MALE, "atheist", "miner",
                mock(RaceDTO.class), Byte.MIN_VALUE, Byte.MIN_VALUE,
                Byte.MIN_VALUE, Byte.MIN_VALUE, Byte.MIN_VALUE, Byte.MIN_VALUE);
        assertNull(newHero);
    }

}
TOP

Related Classes of cz.muni.fi.pa165.ddtroops.business.HeroTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.