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 8240311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:26:54+00:00 2026-06-07T20:26:54+00:00

So I have used easymock for mocking my data layer objects while unit testing.

  • 0

So I have used easymock for mocking my data layer objects while unit testing.
I am using JPA for persistency. E.g. Project project = EasyMock.cre..(Project.class); etc.

Now the method which I want to test gets this project does some stuff and then persists it calling persist(project). project is a mocked object so it throws me error here. My manager is telling me since you just want to test the functionality of the method. The return value from db is not imp that is why you should use mocking instead of real db. So in case of this method which has persist method call, what should I do?

Thanks.

  • 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. Editorial Team
    Editorial Team
    2026-06-07T20:26:57+00:00Added an answer on June 7, 2026 at 8:26 pm

    You should be mocking the entity manager rather than the entity.

    The entity is just a pojo that you can easily create, you need to see if the persist is called on the entity manager.

    Edit

    That looks like you are creating an instance of the entity manager in the test under class through a static method. There is no easy way to mock it out.

    You should pass the Entity manager to the object that uses it using dependency injection. Then instead of passing the real implementation, you can just pass the mock instance.

    So your code would look something like:

    Project project = ...    
    
    EntityManager manager = EasyMock.createStrictMock(EntityManager.class); 
    ClassUnderTest test = new ClassUnderTest(manager)
    
    //You expect this to be called    
    manager.persist(project);
    
    EasyMock.replay(manager);
    
    //The method you are testing
    test.save(project);
    
    EasyMock.verify(manager);
    

    (I haven’t used easymock for a while so the methods might not be quite right.)

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

Sidebar

Related Questions

I have used this website for testing http://www.webpagetest.org and among some suggestions for optimization
I have used geocoder in my ruby on rails application for a while but
I have used This guide . But if i add it to another project,
I have used FCKeditor in my project. It worked fine before i add url
I have used Rhino.Mocks extensively currently writing some tests in Java using EasyMocks. However
I have used IPC in Win32 code a while ago - critical sections, events,
I have used the data:image URI to set the source of an image tag.
I have used have_db_column in other project, I don't understand the undefined method error.
I have used twitter bootstrap in my project with DotNetNuke. I did my designing
I have used UIDocument , but I don't understand the advantage over using Archiving?

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.