有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

单元测试Java类有注释,如何在没有注释的情况下测试这个类

EAX示例:

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行


共 (1) 个答案

  1. # 1 楼答案

    只有当存在扩展class A的子类时,您的场景才有意义。所以要测试它,您可以创建一个子类并测试它

    @Test(expected = IllegalStateException.class)
    public void getSchema() {
        A testClass = new A() {};
        testClass.getSchema();
    }