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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:57:13+00:00 2026-06-07T21:57:13+00:00

I am testing a class let say MyClass with JUnit. I am using easymock

  • 0

I am testing a class let say MyClass with JUnit. I am using easymock to isolate the need for db. It works fine. So if there is a call to a model object I just mock that object.
E.g. if I have

public void method(Project project) { project.getName(); ..}

inside MyClass I just use mockedProject. Then I say MyClass.method(mockedProject);
But what if I have this.getName() inside MyClass. In that case, since I want the real object for the class which I am testing (MyClass) I cannot mock MyClass object. So I cannot define a return value for MyClass object as it is a real object. Please have in mind that getName() would go throw the db which I don’t want it to go.

What should I do in this case when I have this.method() and where the method works with db.
I cannot mock this object which I am testing.
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-07T21:57:15+00:00Added an answer on June 7, 2026 at 9:57 pm

    If you are able to mock the classes that you use to access the DB then do that and then this.getName() will use the mocked classes and will get whatever data you defined.

    If the DB access classes cannot be mocked, then you can’t really do anything.

    UPDATE:

    Testing JPA entities might be a bit trickier, if you can mock the actual entity then by all means, go for it and have it return whatever you like instead of the DB result. But I’m not sure it is possible.

    I suggest you take a look here and here for a more elaborate example.

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

Sidebar

Related Questions

Let's say I am using a framework that has a class called Animal. class
Let's say we are testing a class C which has 2 methods M1 and
I have two classes that I am testing (let's call them ClassA and ClassB).
Okay, let's say we have a class defined like public class TestClass { private
Let say I have a Transformation class that extending a CommonDomain class. and I
Let's say I have a manager that looks something like this: public class CustomerManager
Let's say I have an ActiveRecord model called Book that has a has_many association
I have a question concerning unit testing. Let's say that I have several classes
Let's say I am defining a browser implementation class for my application: class InternetExplorerBrowser
Let's say you have a request class defined like this: public class CreateUserRequest {

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.