用于测试私有方法的Java工具? [英] Java tool for testing private methods?

查看:138
本文介绍了用于测试私有方法的Java工具?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对私有方法测试的意义有不同意见,例如,此处此处。我个人认为这是有道理的,问题是如何正确地做到这一点。
在C ++中,您可以使用 #define hack 或制作测试类朋友,在C#中有 InternalsVisibleToAttribute ,但在Java中,我们要么必须使用反射或使它们可见于测试和注释它们以使意图明确。两者的缺点应该非常明确。

There are different opinions on the meaningfulness of testing of private methods, e.g., here and here. I personally think it makes sense, the question is how to do it properly. In C++ you can use a #define hack or make the test class friend, in C# there's the InternalsVisibleToAttribute, but in Java we either have to use reflection or to make them "visible for testing" and annotate them as such in order to make the intent clear. The disadvantages of both should be quite clear.

我认为应该有更好的东西。从

I think there should be something better. Starting with

public class Something {
    private int internalSecret() {
        return 43;
    }
}

能够调用私有方法会很高兴在测试代​​码中

it would be nice to be able to call private methods in the test code like

@MakeVisibleForTesting Something something = new Something();
Assert.assertEquals(43, something.internalSecret());

这里的注释会默默地将所有调用转换为的私有方法使用反射。我想知道 Lombok 是否可以这样做(并会询问作者)。

Here the annotation would silently convert all calls to private methods of something using reflection. I wonder if Lombok could do it (and will ask the authors).

很有可能做那么多魔术证明太复杂了,无论如何都需要一些时间,所以我正在寻找一些替代方案。也许用 @Decapsulate 之类的东西来注释正在测试的类,并使用注释处理器生成一个类 Decapsulated_Something 看起来像

It's quite possible that doing that much magic proves too complicated, and in any case it'll take some time, so I'm looking for some alternative. Maybe annotating the class under test with something like @Decapsulate and using an annotation processor to generate a class Decapsulated_Something looking like

public class Decapsulated_Something {
    public Decapsulated_Something(Something delegate) {
        this.delegate = delegate
    }
    public boolean internalSecret() {
        // call "delegate.internalSecret()" using reflection
    }
    ...
}

允许使用

Decapsulated_Something something = new Decapsulated_Something(new Something());
Assert.assertEquals(43, something.internalSecret());

我对注释处理没有多少经验,所以我先问一下:

I don't have much experience with annotation processing, so I ask first here:


  • 实施这有多复杂?

  • 我忘记了什么?

  • 您对此有何看法?

推荐答案


能够在测试代码中调用私有方法会很高兴,比如

it would be nice to be able to call private methods in the test code like

@MakeVisibleForTesting Something something = new Something();
Assert.assertEquals(43, something.internalSecret());


有一个方法注释,请查看 dp4j @TestPrivates

There's such thing as a method annotation, check out dp4j's @TestPrivates:

@Test
@TestPrivates 
//since the method is annotated with JUnit's @Test this annotation is redundant. 
// You just need to have dp4j on the classpath.
    public void somethingTest(){
      Something something = new Something();
      int sthSecret = something.internalSecret();
       Assert.assertEquals(43, sthSecret); //cannot use something.internalSecret() directly because of bug [dp4j-13][2] 
    }

这篇关于用于测试私有方法的Java工具?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆