tags:

views:

143

answers:

2

Using the latest version of EasyMock, I have a method that I need to stub out. The method takes an object parameter and returns void.

The stubbed method is being called by the method I'm testing. No surprises there. My difficulty is that the object that is supplied as an argument to the mocked method is being created by the method I'm testing.

I know I can get around this using createNiceMock() but is there a way to explicitly stub out this method?

Sample code:

public interface IMockMe { 
    void doSomething(InnerObj obj);
}

public class TestMe {
    IMockMe mockMe; 

    public void testThisMethod() {
        InnerObj obj = new InnerObj();
        mockMe.doSomething(obj);
    }
}

class Tester {
    @Test
    public void testThatDarnedMethod() {
        IMockMe mocked = EasyMock.create(IMockMe.class);

        mocked.doSomething( /* what goes here? */);
        EasyMock.expectLastCall();

        TestMe testMe = new TestMe(mocked);
        testMe.testThisMethod();

    }
}
+1  A: 

Have a look at the "Flexible Expectations with Argument Matchers" section of the EasyMock documentation. (No time to provide an example right now, I'm afraid - but basically you set up a matcher to check what you need to be true about the argument.)

Jon Skeet
Beautiful, thanks!
roufamatic
A: 

Use anyObject()

Henri