Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can EasyMock support multiple overloaded methods being added to createMockBuilder

Tags:

java

easymock

I am working with existing test cases that use EasyMock to mock a class. I overloaded a no-parameter method so that now a method exists to take a string. For example:

public class HelloClass {
   // This method always existed.
   public String methodHello() {
       ...
   }
   // This method is new; it overloads the methodHello() method.
   public String methodHello(String msg) {
       ...
   }
}

In the test class, the HelloClass is mocked. As a result, I added the overloaded method so that we have the declaration:

public static HelloClass mockHelloClass = createMockBuilder(HelloClass.class)
   .addMockedMethod("methodHello")
   .addMockedMethod("methodHello", String.class)
   .createMock();

However, the test cases fail when I run them. When I make the methodHello(String) method private then the test cases pass again.

Is EasyMock able to handle several overloaded methods being added to the createMockBuilder?

like image 698
whyceewhite Avatar asked Dec 16 '14 01:12

whyceewhite


1 Answers

I think you encountered this exception at runtime:

java.lang.RuntimeException: Ambiguous name: More than one method are named methodHello

Here's what your mock object should have looked like instead:

public static HelloClass mockHelloClass = createMockBuilder(HelloClass.class)
   .addMockedMethod("methodHello", new Class[]{}) // you got this one wrong
   .addMockedMethod("methodHello", String.class)
   .createMock();

You should clearly specify which methods you want to mock - adding a mocked method like

addMockedMethod("methodHello")

doesn't automatically mean you are talking about the overloaded variant that takes no parameters. This is how you represent it instead:

addMockedMethod("methodHello", new Class[]{})

like image 165
mystarrocks Avatar answered Sep 20 '22 04:09

mystarrocks