public interface InterfaceA {
public InternalClass create(Object param1, Object param2);
public int getID();
}
public class InternalClass {
private int result = 0;
public void setResult(int result) {
this.result = result;
}
public int getResult() {
return result;
}
}
public class ExternalClass {
private int id = 0;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
public class ClassB {
private static InterfaceA inta;
public static void setInta(InterfaceA inta) {
ClassB.inta = inta;
}
public InternalClass methodb(Object param1, Object param2){
return inta.create(param1, param2);
}
}
public class ClassUnderTest {
public void createobject(ExternalClass retvalue){
ClassB classb = new ClassB();
Object param1 = new Object();
Object param2 = new Object();
InternalClass obj = classb.methodb(param1, param2); //Cannot modify this line in class
retvalue.setId(obj.getResult());
}
}
//And my Test case
public class ClassTest {
private InterfaceA inta = null;
@Test
public void testcase1() {
//Using createNiceMock doesn't return retval and returns null
inta = org.easymock.EasyMock.createMock(InterfaceA.class);
Object obj1 = new Object();
Object obj2 = new Object();
InternalClass retval = new InternalClass();
retval.setResult(1000);
org.easymock.EasyMock.expect(inta.create(obj1, obj2)).andReturn(retval); //Problem line
org.easymock.EasyMock.replay(inta);
ClassB.setInta(inta);
ClassUnderTest test = new ClassUnderTest();
ExternalClass extclass = new ExternalClass();
test.createobject(extclass);
assertTrue(extclass.getId() == 1000);
}
}
Run the test case here... And i get Unexpected method call expected: 1, actual:
0 error.
I know the error is because i create new object obj1, obj2, where as in the
original class i test, new objects param1 and param2 are created, which doesn't
match the original arguments given in expect.
What i do not know is how do i ignore the error and get the id of the retuning
object? Someone suggested to use createNiceMock instead of createMock, but it
only returns null.
Should i mock the creation of object[in my case different class objects] too? I
personally am not interested in mocking the object instances as it can be
created directly. Or is there any way to override the implementation to compare
the contents of the object rather than the hashcode of the objects?
Original issue reported on code.google.com by bsrera...@gmail.com on 7 Nov 2013 at 11:35
Original issue reported on code.google.com by
bsrera...@gmail.com
on 7 Nov 2013 at 11:35