gpt4 book ai didi

java - 如何使用注释扩展让 spock 在运行时执行不同的方法?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:32:22 26 4
gpt4 key购买 nike

首先,如果有更简单的方法来解决这个问题,这里是我要完成的事情的概述。我想使用 KnownIssue 注释(扩展 AbstractAnnotationDrivenExtension)来注释我的测试方法,该注释将缺陷 ID 作为参数并在执行测试之前检查缺陷的状态。如果缺陷已修复,它将继续执行,如果未修复,我希望它忽略测试,但如果它已关闭或删除,我想通过日志记录说明应该删除或更新测试来引发测试失败并且注释已删除,因为缺陷现已关闭或删除。

在引发测试失败之前,我已经做好了一切准备工作。我尝试过的方法不起作用:

  • 在 visitFeatureAnnotation 方法中抛出异常,导致失败,导致此后所有测试无法执行。
  • 创建一个扩展 Spec 的类并包含一个记录消息但失败的测试方法,然后尝试使用 feature.featureMethod.setReflection() 将要执行的方法设置为另一个方法。在这种情况下,我得到一个 java.lang.IllegalArgumentException:对象不是声明类的实例
  • 然后我尝试使用 ExpandoMetaClass 将一个方法直接添加到 declaringClass,并将 feature.featureMethod.setReflection 指向它,但我仍然得到相同的 IllegalArgumentException。

这是我最近尝试的 visitFeatureAnnotation 方法中的内容:

def myMetaClass = feature.getFeatureMethod().getReflection().declaringClass.metaClass
myMetaClass.KnownIssueMethod = { -> return false }
feature.featureMethod.setReflection(myMetaClass.methods[0].getDoCall().getCachedMethod());

关于我如何实现这一点,以及导致测试失败,或者用另一个会失败的方法替换该方法的任何其他想法?

最佳答案

好吧...我终于想出了一个解决方案。这是我的工作。在 visitFeatureAnnotation 方法中,我添加了一个我创建的 CauseFailureInterceptor。

如果有人感兴趣,这里是完整的源代码,只需要您扩展 KnownIssueExtension 并实现抽象方法 getDefectStatus:

public abstract class KnownIssueExtension extends AbstractAnnotationDrivenExtension<KnownIssue> {
private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(KnownIssueExtension.class)
public void visitFeatureAnnotation(KnownIssue knownIssue, FeatureInfo feature) {
DefectStatus status = null
try{
status = getDefectStatus(knownIssue.value())
} catch(Exception ex){
LOGGER.warn("Unable to determine defect status for defect ID '{}', test case {}", knownIssue.value(), feature.getName())
// If we can't get info from Defect repository, just skip it, it should not cause failures or cause us not to execute tests.
}
if (status != null){
if(!status.open && !status.fixed){
LOGGER.error("Defect with ID '{}' and title '{}' is no longer in an open status and is not fixed, for test case '{}'. Update or remove test case.", knownIssue.value(), status.defectTitle, feature.getName())
feature.addInterceptor(new CauseFailureInterceptor("Defect with ID '" + knownIssue.value() + "' and title '" + status.defectTitle + "' is no longer in an open status and is not fixed, for test case '" + feature.getName() + "'. Update or remove test case."))
}else if (status.open && !status.fixed){
LOGGER.warn("Defect with ID '{}' and title '{}' is still open and has not been fixed. Not executing test '{}'", knownIssue.value(), status.defectTitle, feature.getName())
feature.setSkipped(true)
}else if (!status.open && status.fixed){
LOGGER.error("Defect with ID '{}' and title '{}' has been fixed and closed. Remove KnownIssue annotation from test '{}'.", knownIssue.value(), status.defectTitle, feature.getName())
feature.addInterceptor(new CauseFailureInterceptor("Defect with ID '" + knownIssue.value() + "' and title '" + status.defectTitle + "' has been fixed and closed. Remove KnownIssue annotation from test '" + feature.getName() + "'."))
}else { // status.open && status.fixed
LOGGER.warn("Defect with ID '{}' and title '{}' has recently been fixed. Remove KnownIssue annotation from test '{}'", knownIssue.value(), status.defectTitle, feature.getName())
}
}
}

public abstract DefectStatus getDefectStatus(String defectId)

}

public class CauseFailureInterceptor extends AbstractMethodInterceptor{
public String failureReason
public CauseFailureInterceptor(String failureReason = ""){
this.failureReason = failureReason
}

@Override
public void interceptFeatureExecution(IMethodInvocation invocation) throws Throwable {
throw new Exception(failureReason)
}
}

class DefectStatus{
boolean open
boolean fixed
String defectTitle
}

关于java - 如何使用注释扩展让 spock 在运行时执行不同的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29312006/

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