Java- A class 有一个注解,如何在没有那个注解的情况下测试这个 class
Java- A class has an annotation, how to test this class without that annotation
示例:
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();
}
示例:
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();
}