tags:

views:

26

answers:

1

I am using EasyMocks for J2EE. I have a behavior method call that takes in 3 parameters, one of which is an object array and the other are strings.

classUnderTest.method(new anotherClass("string1","string2", objectArray));

When I try to use the aryEq method on the object array, classUnderTest.method(new anotherClass("string1","string2", aryEq(objectArray)));

I get an unexpected method error, because the behavior I have seems to be using only the array Parameter and is omitting the other two parameters. Unexpected Method : method(new anotherClass("string1","string2", objectArray) Expected method : method(objectArray)

Even if I add matchers to the other parameters, it doesnt work because only 1 matcher is expected.

Thanks

A: 

EasyMock cant be used in that way, what you can try is this

    @Test
    public void objectTest() {
        final Object[] expectedArray = { "a" };

        ClassUnderTest classUnderTest = createStrictMock(ClassUnderTest.class);
        classUnderTest.method((AnotherClass) anyObject());
        IExpectationSetters  expectLastCall = EasyMock
                .expectLastCall();
        expectLastCall.andAnswer(new IAnswer () {
            public Object answer() throws Throwable {
                Object[] args = EasyMock.getCurrentArguments();
                AnotherClass ac = (AnotherClass) args[0];
                Object[] passedArray = ac.getObjectArray();
                System.out.println("passedArray="
                        + Arrays.toString(passedArray));
                Assert.assertEquals(passedArray, expectedArray);
                return null;
            }
        }).anyTimes();
        replay(classUnderTest);


        classUnderTest.method(new AnotherClass("string1", "string2",
                new Object[] { "a" }));
        classUnderTest.method(new AnotherClass("string1", "string2",
                new Object[] { "b" }));
    }

output

passedArray=[a]
passedArray=[b]
FAILED: objectTest
java.lang.AssertionError: Lists differ at element [0]: a != b expected:<a> but was:<b>
01