0

I have an abstract base class that defines both concrete and abstract methods. I'm wondering if there's a way, using JMockit, to mock this type such that I can declare expectations for the abstract methods and test the implementation of the concrete methods.

Clearly I can do this without using any mocking framework simply be defining a subclass of the abstract one in my test. Then I can override the abstract methods to return whatever I need. I'm just wondering if there's a slicker, black-magic JMockit way to do this.

pedorro
  • 3,079
  • 1
  • 24
  • 24
  • I thought of using a `MockUp` but it only works with interfaces... The easiest would probably be to create a basic implementation that just returns 0 / false / null / does nothing for all abstract methods. – assylias May 20 '13 at 17:03
  • 2
    `MockUp` can be used to mock any class. What you cannot do is have a `@Mock` method for what is an `abstract` method in the mocked class. – Rogério May 22 '13 at 16:10

1 Answers1

3

Use @Mocked on the abstract class and record/verify expectations on its abstract methods. If you want to execute the non-abstract methods in the mocked class, then apply partial mocking by passing the mocked instance created by JMockit to the Expectations(Object...) or NonStrictExpectations(Object...) constructor.

Rogério
  • 16,171
  • 2
  • 50
  • 63
  • This does not work for me. In 1.39 I use Expectiations(instance) where instance is a @Mocked field. An instance.someMethod call after the Expectations block cause NPE. If I only write Expectation(), then the base class methods are all mocked, it is not a partial mock - as expected. – Hontvári Levente Jun 11 '18 at 18:11