java - Java-一个类有一个注解,如何在没有该注解的情况下测试这个类
问题描述
示例:
1 @Schema // you don't need to care what Schema annotation is
2 public class A {
3 public Schema getSchema() {
4 Schema schema = getClass().getAnnotation(Schema.class);
5 if (schema == null) {
6 throw new IllegalStateException("Schema annotation is missing for class " + getClass().getName());
7 }
8 return schema;
9 }
10 }
如何编写可以覆盖第 6 行的单元测试。现在我只能覆盖第 4、5、8 行
解决方案
只有当有扩展的子类时,您的方案才有意义class A
。因此,要对其进行测试,您可以创建一个子类并对其进行测试。
@Test(expected = IllegalStateException.class)
public void getSchema() {
A testClass = new A() {};
testClass.getSchema();
}
推荐阅读
- assembly - 汇编 - 隐式计算会更改操作码吗?
- python - 如何将大于 9 的数字传递给 psycopg2 选择语句
- python - 具有不必要特征的图像中的细胞计数
- c++ - 将对 main() 中的局部变量的引用传递给其他线程/函数是不好的做法吗?
- api - 如何从 API 检索沃尔玛产品
- api - 无法使用邮递员创建史诗
- python - 如果我已经打乱了一个数组`a`来获得一个数组`b`,我怎样才能获得一个索引数组'`i`使得`b [i] == a`?
- python-3.x - 我们如何从多字典创建 pyspark 数据框
- wordpress - WooCommerce/Elementor 分页更改整个页面
- javascript - 如何将项目添加到承诺之外的数组