Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 134483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:37:18+00:00 2026-05-11T06:37:18+00:00

I am trying to test a dao method which uses the hibernate criteria API,

  • 0

I am trying to test a dao method which uses the hibernate criteria API, using JUnit4 and EasyMock 2.4.

When I run the test fixture ‘testGetAsset’ I receive the following exception:

java.lang.AssertionError:    Unexpected method call add(name=Test):    add(name=Test): expected: 1, actual: 0    add(source=GSFP): expected: 1, actual: 0      uniqueResult(): expected: 1, actual: 0   at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:32)   at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:61)   at $Proxy7.add(Unknown Source)   at com.hsbc.sfd.funddb.persistence.dao.AssetDaoImpl.getAsset(AssetDaoImpl.java:80)   at com.hsbc.sfd.funddb.persistence.AssetDaoTest.testGetAsset(AssetDaoTest.java:62)   

I think the problem is related to the mock criteria object not being initialized with Asset.class, but I’m quite a beginner with EasyMock and mock objects in general, so I’d really appreciate it if someone could take a look and tell me what I need to do to make the test pass.

Many thanks, Mark

Code is below:

Dao Method

public Asset getAsset(String name, Source source) {     return (Asset) this.sessionFactory.getCurrentSession()             .createCriteria(Asset.class)             .add(Restrictions.eq('name', name))             .add(Restrictions.eq('source', source))             .uniqueResult(); } 

Test Class

public class AssetDaoTest {  private SessionFactory factory; private Session session; private Criteria criteria;  private AssetDaoImpl dao;   @Before public void setUp() {     factory = createMock(SessionFactory.class);     session = createMock(Session.class);     criteria = createMock(Criteria.class);      dao = new AssetDaoImpl();     dao.setSessionFactory(factory); }  @Test public void testGetAsset() {      String name = 'Test';     Source source = Source.GSFP;      Asset asset = new Asset();     asset.setName(name);     asset.setSource(source);      expect(factory.getCurrentSession()).andReturn(session);     expect(session.createCriteria(Asset.class)).andReturn(criteria);     expect(criteria.add(Restrictions.eq('name', name))).andReturn(criteria);     expect(criteria.add(Restrictions.eq('source', source))).andReturn(criteria);     expect(criteria.uniqueResult()).andReturn(asset);      replay(factory, session, criteria);      dao.getAsset(name, source); } } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T06:37:19+00:00Added an answer on May 11, 2026 at 6:37 am

    The problem is that the Restrictions you’re passing into criteria.add in the mock set up are not the exact same object instances that are passed in at the DAO level.

    Take a look at EasyMock’s argument matchers. You can create your own argument matchers that might give you the flexibility you’re looking for, with a bit of work.

    It may be worth thinking about what you’re really testing here. A good way to test DAOs with hibernate is to mock your database using something like HSQL, instead of trying to mock out the hibernate criteria API. Then you can load some sample data into your in-memory database and just ensure that your DAO is behaving correctly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to test that a particular method throws an expected exception from
I am trying to test a proof of concept that I can run a
I'm trying to test a method I have in my application, but I don't
Im trying to test my extension method that converts a list of strings in
I am trying to test my web service using SOAPUI (the free version). For
I am just trying to test a simple ajax call on my server using
I'm trying to test the DB2 adapter for BizTalk 2006 (not R2). While trying
I am trying to test the likelihood that a particular clustering of data has
I'm trying to test the functionality of a web app by scripting a login
I am trying to test a class that manages data access in the database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.