easy mock issue

Hi
  Can someone advice why this dao unit test is not working....i've posted all the code

i get this error..

testCreate(PollAnswerDaoUnitTest)  Time elapsed: 0.004 sec  <<< FAILURE!
java.lang.AssertionError:
  Unexpected method call save(PollAnswer@4a9a7d):
    save(PollAnswerDaoUnitTest$1@671f95): expected: 1, actual: 0
        at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:29)
        at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:56)
        at org.easymock.classextension.internal.ClassProxyFactory$1.intercept(ClassProxyFactory.java:74)
        at org.springframework.orm.hibernate3.HibernateTemplate$$EnhancerByCGLIB$$bc66e527.save(<generated>)
        at dao.hibernate.AbstractDao.create(AbstractDao.java:42)
        at HibernatePollAnswerDao.create(HibernatePollAnswerDao.java:44)
        at PollAnswerDaoUnitTest.testCreate(PollAnswerDaoUnitTest.java:88)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:585)
        at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59)
        at org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:98)
        at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:79)
        at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:87)
        at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:77)
        at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
        at org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:88)
        at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
        at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
        at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
        at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
        at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
        at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
        at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
        at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
        at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:585)
        at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:338)
        at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:997)


Thanks


public class PollAnswerDaoUnitTest {
    private static final Logger logger = LoggerFactory.getLogger(PollAnswerDaoUnitTest.class);
    private HibernateTemplate mockHibernateTemplate;
    private PollAnswer pollAnswer;
 
    /**
     * Setup test data for all tests
     */
    @Before
    public void setUp() {
        mockHibernateTemplate = createMock(HibernateTemplate.class);
        pollAnswer = createTestData(5555);
    }
 
    /**
     * Tear down to reset/release resources after the tests are run
     */
    @After
    public void tearDown() {
        reset(mockHibernateTemplate);
    }
 
    /**
     * Test Create Method
     */
    @Test
    public void testCreate() {
        // SETUP
        PollAnswerDao pollAnswerDao = new HibernatePollAnswerDao() {
 
                {
                    hibernateTemplate = mockHibernateTemplate;
                }
            };
 
        Integer expectedTransactionLogId = pollAnswer.getSmscNumber();
        expect(mockHibernateTemplate.save(pollAnswerDao))
            .andReturn(expectedTransactionLogId);
        ;
        replay(mockHibernateTemplate);
 
        // TEST
        Integer actualTransactionLogId = pollAnswerDao.create(pollAnswer);
 
        // VERIFY
        assertEquals(actualTransactionLogId, expectedTransactionLogId);
        verify(mockHibernateTemplate);
    }
 
    /**
     * Test Read Method
     */
 
    /**
     * createTestData
     *
     * @return TransactionLog
     */
    private PollAnswer createTestData(int id) {
        PollAnswer a = new PollAnswer();
        a.setAnswerText("test");
        a.setMsisdn("030");
        a.setSmscNumber(id);
 
        return a;
    }
 
 
 
 
@Entity
@Table(name = "POLL_ANSWER")
public class PollAnswer {
    @Id
    @Column(name = "POLL_ANSWER_ID", nullable = false)
    // NOTE - this custom generator will use the sequence to generate the id if the transactionId property is null. Otherwise it will use the value of the property.
    @GenericGenerator(name = "poll_answer_seq", strategy = "TransactionIdSequenceGenerator")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "poll_answer_seq")
    private Integer pollAnswerId;
    @Column(name = "MSISDN")
    private String msisdn;
    @Column(name = "SMSC_NUMBER")
    private int smscNumber;
    @Column(name = "RECEIVED_DATE")
    @Formula("SELECT SYSDATE FROM DUAL")
    private String receivedDate;
    @Column(name = "ANSWER_TEXT")
    private String answerText;
 
    public Integer getPollAnswerId() {
        return pollAnswerId;
    }
 
    public void setPollAnswerId(Integer id) {
        this.pollAnswerId = id;
    }
 
    public int getSmscNumber() {
        return smscNumber;
    }
 
    public void setSmscNumber(int smsc_number) {
        this.smscNumber = smsc_number;
    }
 
    public String getReceivedDate() {
        return receivedDate;
    }
 
    public void setReceivedDate(String received_date) {
        this.receivedDate = received_date;
    }
 
    public String getMsisdn() {
        return msisdn;
    }
 
    public void setMsisdn(String msisdnVal) {
        this.msisdn = msisdnVal;
    }
 
    public String getAnswerText() {
        return answerText;
    }
 
    public void setAnswerText(String answer_text) {
        this.answerText = answer_text;
    }
}
 
 
 
public class HibernatePollAnswerDao extends AbstractDao implements PollAnswerDao {
    private Logger log = LoggerFactory.getLogger(getClass());
 
    public Integer create(PollAnswer pollAnswer) {
        return (Integer) super.create(pollAnswer);
    }
}

Open in new window

trance12Asked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
doesn't sound right to me, your dao class should be calling hibernatetemplate.
I don't see why you would be passing your dao to hibernatetemplae

and the unit tests agrees :)

0
 
objectsCommented:
looks like save() does not get called

0
 
trance12Author Commented:
when i put logging, it seems to be failing at this line

Integer actualTransactionLogId = pollAnswerDao.create(pollAnswer);


which is the insert into table...
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
trance12Author Commented:
forgot to post PollAnswerDao...
public interface PollAnswerDao {
 
    public Integer create(final PollAnswer pollAnswer);
}

Open in new window

0
 
objectsCommented:
>         expect(mockHibernateTemplate.save(pollAnswerDao))

would u expect save to passed the PollAnswer, you have it expecting to have the dao passed to it

0
 
trance12Author Commented:
yes.,...cause this is to insert the record and return the primary key generated (sequence).....so it takes a dao as input and inserts the dao....

0
 
objectsCommented:
can you post the source for AbstractDao.create()

0
 
trance12Author Commented:
oops...yep I need to pass the domain model...

Thanks...:)
0
 
trance12Author Commented:
this is abstractdao.create


 protected Object create(final Object domainObject) {
        return this.hibernateTemplate.save(domainObject);
    }

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.