强制FileNotFoundException

mag*_*ter 1 java testing junit filenotfoundexception ioexception

我正在为一段代码编写测试,其中包含一个IOException catch,我试图覆盖它.try/catch看起来像这样:

try {
    oos = new ObjectOutputStream(new FileOutputStream(cacheFileName));
} catch (IOException e) {
    LOGGER.error("Bad news!", e);
} finally {
Run Code Online (Sandbox Code Playgroud)

最简单的方法似乎是让FileOutputStream抛出一个FileNotFoundException,但也许我会以错误的方式解决这个问题.

有人在那里有任何提示吗?

Kev*_*ent 5

您可以设置cacheFileName为无效名称或您知道不存在的名称.