2

我正在使用cobertura maven插件来生成关于我的基于spring的应用程序的测试代码覆盖率的报告。我的单元测试配置为:在cobertura-maven-plugin测试中加载Spring ApplicationContext

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = "classpath:/testAppContext.xml") 
public class TestCase extends TestBase 

testAppContext.xml - 春天IOC配置位于/src/test/resources/testAppContext.xml

而且我的Cobertura的相关pom.xml的部分是:

<build> 
... 
    <plugin> 
     <groupId>org.codehaus.mojo</groupId> 
     <artifactId>cobertura-maven-plugin</artifactId> 
     <executions> 
      <execution> 
       <goals> 
        <goal>clean</goal> 
       </goals> 
      </execution> 
     </executions> 
    </plugin> 
... 
<build> 

<reporting> 
    <plugins> 
     <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>cobertura-maven-plugin</artifactId> 
     </plugin> 
    </plugins> 
</reporting> 

当我做“mvn干净安装”它工作正常,但当我使“mvn网站” - 基于弹簧的测试失败,因为“无法加载ApplicationContext”与基础“注入自动装配依赖失败”,所以我收到有关测试覆盖率的错误报告

我认为这可能是因为testAppContext.xml在“site”目标或其他目录中不在类路径中。任何建议如何解决这个问题?

谢谢你的帮助!

+1

你能发布完整的堆栈跟踪吗?也请看看:http://stackoverflow.com/questions/8391944 –

+0

@TomaszNurkiewicz它也适用于我的情况。我没有“名为'x'的Bean必须是[y]类型,但实际上是在stacktrace中键入[$ Proxy]”,所以我没有机会找到这个解决方案。感谢您的回答和此链接! –

回答

3

引用我的回答从Getting Spring Error "Bean named 'x' must be of type [y], but was actually of type [$Proxy]" in Jenkins

用Cobertura的问题是,它执行相当沉重的字节码仪器包括增加一些自定义的接口。当Spring启动时,它会为bean生成代理。如果bean至少有一个接口,它使用标准的Java代理。否则,它会尝试创建基于类的代理。

我想你的情况下CGLIB类代理被使用,但在Cobertura检测后,Spring会回退到java代理。这导致启动错误,因为依赖注入期望类(或CGLIB子类)。

为了削减长话短说,迫使CGLIB类代理,你会被罚款:

<aop:config proxy-target-class="true"/> 

症状也不尽相同,但apprently以上的伎俩在这里帮助为好。

相关问题