2

I'm using cobertura maven plugin to produce report about test code coverage of my spring-based application. My unit tests configures as:

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

testAppContext.xml - Spring IOC config located at /src/test/resources/testAppContext.xml

And my cobertura's related pom.xml part is:

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

When I make "mvn clean install" it works fine, but when I make "mvn site" - spring-based tests are failed because of "Failed to load ApplicationContext" with underlying "Injection of autowired dependencies failed", so I receive incorrect report about test coverage.

I assume this may be because testAppContext.xml is not on the classpath during "site" goal or something other. Any suggestion how to fix this problem?

Thank you for your help!

Dmitry Spikhalskiy
  • 5,379
  • 1
  • 26
  • 40
  • 1
    Can you post full stack trace? Also take a look at: http://stackoverflow.com/questions/8391944 – Tomasz Nurkiewicz Dec 20 '12 at 15:39
  • @TomaszNurkiewicz It works for my case too. I don't have “Bean named 'x' must be of type [y], but was actually of type [$Proxy]” in stacktrace, so I had no chances to find this solution. Thank you for your answer and this link! – Dmitry Spikhalskiy Dec 20 '12 at 18:07

1 Answers1

4

Quoting my answer from Getting Spring Error "Bean named 'x' must be of type [y], but was actually of type [$Proxy]" in Jenkins:

The problem with Cobertura is that it performs pretty heavy byte-code instrumentation including the addition of some custom interfaces. When Spring starts up it generates proxies for beans. If bean has at least one interface, it uses standard Java proxy. Otherwise it tries to create class-based proxy.

I guess in your case the CGLIB class proxy was used but after Cobertura instrumentation Spring fall back to java proxies. This caused startup error because dependency injection expected class (or CGLIB subclass).

To cut long story short, force CGLIB class proxies and you'll be fine:

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

Symptoms aren't the same but apprently the trick above helped here as well.

Community
  • 1
  • 1
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674