easy mock nested sample

<test class>

1. inner class

 

public class ConcatorIml implements Concater {

 

@Override

public String concat(String str1, String str2) throws Exception {

 

String conc = "";

conc = str1+str2;

 

return conc;

}

 

}

 

2.outer class

 

public class parConcatorImpl implements parConcator {

 

@Override

public String conc(Concater cn) {

 

String str = null;

try {

str = cn.concat("a", "b");

} catch (Exception e) {

 

e.printStackTrace();

}

return str;

}

 

}

 

<mock test code>

@Test

public void testCbIml() throws Exception {

 

Concater mock = EasyMock.createMock(ConcatorIml.class);

EasyMock.expect(mock.concat(

      (String) EasyMock.anyObject(),

      (String) EasyMock.anyObject())).andReturn("inner mock return");

EasyMock.replay(mock);

 

parConcator mockMain = EasyMock.createMock(parConcatorImpl.class);

EasyMock.expect(mockMain.conc(mock)).andAnswer(new IAnswer<String>() {

public String answer() throws Throwable {

String str =mock.concat("a", "b");

            return str;

        }

 

});

 

 

EasyMock.replay(mockMain);

 

String str = mockMain.conc(mock);

EasyMock.verify(mockMain);

EasyMock.verify(mock);

 

 

System.out.println(str);

 

}