是否可以使用PowerMock来模拟新文件的创建?

joh*_*ode 9 java powermock

我想介绍一个逻辑,用单元测试创​​建文件.是否可以模拟File类并避免实际创建文件?

cah*_*hen 7

模拟构造函数,就像在这个示例代码中一样.不要忘记@PrepareForTest中放置将调用"new File(...)"的

package hello.easymock.constructor;

import java.io.File;

import org.easymock.EasyMock;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest({File.class})
public class ConstructorExampleTest {

    @Test
    public void testMockFile() throws Exception {

        // first, create a mock for File
        final File fileMock = EasyMock.createMock(File.class);
        EasyMock.expect(fileMock.getAbsolutePath()).andReturn("/my/fake/file/path");
        EasyMock.replay(fileMock);

        // then return the mocked object if the constructor is invoked
        Class<?>[] parameterTypes = new Class[] { String.class };
        PowerMock.expectNew(File.class, parameterTypes , EasyMock.isA(String.class)).andReturn(fileMock);
        PowerMock.replay(File.class); 

        // try constructing a real File and check if the mock kicked in
        final String mockedFilePath = new File("/real/path/for/file").getAbsolutePath();
        Assert.assertEquals("/my/fake/file/path", mockedFilePath);
    }

}
Run Code Online (Sandbox Code Playgroud)


Nil*_*lsH 6

我不确定是否可能,但我有这样的要求,我通过创建一个FileService界面来解决它.因此,不是直接创建/访问文件,而是添加抽象.然后,您可以在测试中轻松模拟此界面.

例如:

public interface FileService {

    InputStream openFile(String path);
    OutputStream createFile(String path);

}
Run Code Online (Sandbox Code Playgroud)

然后在你的班级使用这个:

public class MyClass {

    private FileService fileService;

    public MyClass(FileService fileService) {
        this.fileService = fileService;
    }

    public void doSomething() {
        // Instead of creating file, use file service
        OutputStream out = fileService.createFile(myFileName);
    }
}
Run Code Online (Sandbox Code Playgroud)

在你的测试中

@Test
public void testOperationThatCreatesFile() {
    MyClass myClass = new MyClass(mockFileService);
    // Do your tests
}
Run Code Online (Sandbox Code Playgroud)

这样,您甚至可以在没有任何模拟库的情况下模拟它.


Ziy*_*iya 6

试试PowerMockito

import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;


@PrepareForTest(YourUtilityClassWhereFileIsCreated.class)
public class TestClass {

 @Test
 public void myTestMethod() {
   File myFile = PowerMockito.mock(File.class);
   PowerMockito.whenNew(File.class).withAnyArguments().thenReturn(myFile);
   Mockito.when(myFile.createNewFile()).thenReturn(true);
 }

}
Run Code Online (Sandbox Code Playgroud)