Gandalf StormCrow
Gandalf StormCrow

Reputation: 26222

How to mock object?

How to mock object for testing if the class you're testing is abstract? you can't instantiate from it ?

Upvotes: 0

Views: 351

Answers (3)

Alexander Pogrebnyak
Alexander Pogrebnyak

Reputation: 45616

If you want to test the common implementation of your abstract class, then you should create a dummy concrete implementation in you test subsystem and just unit test the common methods of that class.

If, on the other hand, you need to pass around a reference to your abstract class, then, as Bozho has suggested, you should use a mocking framework. My favorite is JMock.

Upvotes: 2

Bozho
Bozho

Reputation: 597422

Mocking is creating a mock implementation of an interface or abstract class, and rarely of concrete classes. You can make these mocks at runtime using a mocking framework:

Upvotes: 3

matt b
matt b

Reputation: 140061

As Paddy mentions, you really want to test a concrete class.

But, if you want to test functionality provided by an abstract class, the common method for doing so is to create an abstract TestCase with an abstract method for providing the concrete class and then test the common functionality within your AbstractWhateverTestCase.

See http://c2.com/cgi/wiki?AbstractTestCases for some examples.

Upvotes: 1

Related Questions