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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:50:33+00:00 2026-06-09T07:50:33+00:00

My question: how to create a mocked object and inject attributes via the constructor

  • 0

My question: how to create a mocked object and inject attributes via the constructor or a setter?

I get unexpected invocation errors. I am doing a set on my mock object in @Before to set the name attribute for my BAPI. When i execute the test, I get an unexpected invocation error on my setName method. But, I don’t really want to test this method. I’m just plugging it so the value is there when my code executes the method checkParm(b).

If I don’t add any expectation for the setter on the mocked I get unexpected invocation. But, also if I add this code to the expectations, I still get unexpected invocation.

        ignoring(mockAdapter).setName(BAPI_NAME);
        inSequence(sequence);

here’s my @Before method

@Before
public void setUp() throws Exception {

    objectUnderTest = new ApoSAPExtractor();

    mockAdapter = context.mock(BapiAdapter.class);
    mockAdapter.setName(BAPI_NAME);

    apoResults = new ApoResults();
    apoParameterBean = new ApoParameterBean();
}

Then my test method:

@Test
public final void testExtract() throws Exception {

    final Sequence sequence = context.sequence(SEQUENCE);

    context.checking(new Expectations() {{

        atLeast(1).of(mockAdapter).getName();
        will(returnValue(new String())); 
        inSequence(sequence);

        oneOf(mockAdapter).activate();
        inSequence(sequence);

        oneOf(mockAdapter).getImportTableParameter(IM_PARMS);
        inSequence(sequence);
        will(returnValue(JCoTable.class)); 

        oneOf(mockAdapter).execute(); 
        inSequence(sequence);

        oneOf(mockAdapter).getExportTableAdapter(EX_PO_APO); 
        inSequence(sequence);
        will(returnValue(new TableAdapter(with(any(JCoTable.class)))));

    }});

    objectUnderTest.extract(mockAdapter, apoParameterBean);     
    context.assertIsSatisfied();

}

The class I’m mocking:

     public class ApoSAPExtractor implements SAPExtractor<ApoResults, ApoParameterBean> {

private final static Logger logger = Logger.getLogger(ApoSAPExtractor.class);

public List<ApoResults> extract(BapiAdapter b, ApoParameterBean pb) throws JCoException, Exception {

    checkParm(b);

    List<ApoResults>list = new ArrayList<ApoResults>();
    try {
        b.activate();

        JCoTable itp = b.getImportTableParameter(APOConstants.BAPI_IM_PARMS);
        itp.appendRow();

        JCoTable t = itp.getTable(APOConstants.BAPI_DOC_TYPES);
        Utils.appendParm(t, pb.getDocTypes());

        b.execute();

        TableAdapter ta = b.getExportTableAdapter(APOConstants.BAPI_EX_PO_APO);

        for (int i = 0; i < ta.size(); i++) {

            ApoResults ar = new ApoResults();

            ... lots of setters ...

            list.add(ar);

            ta.next();
        }

    } catch (Exception e) {
        logger.info(String.format("Program %s failed.",this.getClass().getSimpleName(),  "failed"));
        e.printStackTrace();
        throw e;
    }

    return list;
}
  • 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-09T07:50:35+00:00Added an answer on June 9, 2026 at 7:50 am

    You can’t inject things into a mock, you can set expectations on it

    Instead of this (which gives you your unexpected exception):

    mockAdapter.setName(BAPI_NAME);
    

    you might do this in your expectations:

    atLeast(1).of(mockAdapter).getName();
    will(returnValue(BAPI_NAME));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i got a question when create a javascript object, when one function invoking another
I'm doing a practice problem and the question asks to create a destructor to
I've decided to take the advice in this question: create-excel-chart-programmatically-in-php and NOT attempt to
Slightly more advanced mapping then in my previous question :) Tables: create table [Primary]
I am trying to create a question and answers page. In which I am
Question: I have to create a tablet application. In this i want to make
Question How do you procedurally create a function in Python which takes specific named
Question I'm trying to create a basic wxWidgets program with a text entry box,
Question How do I create user account by chef-solo? Why does users recipe needs
Question: Given a SQL string like CREATE VIEW TestView AS SELECT value1, value2 FROM

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.