0
我有一個類MyClass
,它包含一個調用接口MyInterface
的構造函數。 MyInterface
包含接受Int
和String
的方法validator
。使用EasyMock來模擬接受參數的接口
我需要在JUnit測試中使用EasyMock來模擬MyInterface.validator
的返回Boolean
值。
我已經做了幾次嘗試,我只是在嘗試從MyClass
調用MyInterface.validator
時纔得到Java異常。
public class MyClass {
public MyInterface myInterface;
public int test;
public MyClass (int INT, String STRING, MyInterface myInterface) {
this.myInterface = myInterface;
this.test = INT;
myInterface.validator(INT, STRING);
}
}
public interface MyInterface {
public Boolean validator(int INT, String STRING);
}
public class MyClassTest {
MyInterface mockMyInterface;
MyClass myClass;
@Before
public void setUp() throws Exception {
mockMyInterface = createMock(MyInterface.class);
}
@Test
public void test() {
myClass = new MyClass(10, "Test", mockMyInterface);
expect(mockMyInterface.validator(10, "Test")).andStubReturn(true);
replay(mockMyInterface);
assertEquals(myClass.test, 10);
verify(mockMyInterface);
}
}