OpenEJB无法验证(由cobertura提供)@Decorator bean进行单元测试 [英] Instrumented (by cobertura) @Decorator bean could not validated by OpenEJB for unit testing
问题描述
为了获得代码覆盖率报告,我通过cobertura maven插件对@Decorator bean进行了检测. 在OpenEJB容器中运行我的单元测试时.容器在启动过程中报告了一些错误(新的初始上下文).
In order to get code coverage report, i instrument the @Decorator bean by cobertura maven plugin. When running my unit test in OpenEJB container. The container reports some error during start up (new initial context).
由以下原因引起:org.apache.webbeans.exception.WebBeansConfigurationException:装饰器:MyDecorator,名称:空,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的类型
Caused by: org.apache.webbeans.exception.WebBeansConfigurationException: Decorator : MyDecorator, Name:null, WebBeans Type:DECORATOR, API Types:[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], Qualifiers:[javax.enterprise.inject.Any,javax.enterprise.inject.Default] delegate at tribute must implement all of the decorator decorated types, but decorator type interface net.sourceforge.cobertura.coveragedata.HasBeenInstrumented is not assignable from deleg ate type of interface org.apache.commons.configuration.Configuration
我有一个装饰器要进行单元测试. 像
I have one Decorator to be unit tested. Something like
导入org.apache.commons.configuration.AbstractConfiguration;
import org.apache.commons.configuration.AbstractConfiguration;
导入org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.Configuration;
@装饰器
公共类MyDecorator扩展AbstractConfiguration {
public class MyDecorator extends AbstractConfiguration {
@Inject
@Delegate
private Configuration conf;
.....
}
在cobertura对其进行检测后,代码如下:(我将其反编译)
After cobertura instrumented it, the code is like below:(I uncompile it)
导入net.sourceforge.cobertura.coveragedata.HasBeenInstrumented;
import net.sourceforge.cobertura.coveragedata.HasBeenInstrumented;
@装饰器
公共类MyDecorator扩展AbstractConfiguration 实施HasBeenInstrumented {
public class MyDecorator extends AbstractConfiguration implements HasBeenInstrumented {
@Inject
@Delegate
private Configuration conf;
.....
}
如您所见,cobertura为我的装饰器添加了一个接口. 当OpenEJB加载并部署此检测类时,将报告错误:
As you can see, cobertura add one more interface for my decorator. When OpenEJB load and deploy this instrumented class, a error is reported:
由以下原因引起:org.apache.webbeans.exception.WebBeansConfigurationException:Decorator:MyDecorator,Name:null,WebBeans Type:DECORATOR,API Types:[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的类型
Caused by: org.apache.webbeans.exception.WebBeansConfigurationException: Decorator : MyDecorator, Name:null, WebBeans Type:DECORATOR, API Types:[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], Qualifiers:[javax.enterprise.inject.Any,javax.enterprise.inject.Default] delegate at tribute must implement all of the decorator decorated types, but decorator type interface net.sourceforge.cobertura.coveragedata.HasBeenInstrumented is not assignable from deleg ate type of interface org.apache.commons.configuration.Configuration
错误日志显示@Decorator和@Delegate应该实现相同的类型. 但是在仪器之后,要测试的类又有一个接口.
The error log say that the @Decorator and the @Delegate should implement the same types. But after instrument, the to be tested class has one more interface.
然后,我尝试检测org.apache.commons.configuration.AbstractConfiguration和org.apache.commons.configuration.Configuration. (通过cobertura命令行通过commons-configuration-1.9.jar进行配置) 并修改我的代码,例如:
Then i try to instrument the org.apache.commons.configuration.AbstractConfiguration and org.apache.commons.configuration.Configuration. (by instrument the commons-configuration-1.9.jar by cobertura command line) And modify my code like:
@装饰器
公共类MyDecorator扩展AbstractConfiguration {
public class MyDecorator extends AbstractConfiguration {
@Inject
@Delegate
private AbstractConfiguration conf;
.....
} //我使用AbstractConfiguration而不是Configuration,因为Configuration是无法检测的//接口.
} //I use AbstractConfiguration instead of Configuration, because the Configuration is an //interface which could not be instrumented.
所有这些都解决了. 但这不是一个好方法.
After all of this,the problem is solved. But it is not a good way to do this.
根本原因是maven cobertura插件通过在原始类中添加接口来识别类文件,我在大多数情况下都可以使用. 但是不适用于在容器中运行的@Decorator bean.
The root cause is maven cobertura plugin identify the class file is instrumented by adding an interface to the original class, i works for most of the cases. But not for a @Decorator bean which running in an container.
我应该为maven-cobertura-plugin组织创建评论吗?
Should i create an comments for maven-cobertura-plugin org?
任何人都对如何对@Decorators进行单元测试提出了一些建议,并且容易获得覆盖率报告吗? 可能是我的单元测试不是很好的实现,也许openejb对此不好吗?
Any one has some suggestion on how to unit test @Decorators.And easy to get coverage report? May be my unit test is not implement in the good way, maybe the openejb is not good for this?
通常您如何对@Decorators进行单元测试?
Normally how do you unit test your @Decorators?
推荐答案
Cobertura不检测接口.出于这个原因,建议将非仪表类放到插装类之后的类路径中.
Cobertura does not instrument interfaces. It is recommended the non-instrumented classes go in the classpath after the instrumented classes for that reason.
因此,在进行检测时,请先使用maven正常进行编译,然后将您自己放置在要检测的类的源代码所在的目录中,然后运行以下命令:mvn cobertura:instrument.
So when instrumenting, compile first with maven normally, then place yourself in the directory where the sourcecode of the classes you want to instrument exists, and then run the following command : mvn cobertura:instrument.
这将使cobertura检测所有类,并且maven将自动添加未检测的文件.检测的代码将位于.\ target \ generated-classes \ cobertura".
This will make cobertura instrument all the classes, and maven will automatically add the files not instrumented. the instrumented code will be at ".\target\generated-classes\cobertura".
您需要运行"jar -cvf [jar的名称] .jar *",然后将获得已检测的jar.
You'll need to run the 'jar -cvf [name-of-jar].jar *', then you'll get your instrumented jar.
这篇关于OpenEJB无法验证(由cobertura提供)@Decorator bean进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!