java 使用 JMockit 模拟私有静态字段?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17674585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 14:38:02  来源:igfitidea点击:

Mock a private static field with JMockit?

javaunit-testingjmockit

提问by user2586917

I have a class like the following;

我有一个如下所示的课程;

class ClassA {
    private static File myDir;

    // myDir is created at some stage

    private static String findFile(final String fileName) {
       for (final String actualBackupFileName : myDir.list()) {
           if (actualBackupFileName.startsWith(removeExtensionFrom(backupFile))) {
               return actualBackupFileName;
            }
       }
    }
}

So, basically, I want to test this class by mocking out the File class so that when list() is called on it it returns a list of strings I define in my test class.

所以,基本上,我想通过模拟 File 类来测试这个类,以便在调用 list() 时它返回我在测试类中定义的字符串列表。

I've got the following but its not working at the minute, there's probably something obvious I'm doing wrong - I'm new to JMockit - any help is much appreciated!

我有以下内容,但目前无法正常工作,我可能做错了一些明显的事情 - 我是 JMockit 的新手 - 非常感谢任何帮助!

@Mocked("list") File myDir;

@Test
  public void testClassA() {
    final String[] files = {"file1-bla.txt"};

    new NonStrictExpectations() {{
      new File(anyString).list(); 
      returns(files);
   }};

   String returnedFileName = Deencapsulation.invoke(ClassA.class, "findFile","file1.txt");

   // assert returnedFileName is equal to "file1-bla.txt"
  }

When running the above test I get a NullPointerException for the myDir field in ClassA - so it looks like its not getting mocked properly?

运行上述测试时,我得到 ClassA 中 myDir 字段的 NullPointerException - 所以看起来它没有被正确模拟?

回答by Martin Lane

You can use the setField method from the Deencapsulation class. Note example below:

您可以使用 Deencapsulation 类中的 setField 方法。请注意以下示例:

Deencapsulation.setField(ClassA, "File", your_desired_value);

回答by Rogério

JMockit (or any other mocking tool) does not mock fields or variables, it mocks types(classes, interfaces, etc.) Where the instances of those types get stored inside the code under test is not relevant.

JMockit(或任何其他模拟工具)不模拟字段或变量,它模拟类型(类、接口等)。这些类型的实例存储在被测代码中的情况与此无关。

Example test for ClassA:

示例测试ClassA

@Test
public void testClassA(@Mocked File myDir)
{
    new Expectations() {{ myDir.list(); result = "file1-bla.txt"; }};

    String returnedFileName = new ClassA().publicMethodThatCallsFindFile("file1.txt");

    assertEquals("file1-bla.txt", returnedFileName);
}

The above should work. Note that testing privatemethods directly (or accessing privatefields) is considered bad practice, so I avoided it here. Also, it's best to avoid mocking the Fileclass. Instead, test only your publicmethods, and use actual files instead of mocking the filesystem.

以上应该工作。请注意,private直接测试方法(或访问private字段)被认为是不好的做法,所以我在这里避免了它。此外,最好避免嘲笑File课程。相反,只测试您的public方法,并使用实际文件而不是模拟文件系统。

回答by jdevelop

try out this:

试试这个:

new Expectations {{
  invoke(File.class, "list", null, null);
  returns(files);
}}