相关疑难解决方法(0)

用Java模拟文件 - 模拟内容 - Mockito

我很嘲笑,我一直在尝试模拟实际内容(本质上只在内存中创建一个虚拟文件),这样就不会在任何时候将数据写入磁盘.

我已经尝试过像模拟文件和模拟尽可能多的我可以解决的许多属性的解决方案,然后用文件编写器/缓冲区写入器写入它,但是那些不能正常工作,因为它们需要规范路径.有人找到了这个或类似的解决方案,但我接近这个错误?

我一直在这样做:

private void mocking(){
    File badHTML = mock(File.class);
    //setting the properties of badHTML
    when(badHTML.canExecute()).thenReturn(Boolean.FALSE);
    when(badHTML.canRead()).thenReturn(Boolean.TRUE);
    when(badHTML.canWrite()).thenReturn(Boolean.TRUE);
    when(badHTML.compareTo(badHTML)).thenReturn(Integer.SIZE);
    when(badHTML.delete()).thenReturn(Boolean.FALSE);
    when(badHTML.getFreeSpace()).thenReturn(0l);
    when(badHTML.getName()).thenReturn("bad.html");
    when(badHTML.getParent()).thenReturn(null);
    when(badHTML.getPath()).thenReturn("bad.html");
    when(badHTML.getParentFile()).thenReturn(null);
    when(badHTML.getTotalSpace()).thenReturn(0l);
    when(badHTML.isAbsolute()).thenReturn(Boolean.FALSE);
    when(badHTML.isDirectory()).thenReturn(Boolean.FALSE);
    when(badHTML.isFile()).thenReturn(Boolean.TRUE);
    when(badHTML.isHidden()).thenReturn(Boolean.FALSE);
    when(badHTML.lastModified()).thenReturn(System.currentTimeMillis());
    when(badHTML.mkdir()).thenReturn(Boolean.FALSE);
    when(badHTML.mkdirs()).thenReturn(Boolean.FALSE);
    when(badHTML.setReadOnly()).thenReturn(Boolean.FALSE);
    when(badHTML.setExecutable(true)).thenReturn(Boolean.FALSE);
    when(badHTML.setExecutable(false)).thenReturn(Boolean.TRUE);
    when(badHTML.setReadOnly()).thenReturn(Boolean.FALSE);

    try {
        BufferedWriter bw = new BufferedWriter(new FileWriter(badHTML));
        /*
          badHTMLText is a string with the contents i want to put into the file, 
          can be just about whatever you want
         */
        bw.append(badHTMLText);
        bw.close();

    } catch (IOException ex) {
        System.err.println(ex);
    }
}
Run Code Online (Sandbox Code Playgroud)

任何想法或指导都会非常有帮助.在此之后的某个地方我基本上尝试使用另一个类从文件中读取.我会尝试模拟某种输入流,但另一类不接受输入流,因为它是项目的io处理类.

java file mockito

28
推荐指数
2
解决办法
7万
查看次数

构造函数中的逻辑?

下面的代码工作正常,但我想知道在对象创建时间是否有任何问题.

import java.util.Scanner;

public class FactorialExample {
    public FactorialExample(int n) {
        int fact=1;
        for(int i=1;i<=n;i++) {
              fact=fact*i;  
        }
        System.out.println("the factorial of a given number is::"+fact);        
    }
            public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter any Integer Value:");
        int value=sc.nextInt();
        FactorialExample fe=new FactorialExample(value);

    }
}
Run Code Online (Sandbox Code Playgroud)

java

4
推荐指数
1
解决办法
4621
查看次数

标签 统计

java ×2

file ×1

mockito ×1