gpt4 book ai didi

java - 如何使用 junit 在 java 中测试注解

转载 作者:行者123 更新时间:2023-11-29 04:55:17 24 4
gpt4 key购买 nike

类有

@Info(name = "ABC", version = "1.1.1")
public class Test implements TestPlugin{
}

其中@Info是@interface注解

我想写junit测试用例来验证它的@info名称和版本,我已经通过了。

最佳答案

你可以为此使用反射:

Annotation annotation = Test.class.getAnnotation(Info.class);

if(annotation instanceof Info){
Info info = (Info) annotation;

assertEquals("ABC", info.name());
assertEquals("1.1.1", info.version());
} else {
fail("Did not find annotation");
}

关于java - 如何使用 junit 在 java 中测试注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34018969/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com