Java Guava @VisibleForTesting :帮我举个完整的例子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24051476/
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-08-14 09:53:46  来源:igfitidea点击:

Guava @VisibleForTesting : Help me with a complete example

javamavenunit-testingguava

提问by user3709525

My intent is to do unit test of private methods and I understand on how to import the @VisibleForTestingand use it for a private method. I have done a quite a bit of search but unable to see a complete example that demonstrates this feature.

我的目的是对私有方法进行单元测试,我了解如何导入@VisibleForTesting并将其用于私有方法。我进行了大量搜索,但无法看到演示此功能的完整示例。

For eg:

例如:

class MyClass {
    @VisibleForTesting 
    private double[] getWorkArray(double[] values,int length) {
               :
               :
        return <some double array> 
    }
}

Now in JUnit, I must be able to do

现在在 JUnit 中,我必须能够做到

@Test
public void testProvateMethod() {
    MyClass object = new MyClass();
    assertNotNull(object.getWorkArray(...);
}

But the difficult part is I am unable to comprehend/do the following a) Snippet of maven compiler plugin for including the relevant annotation processor b) Actually be able to test a private method. (since it throws error related to visibility of method)

但困难的部分是我无法理解/执行以下操作 a) 用于包含相关注释处理器的 maven 编译器插件的片段 b) 实际上能够测试私有方法。(因为它会引发与方法可见性相关的错误)

I am unable to do it in action while I write a test in JUnit (due to the private access error). For eg: mvn clean test

在 JUnit 中编写测试时,我无法执行此操作(由于私有访问错误)。例如:mvn clean test

Please provide a complete example to really all steps involved in getting the JUnit test of private methods done.

请提供一个完整的示例,说明完成私有方法的 JUnit 测试所涉及的所有步骤。

回答by u290629

Firstly, I do notrecommend to test private methods, unit tests should test public methods in most cases. If you have to test private methods, it usually indicates a bad design.

首先,我建议测试私有方法,单元测试在大多数情况下应该测试公共方法。如果您必须测试私有方法,通常表明设计很糟糕。

Regarding to @VisibleForTesting, it is used in package-methods in Guava, and not part of JUnit API. The annotation is just a tag to indicate the method can be tested, it even doesn't be loaded in JVM. So if you need to test non-public methods, make the methods package scopewhich is visible to unit test classes in same package.

关于 to @VisibleForTesting,它用于 Guava 中的 package-methods,而不是 JUnit API 的一部分。注解只是一个标记,表明该方法可以被测试,它甚至不会被加载到 JVM 中。因此,如果您需要测试非公共方法,请将方法包范围设为对同一包中的单元测试类可见。

Last, by using reflect can access private methods, if you really have to test them.

最后,通过使用reflect可以访问私有方法,如果你真的需要测试它们。

回答by coderz

You can remove privatekeyword:

您可以删除private关键字:

class MyClass{
      @VisibleForTesting double[] getWorkArray(double[] values,int length) {
                 :
                 :
           return <some double array> 
       }
}

Then you are able to:

然后你就可以:

MyClass object = new MyClass();
assertNotNull(object.getWorkArray(...);

in your test.

在你的测试中。

回答by Dark

Testing a private method must be one of the bad patterns. However, there are times when you often feel the urge to test private methods. In this case, I personally use ReflectionTestUtils to test the method. This is because we wanted to keep the original intent of the private method, and only test the method. Below is an example of my sample.

测试私有方法必须是不良模式之一。但是,有时您经常会想要测试私有方法。在这种情况下,我个人使用 ReflectionTestUtils 来测试该方法。这是因为我们想保持私有方法的初衷,只测试方法。下面是我的样本的一个例子。

MyClass myClass = new MyClass();

ReflectionTestUtils.invokeMethod(myClass, "getWorkArray", values, length);

One drawback is the fact that I get the name of the method as a String and it is quite a bit sad except for the fact that refactoring does not convert correctly in IDEA.

一个缺点是我将方法的名称作为字符串获取这一事实,除了重构无法在 IDEA 中正确转换这一事实之外,这有点令人难过。

I hope it helps.

我希望它有帮助。

Thanks.

谢谢。