如何使用反射访问 JUnit 中的私有 Map?

发布于 2024-12-15 17:46:21 字数 580 浏览 1 评论 0原文

我有一个连接器类(SVNConnector),应该进行junit测试。有一个名为 private Map的私有地图; connectionMap 应该在 JUnit 中访问,但该映射没有 getter 方法。所以我必须使用反射来做到这一点。我的问题是:这是如何运作的?我尝试了以下操作:

@BeforeClass
public static void setUpBeforeClass() throws Exception {
    svnConnector = new SVNConnector(user, pwd);
    Field connectionMapField = SVNConnector.class.getDeclaredField("connectionMap");
    connectionMapField.setAccessible(true);
//AND NOW?
}

没有任何 Collection 特定的 getter 或 setter 来检查 collectionMap 或类似的大小。那么我怎样才能访问它呢?

谢谢。

I have a connector class (SVNConnector) which should be junit tested. There is a private map called private Map<String, SVNRepository> connectionMap which should be accessed in JUnit, but this map doesn't have a getter method. So I have to use reflections to do that. My question is: How does that work? I tried the following:

@BeforeClass
public static void setUpBeforeClass() throws Exception {
    svnConnector = new SVNConnector(user, pwd);
    Field connectionMapField = SVNConnector.class.getDeclaredField("connectionMap");
    connectionMapField.setAccessible(true);
//AND NOW?
}

There are not any Collection specific getters or setters to check the size of the collectionMap or similar. So how can I access it?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

话少心凉 2024-12-22 17:46:21

你的意思是你想要获取该字段的值吗?

Map<String, SVNRepository> connectionMap = 
    (Map<String, SVNRepository>) connectionMapField.get(svnConnector);

Do you mean you want to get the value of the field?

Map<String, SVNRepository> connectionMap = 
    (Map<String, SVNRepository>) connectionMapField.get(svnConnector);
粉红×色少女 2024-12-22 17:46:21

查看 JUnit 插件。

特别是 - http://junit-addons.sourceforge.net/junitx/util/PrivateAccessor .html

主页在这里 - http://junit-addons.sourceforge.net/

例如:

PrivateAccessor.setField(myClassInstance, "privateMapName", myMap);

Check out JUnit-addons.

Particularly - http://junit-addons.sourceforge.net/junitx/util/PrivateAccessor.html

With the main page here - http://junit-addons.sourceforge.net/

For example:

PrivateAccessor.setField(myClassInstance, "privateMapName", myMap);
も让我眼熟你 2024-12-22 17:46:21

从字段获取引用(通过使用 Field.get(Object) 方法),将其转换为 Map,然后使用它。

Map connectionMap = (Map) connectionMapField.get(svnConnector);

int size = connectionMap.getSize();

您可能还应该看看 Java 反射教程

Obtain the reference form the field (by using the Field.get(Object) method), cast it to Map and then use it.

Map connectionMap = (Map) connectionMapField.get(svnConnector);

int size = connectionMap.getSize();

You may also should have a look at the Java Reflection Tutorial.

Oo萌小芽oO 2024-12-22 17:46:21

如果您喜欢使用其他库(和注释处理器),您可以使用 dp4j@TestPrivates 注解:

    @TestPrivates
    @BeforeClass
public static void setUpBeforeClass() throws Exception {
    svnConnector = new SVNConnector(user, pwd);
    Map<String, SVNRepository> connectionMap = svnConnector.connectionMap; 
/* dp4j will replace this plain access the equivalent Reflection API code at compile-time */
    }

您还可以使用-Averbose选项查看生成的Reflection代码,复制粘贴它,然后删除@TestPrivates

Had you liked to use additional libraries (and annotation processors) you could have used dp4j's @TestPrivates annotation:

    @TestPrivates
    @BeforeClass
public static void setUpBeforeClass() throws Exception {
    svnConnector = new SVNConnector(user, pwd);
    Map<String, SVNRepository> connectionMap = svnConnector.connectionMap; 
/* dp4j will replace this plain access the equivalent Reflection API code at compile-time */
    }

You can also use the -Averbose option to see the generated Reflection code, copy-paste it, and then remove @TestPrivates

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文