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

  • SEARCH
  • Home
  • 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 7534489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:01:53+00:00 2026-05-30T06:01:53+00:00

Hello I am struggling with simple problem. General idea: class Foo(){ public boolean method1();

  • 0

Hello I am struggling with simple problem.

General idea:

class Foo(){
  public boolean method1();
  public String method2();
  public String method3();
  public String shortcut(){
    return (method1() == true) ? method2() : method3();
  }
}

How should I test shortcut method?

I know how to mock objects and test methods that use other object. Sample:

class Car{
  public boolean start(){};
  public boolean stop(){};
  public boolean drive(int km){};
}
class CarAutoPilot(){
  public boolean hasGotExternalDevicesAttached(){
     //Hardware specific func and api calls
     //check if gps is available 
     //check if speaker is on
     //check if display is on 
  }
  public boolean drive(Car car, int km){
    //drive
    boolean found = hasGotExternalDevicesAttached();
    boolean start = c.start();
    boolean drive = c.drive(km);
    boolean stop = c.stop();
    return (found && start && drive && stop) == true;   
  }
}

class CarAutoPilotTest(){
   @Test
   public void shouldDriveTenKm(){
     Car carMock = EasyMock.Create(Car.class);
     EasyMock.expect(carMock.start()).andReturns(true);
     EasyMock.expect(carMock.drive()).andReturns(true);
     EasyMock.expect(carMock.stop()).andReturns(true);
     EasyMock.reply(carMock);     

     CarAutoPilot cap = new CarAutoPilot();
     boolean result = cap.drive(cap,10);
     Assert.assertTrue(result);
     EasyMock.verify(carMock);
   }
}

But what about hasGotExternalDevicesAttached() method? This is only sample not real scenario. How should I test drive method? Should I also mock hasGotExternalDevicesAttached function?

Can I mock class that is being tested?

  • 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-05-30T06:01:55+00:00Added an answer on May 30, 2026 at 6:01 am

    You can create a subclass of CarAutoPilot in which you override hasGotExternalDevicesAttached(), and run the test with an instance of this subclass.

    You can do it inline:

    CarAutoPilot cap = new CarAutoPilot() {
        public boolean hasGotExternalDevicesAttached(){
            // return true or false depending on what you want to test
        }
    };
    

    This way you can create a valid unit test for the rest of CarAutoPilot‘s behaviour.

    You can call this a poor man’s partial mock if you wish 🙂

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

Sidebar

Related Questions

Hello struggling here guys.. Is it possible to string replace anything between the the
Hello following problem, i need the id attribute string for each div in a
I'm struggling with my first simple hello world RX application. I'm using VS2010 RC,
Hello all you math whizzes out there! I am struggling with a math problem
Hello can anybody solve this please I'm creating the object in the action class
Hello guys I need to pass a string array over to a setter that
My question may sound naive, but really struggling to do a very simple thing.
hello everyone i struggling to pass two parameter to an event handler basically when
Hello people I've been struggling to use sqlite in my C#2.0 application and I
Hello android developers, I was struggling to get this work but failed and now

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.