Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EasyMock -- mock methods within tested class?

In my code I sometimes call public or private methods within the same class. These methods aren't a good candidate for being pulled out into their own class. Each of these methods that I call are tested in their own unit test.

So, if I have a method in my class A that calls each of those methods also in class A, is there some way to mock the calls? I can certainly cut and paste my expectations/mock behavior, but not only is that tedious, it obfuscates the point of the test, violates modularity, and makes testing more difficulty because of the inability to control what is returned.

If not, what is the usual solution to this kind of thing?

like image 931
Jeremy Avatar asked Nov 27 '12 20:11

Jeremy


People also ask

How do you mock a class on EasyMock?

EasyMock: mock(…): generates a mock of the target class, be it a concrete class or an interface. Once created, a mock is in “recording” mode, meaning that EasyMock will record any action the Mock Object takes, and replay them in the “replay” mode.

What method is used to make EasyMock?

The expect() method tells EasyMock to simulate a method with certain arguments. The andReturn() method defines the return value of this method for the specified method parameters. The times() method defines how often the Mock object will be called. The replay() method is called to make the Mock object available.

Is EasyMock a mocking framework?

EasyMock is a mocking framework, JAVA-based library that is used for effective unit testing of JAVA applications. EasyMock is used to mock interfaces so that a dummy functionality can be added to a mock interface that can be used in unit testing.

How do you mock a void method with EasyMock?

If we just want to mock void method and don't want to perform any logic, we can simply use expectLastCall(). andVoid() right after calling void method on mocked object.


2 Answers

It sounds like you're looking for Partial Mocks... here's one blog post that covers them: http://www.jroller.com/alessiopace/entry/partial_mocks_with_easymock

This requires the EasyMock ClassExtension, which unfortunately can't mock private methods however.

like image 114
ach Avatar answered Oct 18 '22 05:10

ach


This can be done with EasyMock 2.2 class extension, or EasyMock 3.0 and on (which includes class extension.)

Partial mocking is documented here:

http://www.easymock.org/EasyMock2_2_2_ClassExtension_Documentation.html

The syntax is fairly simple. You specify the class you're mocking and which methods you're mocking. In this example, imagine that the class is "Dog" and it has two methods, "eat" and "eatUntilFull". You might put this code in the eatUntilFull test:

mockDog = createMockBuilder(Dog.class).addMockedMethod("eat").createMock();

You can then treat that like any other mock.

Caveats:

1) Calling one method within your class from another might be an indication of poor design -- can you abstract out that logic to another class?

2) Even if you can't, there may be no problem in letting your method call another method itself during the test. This may be the preferred behavior.

3) You still can't target private methods, so you may want to set them as package-private instead of private.

like image 31
Jeremy Avatar answered Oct 18 '22 04:10

Jeremy