【问题标题】:Instrumented (by cobertura) @Decorator bean could not validated by OpenEJB for unit testing检测(由 cobertura)@Decorator bean 无法由 OpenEJB 验证以进行单元测试
【发布时间】:2023-03-22 02:39:01
【问题描述】:

为了获得代码覆盖率报告,我通过 cobertura maven 插件检测了@Decorator bean。 在 OpenEJB 容器中运行我的单元测试时。容器在启动过程中报告了一些错误(新的初始上下文)。

原因:org.apache.webbeans.exception.WebBeansConfigurationException:装饰器:MyDecorator,名称:null,WebBeans 类型:DECORATOR,API 类型:[org.apache.commo ns.configuration.Configuration,net.sourceforge.cobertura.coveragedata.HasBeenInstrumented,org.apache.commons.configuration.AbstractConfiguration,MyDecorator,org.apache.commons.configuration.event.EventSource,java.lang.Object],限定符:[ javax.enterprise.inject.Any,javax.enterprise.inject.Default] 委托在 贡品必须实现所有装饰器装饰类型,但装饰器类型接口 net.sourceforge.cobertura.coveragedata.HasBeenInstrumented 不能从 deleg 分配 吃了接口org.apache.commons.configuration.Configuration的类型

详情:

我有一个装饰器要进行单元测试。 类似的东西

导入 org.apache.commons.configuration.AbstractConfiguration;

导入 org.apache.commons.configuration.Configuration;

@装饰器

公共类 MyDecorator 扩展 AbstractConfiguration {

@Inject
@Delegate
private Configuration conf;

.....

}

cobertura 检测后,代码如下:(我反编译)

导入 net.sourceforge.cobertura.coveragedata.HasBeenInstrumented;

@装饰器

公共类 MyDecorator 扩展 AbstractConfiguration 实现 HasBeenInstrumented {

@Inject
@Delegate
private Configuration conf;

.....

}

如你所见,cobertura 为我的装饰器增加了一个界面。 当 OpenEJB 加载和部署这个检测类时,会报错:

原因:org.apache.webbeans.exception.WebBeansConfigurationException:装饰器:MyDecorator,名称:null,WebBeans 类型:DECORATOR,API 类型:[org.apache.commo ns.configuration.Configuration,net.sourceforge.cobertura.coveragedata.HasBeenInstrumented,org.apache.commons.configuration.AbstractConfiguration,MyDecorator,org.apache.commons.configuration.event.EventSource,java.lang.Object],限定符:[ javax.enterprise.inject.Any,javax.enterprise.inject.Default] 委托在 贡品必须实现所有装饰器装饰类型,但装饰器类型接口 net.sourceforge.cobertura.coveragedata.HasBeenInstrumented 不能从 deleg 分配 吃了接口org.apache.commons.configuration.Configuration的类型

错误日志说@Decorator 和@Delegate 应该实现相同的类型。 但是在instrument之后,待测类多了一个接口。

然后我尝试检测 org.apache.commons.configuration.AbstractConfiguration 和 org.apache.commons.configuration.Configuration。 (通过 cobertura 命令行检测 commons-configuration-1.9.jar) 并修改我的代码:

@装饰器

公共类 MyDecorator 扩展 AbstractConfiguration {

@Inject
@Delegate
private AbstractConfiguration conf;

.....

} //我使用 AbstractConfiguration 而不是 Configuration,因为 Configuration 是一个 //无法检测的接口。

做完这些,问题就解决了。 但这不是一个好方法。

根本原因是 maven cobertura 插件通过向原始类添加接口来识别类文件,我适用于大多数情况。 但不适用于在容器中运行的 @Decorator bean。

我应该为 maven-cobertura-plugin org 创建一个 cmets 吗?

任何人对如何对@Decorators 进行单元测试有一些建议。并且容易获得覆盖率报告? 可能是我的单元测试没有以好的方式实现,也许 openejb 不适合这个?

您通常如何对@Decorators 进行单元测试?

【问题讨论】:

  • 我已经为 maven cobertura 插件 jira.codehaus.org/browse/MCOBERTURA-173 创建了一个问题,我认为解决方案可能是:让 cobertura 不添加用于识别检测类的接口,而不是使用注释。如果我有时间,我会尝试实现它。

标签: unit-testing java-ee-6 code-coverage cobertura openejb


【解决方案1】:

Cobertura 不检测接口。因此,建议将非插桩类放在插桩类之后的类路径中。

所以在instrumenting的时候,先用maven正常编译,然后把自己放到需要instrument的类的源码所在的目录下,然后运行如下命令:mvn cobertura:instrument。

这将使 cobertura 检测所有类,并且 maven 将自动添加未检测的文件。检测代码将位于“.\target\generated-classes\cobertura”。

您需要运行 'jar -cvf [name-of-jar].jar *',然后您将获得已检测的 jar。

【讨论】:

  • 我认为我的问题是插入的类(装饰器)没有通过 OpenEJB 的 @Decorator bean 验证。
  • 我找到了一个链接,也许可以帮到你link
  • 添加属性“openejb.tempclassloader.skip=annotations”可以跳过扫描,我不会有问题,但同时我的bean不会被部署。无论如何,非常感谢您的宝贵时间。
猜你喜欢
  • 1970-01-01
  • 2020-10-23
  • 2011-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-25
  • 1970-01-01
  • 2014-06-16
相关资源
最近更新 更多