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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:04:13+00:00 2026-06-19T00:04:13+00:00

public class TargetObjectWrapper extends TargetObject { @Mock SomeObject myMockOfSomeObject; @InjectMocks TargetObject targetObject; public TargetObjectWrapper(){

  • 0
public class TargetObjectWrapper extends TargetObject {
   @Mock SomeObject myMockOfSomeObject;
   @InjectMocks TargetObject targetObject;

   public TargetObjectWrapper(){
       this.targetObject = mock(originalObject);

       //doAnswer(...).when(...) methods
   } 

   //Override other methods of TargetObject
 }

I’m trying to inject a specific mock object into one field of a targetObject. I know that targetObject will always have a field with this specific signature.

This all works, I can inject myMockOfSomeObject successfully, and redirect the calls appropriately.

But when the TargetObject class contains other fields like this simple example below, that field gets nulled out and causes null pointer exceptions when I pass requests on to the original object.

Log log = LogFactory.getLog(getClass());

How can I avoid causing other objects to get null values? Especially if the other variables might change from object to object (this code should be generic)?

  • 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-19T00:04:15+00:00Added an answer on June 19, 2026 at 12:04 am

    It seems that spy(..) rather than mock(...) is the correct way to go in this case. Though I haven’t yet tested that it injected my mock object, I believe it did.

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

Sidebar

Related Questions

public class HttpPostTask extends AsyncTask<Void, Integer, Void> { TextView txtStatus = (TextView)findViewById(R.id.txtStatus); @Override protected
public class CheckBoxDemo extends Activity implements CompoundButton.OnCheckedChangeListener { CheckBox cb; @Override public void onCreate(Bundle
public class XPBN extends Activity{ private Map _map; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState);
public class SequenceControlNumber extends SequenceGenerator { private static final Logger log = LoggerFactory.getLogger(SequenceGenerator.class); @Override
public class MainActivity extends Activity { Button b; //FrameLayout fl; @Override protected void onCreate(Bundle
public class cPan extends JPanel implements ActionListener{ @Override public void actionPerformed(ActionEvent arg0) { }
public class tryAnimActivity extends Activity { /** * The thread to process splash screen
public class GraphicsBoard extends LayerUI { String picPath = pictures/; String[] fileName = {
public class InsertionSort { public static <T extends Comparable<T>> void sort(T[] array) { for
public class MyActivity extends Activity { /** Called when the activity is first created.

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.