此时缺少对模拟类型的调用;

时间:2014-07-31 00:50:18

标签: unit-testing jmockit injectable

我是jMockit的新手。我试图在方法中模拟java.io.File类型的多个实例。有一些地方,我不应该模拟文件对象。出于这个原因,我正在使用@Injectable。它抛出了以下异常。

我不想模拟java.io.File的所有实例。我希望从这些方法返回的实例是实际的文件。

以下是测试类。

/**
 * 
 */
package org.iis.uafdataloader.tasklet;

import static org.junit.Assert.fail;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.regex.Pattern;

import mockit.Expectations;
import mockit.Injectable;
import mockit.Mocked;
import mockit.NonStrictExpectations;
import mockit.VerificationsInOrder;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.RegexFileFilter;
import org.iis.uafdataloader.tasklet.validation.FileNotFoundException;
import org.junit.Test;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.repeat.RepeatStatus;

/**
 * @author K23883
 *
 */
public class FileMovingTaskletTest {

    private FileMovingTasklet fileMovingTasklet;

    @Mocked
    private StepContribution contribution;

    @Mocked
    private ChunkContext chunkContext;



    /**
     * Test method for
     * {@link org.iis.uafdataloader.tasklet.FileMovingTasklet#execute(org.springframework.batch.core.StepContribution, org.springframework.batch.core.scope.context.ChunkContext)}
     * .
     * 
     * @throws Exception
     */
    @Test
    public void testExecuteWhenWorkingDirDoesNotExist(
//          @Mocked final File file,
            @Injectable final File sourceDirectory,
            @Injectable final File workingDirectory,
            @Injectable final File archiveDirectory,
            @Mocked final RegexFileFilter regexFileFilter,
            @Mocked final FileUtils fileUtils) throws Exception {

        fileMovingTasklet = new FileMovingTasklet();
        fileMovingTasklet.setSourceDirectoryPath("sourceDirectoryPath");
        fileMovingTasklet.setInFileRegexPattern("inFileRegexPattern");
        fileMovingTasklet.setArchiveDirectoryPath("archiveDirectoryPath");
        fileMovingTasklet.setWorkingDirectoryPath("workingDirectoryPath");



        final File[] sourceDirectoryFiles = new File[] {
                new File("sourceDirectoryPath/ISGUAFFILE.D140728.C00"),
                new File("sourceDirectoryPath/ISGUAFFILE.D140729.C00") };

        final File[] workingDirectoryFiles = new File[] {
                new File("workingDirectoryPath/ISGUAFFILE.D140728.C00"),
                new File("workingDirectoryPath/ISGUAFFILE.D140729.C00") };




        new NonStrictExpectations(){{

                new File("sourceDirectoryPath");
                result = sourceDirectory;

                sourceDirectory.exists();
                result = true;

                sourceDirectory.isDirectory();
                result = true;

                // workingDirectory =
                new File("workingDirectoryPath");
                result = workingDirectory;

                workingDirectory.exists();
                result = false;

                workingDirectory.mkdirs();

                FileUtils.cleanDirectory(onInstance(workingDirectory));

                FilenameFilter fileNameFilter = new RegexFileFilter(anyString,
                        Pattern.CASE_INSENSITIVE);

                sourceDirectory.listFiles(fileNameFilter);
                result = sourceDirectoryFiles;

                System.out.println("sourceDirectoryFile :"
                        + ((File[]) sourceDirectoryFiles).length);

                // for (int i = 0; i < sourceDirectoryFiles.length; i++) {
                // FileUtils.moveFileToDirectory(sourceDirectoryFiles[i],
                // workingDirectory, true);
                // }

                // archiveDirectory =
                new File("archiveDirectoryPath");
                result = archiveDirectory;

                workingDirectory.listFiles();
                result = workingDirectoryFiles;

                // for (int i = 0; i < workingDirectoryFiles.length; i++) {
                // FileUtils.copyFileToDirectory(workingDirectoryFiles[i],
                // archiveDirectory);
                // }

            }};


            RepeatStatus status = fileMovingTasklet.execute(contribution,
                    chunkContext);
            assert (status == RepeatStatus.FINISHED);


        new VerificationsInOrder() {{

            sourceDirectory.exists();
            onInstance(sourceDirectory).isDirectory();
            onInstance(workingDirectory).exists();

            onInstance(workingDirectory).mkdirs();

            onInstance(sourceDirectory).listFiles((FilenameFilter)any);

            FileUtils.moveFileToDirectory((File)any, onInstance(workingDirectory), true); 
            times = 2;

            FileUtils.copyFileToDirectory((File)any, onInstance(archiveDirectory));
            times= 2;

        }};

    }

}

以下是实际实施方法

/*
 * (non-Javadoc)
 * 
 * @see org.springframework.batch.core.step.tasklet.Tasklet#execute(org.
 * springframework.batch.core.StepContribution,
 * org.springframework.batch.core.scope.context.ChunkContext)
 */
@Override
public RepeatStatus execute(StepContribution contribution,
        ChunkContext chunkContext) throws Exception {

    File sourceDirectory = new File(sourceDirectoryPath);

    if (sourceDirectory == null || !sourceDirectory.exists()
            || !sourceDirectory.isDirectory()) {
        throw new FileNotFoundException("The source directory '"
                + sourceDirectoryPath
                + "' doesn't exist or can't be read or not a directory");
    }


    File workingDirectory = new File(workingDirectoryPath);

    if (workingDirectory != null && !workingDirectory.exists() ) {
        workingDirectory.mkdirs();

    }

    FileUtils.cleanDirectory(workingDirectory);

    FilenameFilter fileFilter = new RegexFileFilter(inFileRegexPattern,
            Pattern.CASE_INSENSITIVE);

    File[] sourceDirectoryFiles = sourceDirectory.listFiles(fileFilter);

    System.out.println("sourceDirectoryFiles : " + sourceDirectoryFiles.length);

    for (File file : sourceDirectoryFiles) {
        FileUtils.moveFileToDirectory(file, workingDirectory, true);
    }

    File archiveDirectory =  new File(archiveDirectoryPath);

    for (File file : workingDirectory.listFiles()) {
        FileUtils.copyFileToDirectory(file, archiveDirectory);
    }

    return RepeatStatus.FINISHED;
}

以下是堆栈跟踪。

java.lang.IllegalStateException: Missing invocation to mocked type at this point; please make sure such invocations appear only after the declaration of a suitable mock field or parameter
    at org.iis.uafdataloader.tasklet.FileMovingTaskletTest$1.<init>(FileMovingTaskletTest.java:75)
    at org.iis.uafdataloader.tasklet.FileMovingTaskletTest.testExecuteWhenWorkingDirDoesNotExist(FileMovingTaskletTest.java:71)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

请帮助我解决问题。

1 个答案:

答案 0 :(得分:2)

@Injectable为您提供了一个模拟实例;它不会影响模拟类型的其他实例。因此,当测试尝试记录new File("sourceDirectoryPath")时,它会说“此时缺少对模拟类型的调用”,正是因为File(String) 而不是被模拟。

要模拟整个File类(包括其构造函数)以便所有实例都受到影响,您需要使用@Mocked,如下例所示:

@Test
public void mockFutureFileObjects(@Mocked File anyFile) throws Exception
{
    final String srcDirPath = "sourceDir";
    final String wrkDirPath = "workingDir";

    new NonStrictExpectations() {{
        File srcDir = new File(srcDirPath);
        srcDir.exists(); result = true;
        srcDir.isDirectory(); result = true;

        File wrkDir = new File(wrkDirPath);
        wrkDir.exists(); result = true;
    }};

    sut.execute(srcDirPath, wrkDirPath);
}

JMockit教程describes the same mechanism,虽然语法略有不同。

这就是说,我建议改为用真实的文件和目录编写测试。