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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:31:25+00:00 2026-05-23T22:31:25+00:00

Given a class hierarchy that looks like this: public class Vehicle { private String

  • 0

Given a class hierarchy that looks like this:

public class Vehicle {

    private String name;

    public Vehicle(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

}


public class Car extends Vehicle {

    public Car(String name) {
        super(name);
    }

    public String drive() {
        return "driving the car";
    }

    public String boardBus() {
        Bus bus = new Bus("bus to cut off");

        return bus.board();
    }

}

public class Bus extends Vehicle {

    public Bus(String name) {
        super(name);
    }

    public String board() {
        return "boarding the bus";
    }

}

I’m trying to test the Car class. However, Car also happens to make use of Bus. So, in my test, I’m trying to mock Bus. My test code looks like this:

import static org.junit.Assert.assertEquals;
import mockit.Mocked;
import mockit.NonStrictExpectations;

import org.junit.Test;

public class CarTest {

    @Test
    public void testCar() {
        final String name = "I am a car";
        final Car car = new Car(name);

        new NonStrictExpectations() {
            @Mocked Bus bus;

            {
                bus.board(); result = "test bus boarding";
            }
        };

        assertEquals("I am a car", car.getName());
    }

}

The assert fails because car.getName() returns null.

By inserting System.out.println‘s in the constructors for Vehicle, Car, and Bus, I’m suspecting that the “real” Vehicle that is loaded by new Car(name) is later replaced by a mocked Vehicle when the @Mocked Bus bus is executed.

Is there a way for jmockit to preserve the real Vehicle that is “instantiated” when Car is constructed?

  • 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-23T22:31:27+00:00Added an answer on May 23, 2026 at 10:31 pm

    I see two solutions:

    @Test
    public void boardBus_usingInstanceSpecificMockingForNewedInstances()
    {
        new Expectations() {
            @Capturing @Injectable Bus bus;
    
            {
                bus.board(); result = "mocked";
            }
        };
    
        String result = new Car("myCar").boardBus();
        assertEquals("mocked", result);
    }
    
    @Test
    public void boardBus_usingPartialMocking()
    {
        final Bus bus = new Bus("");
        new Expectations(bus) {{ bus.board(); result = "mocked"; }};
    
        String result = new Car("myCar").boardBus();
        assertEquals("mocked", result);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data structure that looks like this: public class Node { public
I have this class hierarchy: public interface ISR { } public interface ICU {
Following class structure is given: class Job { String description; Collection<JobHistory> history; } class
For a given class I would like to have tracing functionality i.e. I would
Is there any possibility that GetPropInfo returns nil even if the given class is
Given this class class Foo { // Want to find _bar with reflection [SomeAttribute]
Given a class: from django.db import models class Person(models.Model): name = models.CharField(max_length=20) Is it
I have the following class hierarchy: public class Row : ICloneable, IComparable, IEquatable<Row>, IStringIndexable,
Given that one can define a class in VB Script, is there any way
I've got a class hierarchy as this one: class A { } // class

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.