Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mock a private method of the class under test in JMockit

In my class under test (CUT) - an ejb - I have a private method "getConnection". I want to test another method of the CUT but this method will fail before hand.

I tried it like shown below, but "invoke" is wrong. I don't want to invoke the method, I want to stub it. But how? ('connection' is a stub)

new NonStrictExpectations() {
  {
    invoke(archivingBean, "getConnection");result = connection;
  }
};
archivingBean.moveCreditBasic2Archive(new Date());
like image 987
KFleischer Avatar asked Mar 23 '23 11:03

KFleischer


1 Answers

Your test is correct, except that it's missing a declaration for the mocked type. The EJB class, in this case.

Normally, mocked types are mocked in full (all methods). In such cases, you would declare a @Mocked MyEJB archivingBean parameter to the test method, for example.

For partial mocking, on the other hand, you use the NonStrictExpectations(Object...) constructor, like this:

new NonStrictExpectations(archivingBean) {{ // <== note the argument here
    invoke(archivingBean, "getConnection"); result = connection;
}};
like image 170
Rogério Avatar answered Mar 31 '23 19:03

Rogério