Question

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());
Was it helpful?

Solution

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;
}};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top