Instrumented (by cobertura) @Decorator bean could not validated by OpenEJB for unit testing

天涯浪子 提交于 2019-12-11 18:09:22

问题


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).

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

Details:

I have one Decorator to be unit tested. Something like

import org.apache.commons.configuration.AbstractConfiguration;

import org.apache.commons.configuration.Configuration;

@Decorator

public class MyDecorator extends AbstractConfiguration {

@Inject
@Delegate
private Configuration conf;

.....

}

After cobertura instrumented it, the code is like below:(I uncompile it)

import net.sourceforge.cobertura.coveragedata.HasBeenInstrumented;

@Decorator

public class MyDecorator extends AbstractConfiguration implements HasBeenInstrumented {

@Inject
@Delegate
private Configuration conf;

.....

}

As you can see, cobertura add one more interface for my decorator. When OpenEJB load and deploy this instrumented class, a error is reported:

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

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.

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:

@Decorator

public class MyDecorator extends AbstractConfiguration {

@Inject
@Delegate
private AbstractConfiguration conf;

.....

} //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.

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.

Should i create an comments for maven-cobertura-plugin org?

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?

Normally how do you unit test your @Decorators?


回答1:


Cobertura does not instrument interfaces. It is recommended the non-instrumented classes go in the classpath after the instrumented classes for that reason.

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.

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".

You'll need to run the 'jar -cvf [name-of-jar].jar *', then you'll get your instrumented jar.



来源:https://stackoverflow.com/questions/14601601/instrumented-by-cobertura-decorator-bean-could-not-validated-by-openejb-for-u

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!