我有這樣的代碼,作業正常。
public class MyClass implements Serializable {
...
private UploadedFile uploadedFile;
public UploadedFile getUploadedFile() {
return uploadedFile;
}
public void setUploadedFile(UploadedFile uploadedFile) {
this.uploadFile = uploadedFile;
}
public void fileRead(){
...
Scanner scanner = new Scanner(uploadFile.getInputStream())。
while (scanner.hasNextLine() ) { ...}
...
}
現在我想為它創建一個測驗。 問題是,當我運行下面的測驗代碼時,當它到達Scanner行時,uploadFile是空的。 為了使uploadFile在Scanner行中不為空,我需要在@Test方法中做什么修改?
@Test
public void emptyFileTest() throws Exception {
...
UploadedFile uploadedFile = Mockito.mock(UploadedFile.class)。
PowerMockito.doReturn(new ByteArrayInputStream("".getBytes()).當(uploadedFile).getInputStream()。
this.myClass.fileRead()。
...
我也試了一下,但結果是一樣的。
我也試了一下,但結果是一樣的。
PowerMockito.when(uploadFile.getInputStream()).thenReturn(new ByteArrayInputStream("".getBytes()) )。
uj5u.com熱心網友回復:
看起來你沒有在myClass中設定uploadedFile這個變數
。PowerMockito.doReturn(new ByteArrayInputStream("".getBytes())).when(uploadFile).getInputStream();
你可以更容易地做同樣的事情。new ByteArrayInputStream(new byte[0])。沒有必要用一個字串來創建一個空的位元組陣列。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/328120.html
標籤:
上一篇:對使用類或結構作為靜態變數的容器感到困惑(Swift)。
下一篇:如何使用AnjlabgetPurchaseHistory()方法獲取購買歷史記錄AndroidInAppBilling
