Shakti
Shakti

Reputation: 2033

Is there a way to generate all expectations using EasyMock?

I am trying to write a EasyMock Junit test case for some code which is having a lot of extra bits and pieces of code which I am finding a little overkill to Mock. Say for the given example http://java.dzone.com/articles/easymock-tutorial-%E2%80%93-getting,

Following expectation is set to test

portfolio.getTotalValue()

Expectation

EasyMock.expect(marketMock.getPrice("EBAY")).andReturn(42.00);
EasyMock.replay(marketMock);

Now in my case there are around 30-40 such expectations that I need to set before I can come to my piece of code to unit test.

Is there a way to generate expectations of a code or dynamically generate them ? So that I don't have to manually do all this stuff to test my specific piece of code ?

Upvotes: 1

Views: 200

Answers (2)

palacsint
palacsint

Reputation: 28865

Actually, it's a code smell: Hard-to-Test Code. Your object might not fulfill the Single Responsibility Principle (SRP).

You can try extracting out some expectations to one or more allowXY or createMockedXY helper methods (void allowDownloadDocument(path, name, etc), Document createMockedDocument(...) for example). Eliminating static helper classes also could be helpful.

Upvotes: 1

David M. Karr
David M. Karr

Reputation: 15225

No.

Seriously, what would you expect it to do?

You can save some labor over the long run by looking at patterns of expectations in multiple tests, and combining those into reusable methods or "@Before" methods.

Upvotes: 1

Related Questions