Note that there are some explanatory texts on larger screens.

plurals
  1. POPartial mock of template methods with EasyMock
    primarykey
    data
    text
    <p> I have a couple of classes following the "Template Method" pattern. Abstract class A, and concrete extensions, B and C. Like this:</p> <pre class="lang-java prettyprint-override"><code>public abstract class A { protected abstract String getData() throws SomeException; public void doWork() throws OtherException { try { // business logic ... String data = this.getData(); // more business logic ... } catch(SomeException e) { log("...", e); throw new OtherException("...", e); } } } public Class B extends A { protected String getData() throws SomeException { // complicated logic relying on lots of dependencies } } public Class C extends A { protected String getData() throws SomeException { // different but equally complicated logic relying on lots of dependencies } } </code></pre> <p>I want to write a test to verify when getData() throws SomeException that OtherException is thrown. I really want to avoid mocking up all of the complicated dependencies that would be required to force getData() to throw. I don't care how getData() throws, I just want it to throw. So I think a partial mock is what I want. This is what I have:</p> <pre class="lang-java prettyprint-override"><code>import static org.easymock.EasyMock.*; .... @Test(expected = OtherException.class) public void testSomethingOrAnother() throws Exception { B target = createMockBuilder(B.class).addMockedMethod("getData").createMock(); expect(target.getData()).andThrow(SomeException.class).once(); replay(target) try { target.doWork(); // expect this to throw OtherException; } finally { verify(target); } } </code></pre> <p>The test looks good to me, but when I run it I get this:</p> <pre class="lang-java prettyprint-override"><code>java.lang.Exception: Unexpected exception, expected&lt;OtherException&gt; but was&lt;java.lang.RuntimeException&gt; ... deleted for brevity ... Caused by: java.lang.RuntimeException: Ambiguous name: More than one method are named getData at org.easymock.internal.ReflectionUtils.findMethod(ReflectionUtils.java:96) at org.easymock.internal.ReflectionUtils.findMethod(ReflectionUtils.java:64) at org.easymock.internal.MockBuilder.addMockedMethod(MockBuilder.java:73) at org.easymock.internal.MockBuilder.addMockedMethods(MockBuilder.java:92) at com.mycompany.more.packages.BTest(BTest.java:83) ... deleted for brevity ... ... 16 more </code></pre> <p>To be clear: There is NOT an overload of the getData() method anywhere in the hierarchy.</p> <p>Is EasyMock able to do what I'm trying to do here? What am I missing?</p> <p>relevant versions numbers:</p> <ul> <li>EasyMock 3.0</li> <li>JUnit 4.4</li> <li>Java 1.6</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload