模拟助手类方法并返回布尔值

时间:2019-10-01 20:03:49

标签: java junit mockito junit5

我的主要方法如下,我的FileReadHelper类包含静态方法isFileExistsAndNotEmpty(arg1,arg2)。

public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {

        if (args != null && args.length == 2) {
            File firstFile = new File(args[0]);
            File secondFile = new File(args[1]);
            boolean isFileExists = FileReadHelper.isFileExistsAndNotEmpty(firstFile, secondFile);
            if (isFileExists) {
                splitAndReadProcess(firstFile, secondFile);
            }

        } else {
            // TODO
        }
    }

我将测试用例编写如下,

@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
public class FileReadApplicationTest {

    @Mock
    private FileReadHelper fileReadHelper;

    @Test
    void checkWordCountSplitAndReadProcess() throws InterruptedException, ExecutionException, IOException {
        String [] fileNames = {"firstFile.txt","secondFile.txt"};
        when(fileReadHelper.isFileExistsAndNotEmpty(new File(fileNames[0]), new File(fileNames[1]))).thenReturn(true);
        FileReadApplication.main(fileNames);
    }
}

我试图调用并获取以下异常,

org.mockito.exceptions.misusing.MissingMethodInvocationException: 
when() requires an argument which has to be 'a method call on a mock'.
For example:
    when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
   Those methods *cannot* be stubbed/verified.
   Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other object.

我不能直接调用诸如,

when(FileReadHelper.isFileExistsAndNotEmpty(new File(fileNames[0]), new File(fileNames[1]))).thenReturn(true);

在这种情况下,如何返回布尔值,并确保流程继续进行?

0 个答案:

没有答案