如何使用VisibleForTesting进行纯JUnit测试 [英] How to use VisibleForTesting for pure JUnit tests

查看:2019
本文介绍了如何使用VisibleForTesting进行纯JUnit测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对项目上的纯Java文件运行纯JUnit4 Java测试,但是我找不到使用

I´m running pure JUnit4 java tests over my pure java files on my project but I can't find a way to use @VisibleForTesting clearly without making the thing manually public.

例如:

@VisibleForTesting
public Address getAddress() {
  return mAddress;
}

该方法必须为public才能使其对测试公开",但是在那种情况下,注释没有意义吗?如果批注什么都不做,为什么不只使用注释呢?

The method has to be public to let it be "public" to tests, but in that case the annotation doesn't make sense right? why not just use a comment if the annotation will not do nothing?

推荐答案

根据Android文档:

According to Android docs:

您可以选择指定可见性,如果不进行测试,应该是什么?这样,工具就可以从生产代码中捕获意外访问.

You can optionally specify what the visibility should have been if not for testing; this allows tools to catch unintended access from within production code.

示例:

@VisibleForTesting(否则= VisibleForTesting.PRIVATE) 公共地址getAddress()

@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) public Address getAddress()

这篇关于如何使用VisibleForTesting进行纯JUnit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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