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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:44:13+00:00 2026-06-11T08:44:13+00:00

How to mock the object for the Phone object. code bellow, public class Fortest

  • 0

How to mock the object for the Phone object.

code bellow,

public class Fortest {

  UserDao userdao = new UserDao();
  Phone name = new Phone();
  public String handleUser(User user) {

    String returncode="failed";
    //User usr = new User("bob");
    String username=user.getUsername();
    String pass=user.getPass();
    System.out.println("username and password : "+username+" : "+pass);

    Phone name = new Phone();
    String ph = name.getA();
    System.out.println("ph "+ph);

    if(ph.equalsIgnoreCase("test")){
      System.out.println("A "+ph);
      returncode="done";
    }
    System.out.println("returning "+returncode);

    return  returncode;
    //System.out.println("name "+name.toString());
    //System.out.println(name.getA());
  }
}

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-11T08:44:14+00:00Added an answer on June 11, 2026 at 8:44 am

    First I’m going to make some assumptions.
    user.getUsername() & user.getPass() have no side affects.
    The System.out.println are not important to you.

    Thus done your class becomes:

    public class Fortest {
        Phone name = new Phone();
    
        public String handleUser(User user) {
            String ph = name.getA();
    
            if(ph.equalsIgnoreCase("test")){
                return "done";
            }
    
            return  "failed";
    
        } 
    }
    

    So your test has two conditions. Either phone.getA() is “test” and you return “done” or it is not and you return “failed”.

    So how to set set “getA“. One thing is for sure, we will need to be able set “name” from the test. For that we need to “inject” it (we can do it a number of other ways, but I loves injection). I’d use Guice, many would use Spring. Some would use one of the other injection frameworks. But in the tests most of us would use manual injection.

    public class Fortest {
        Phone name;
        Fortest(Phone name) {
            this.name = name;
        }
    
        public String handleUser(User user) {
            String ph = name.getA();
    
            if(ph.equalsIgnoreCase("test")){
                return "done";
            }
    
            return  "failed";
    
        } 
    }
    
    
    public class TestFortest {
       @Before
       public void before() {
              name = ; //... 
              subject = new Fortest(name);
       }
    }
    

    Now the tests are fairly simply:

    public void whenTestModeIsEnabledThenReturnDone() {
         setPhoneIntoTestMode();
         String actual = subject.handleUser(null);
         assertEquals(actual, "done");
    }
    
    public void whenTestModeIsDisabledThenReturnFailed() {
         setPhoneIntoLiveMode();
         String actual = subject.handleUser(null);
         assertEquals(actual, "failed");
    }
    

    The implementation of setPhoneIntoTestMode/setPhoneIntoLiveMode will depend on how complex Phone is. If it is complex than we would look at “facking” it in some way (mocks, stubs, etc). This could be a chunk of code you write, it could be using a tool like Mocketo.

    If the Phone object is simple, and has or can have a “setA” method, then just use that.

    I’m sure later you will need userdao. The same thing will be done at that point. Inject and mock/setup the object.

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

Sidebar

Related Questions

How to mock object for testing if the class you're testing is abstract? you
Is that possible in RhinoMocks to create mock object without it constructor invocation? public
For unit testing purposes I'm trying to write a mock object of a class
So, I'm creating a mock object as a static variable on the class level
[TestMethod] public void TestMethod1() { var mock = new Mock<EmailService>(); mock.Setup(x => x.SendEmail()).Returns(true); var
I am using a mock object in RhinoMocks to represent a class that makes
I'm trying to create a Mockito mock object of a class with some rather
I would like to stub the #class method of a mock object: describe Letter
I have a class that constructs a new object to add to the internal
I have built a mock object using EasyMock, and I'm trying to have the

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.