Package com.vst.service

Source Code of com.vst.service.AnswerManagerTest

package com.vst.service;

import java.util.List;
import java.util.ArrayList;

import com.vst.service.BaseManagerTestCase;
import com.vst.dao.AnswerDao;
import com.vst.model.Answer;
import com.vst.service.impl.AnswerManagerImpl;

import org.jmock.Mock;
import org.springframework.orm.ObjectRetrievalFailureException;

public class AnswerManagerTest extends BaseManagerTestCase {
    private final String answerId = "1";
    private AnswerManagerImpl answerManager = new AnswerManagerImpl();
    private Mock answerDao = null;

    protected void setUp() throws Exception {
        super.setUp();
        answerDao = new Mock(AnswerDao.class);
        answerManager.setAnswerDao((AnswerDao) answerDao.proxy());
    }

    protected void tearDown() throws Exception {
        super.tearDown();
        answerManager = null;
    }

    public void testGetAnswers() throws Exception {
        List results = new ArrayList();
        Answer answer = new Answer();
        results.add(answer);

        // set expected behavior on dao
        answerDao.expects(once()).method("getAnswers")
            .will(returnValue(results));

        List answers = answerManager.getAnswers(null);
        assertTrue(answers.size() == 1);
        answerDao.verify();
    }

    public void testGetAnswer() throws Exception {
        // set expected behavior on dao
        answerDao.expects(once()).method("getAnswer")
            .will(returnValue(new Answer()));
        Answer answer = answerManager.getAnswer(answerId);
        assertTrue(answer != null);
        answerDao.verify();
    }

    public void testSaveAnswer() throws Exception {
        Answer answer = new Answer();

        // set expected behavior on dao
        answerDao.expects(once()).method("saveAnswer")
            .with(same(answer)).isVoid();

        answerManager.saveAnswer(answer);
        answerDao.verify();
    }

    public void testAddAndRemoveAnswer() throws Exception {
        Answer answer = new Answer();

        // set required fields

        // set expected behavior on dao
        answerDao.expects(once()).method("saveAnswer")
            .with(same(answer)).isVoid();
        answerManager.saveAnswer(answer);
        answerDao.verify();

        // reset expectations
        answerDao.reset();

        answerDao.expects(once()).method("removeAnswer").with(eq(new Long(answerId)));
        answerManager.removeAnswer(answerId);
        answerDao.verify();

        // reset expectations
        answerDao.reset();
        // remove
        Exception ex = new ObjectRetrievalFailureException(Answer.class, answer.getAnswerId());
        answerDao.expects(once()).method("removeAnswer").isVoid();
        answerDao.expects(once()).method("getAnswer").will(throwException(ex));
        answerManager.removeAnswer(answerId);
        try {
            answerManager.getAnswer(answerId);
            fail("Answer with identifier '" + answerId + "' found in database");
        } catch (ObjectRetrievalFailureException e) {
            assertNotNull(e.getMessage());
        }
        answerDao.verify();
    }
}
TOP

Related Classes of com.vst.service.AnswerManagerTest

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.