我正在嘗試使用Mockito來模擬JUnit測試的方法。該方法從simple-json獲取JSONObject作爲參數,並用JSONObject進行響應。我試圖模擬的方法來自另一個類,我正在測試的代碼調用它。我似乎無法讓Mockito趕上我的請求並作出相應的迴應。我完全錯過了什麼嗎?以JSONObject作爲Mockito模擬方法中的參數
public class TestClass{
JSONObject jsonRequest;
JSONObject jsonReturn;
AnotherClass anotherClass = Mockito.mock(AnotherClass.class);
@Before
public void setUp(){
jsonRequest = this.readJSONFromFile("jsonRequest.json");
jsonReturn = this.readJSONFromFile("jsonReturn.json");
Mockito.when(anotherClass.anotherClassMethod(jsonRequest)).thenReturn(jsonReturn);
}
@Test
public void testMethod(){
TestClass testClass = new TestClass();
assertEquals(testClass.method(jsonRequest), jsonReturn);
}
}
所以我試圖寫出一個例子,並失敗了。我現在已經對我的例子進行了修改並將其付諸實踐: – JimBob91