Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maven test fails to run moco server when using jacoco maven plugin 0.7.8 or newer #816

Closed
chenhengjie123 opened this issue Jan 5, 2019 · 4 comments
Labels
declined: otherproject 👽 This issue should be reported to other project

Comments

@chenhengjie123
Copy link

This is a issue tracker. Please use our mailing list for general questions:
https://groups.google.com/forum/?fromgroups=#!forum/jacoco

Also check FAQ before opening an issue: http://www.jacoco.org/jacoco/trunk/doc/faq.html

Steps to reproduce

test case code:

import com.github.dreamhead.moco.Moco;
import com.github.dreamhead.moco.junit.MocoJunitRunner;
import org.apache.http.HttpResponse;
import org.apache.http.client.fluent.Request;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.IOException;

public class BzdWithdrawHisServiceImplTest {
    @Rule
    public MocoJunitRunner runner = MocoJunitRunner.jsonHttpRunner(12306, Moco.pathResource("foo.json"));

    @Test
    public void  mockTest() throws IOException {
        HttpResponse httpResponse = Request.Get("http://localhost:12306").execute().returnResponse();
        System.out.println(httpResponse);
    }
}

configuration in pom.xml:

...
        <!-- https://mvnrepository.com/artifact/com.github.dreamhead/moco-junit -->
        <dependency>
            <groupId>com.github.dreamhead</groupId>
            <artifactId>moco-junit</artifactId>
            <version>0.12.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.github.dreamhead/moco-runner -->
        <dependency>
            <groupId>com.github.dreamhead</groupId>
            <artifactId>moco-runner</artifactId>
            <version>0.12.0</version>
        </dependency>
...
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>${jacoco.maven.plugin.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

error occurs when running mvn test to run tests with jacoco.

JaCoCo version: multiple, mentioned in Actual behaviour
Operating system: occurs on mac os 10.14 and windows 7
Tool integration: Maven/Ant/API/Other: maven

Expected behaviour

mvn test could run well

Actual behaviour

when running mvn test, error occurs like below:

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 2.355 sec <<< FAILURE! - in com.ppmoney.feastful.fc.BzdWithdrawHisServiceImplTest
mockTest(com.ppmoney.feastful.fc.BzdWithdrawHisServiceImplTest)  Time elapsed: 0.012 sec  <<< ERROR!
java.lang.IllegalArgumentException: unknown field [$jacocoData]
	at com.ppmoney.feastful.fc.BzdWithdrawHisServiceImplTest.<init>(BzdWithdrawHisServiceImplTest.java:15)
jacoco maven plugin version result
0.7.2.201409121644 success
0.7.7.201606060606 success
0.7.8 error
0.8.2 error
@Godin
Copy link
Member

Godin commented Jan 5, 2019

@chenhengjie123 the list of dependencies is definitely incomplete:

BzdWithdrawHisServiceImplTest.java:[4,37] package org.apache.http.client.fluent does not exist

So could you please attach zip with complete example without ... ?

@Godin Godin added the reproducer required Further information is requested label Jan 5, 2019
@chenhengjie123
Copy link
Author

@chenhengjie123 the list of dependencies is definitely incomplete:

BzdWithdrawHisServiceImplTest.java:[4,37] package org.apache.http.client.fluent does not exist

So could you please attach zip with complete example without ... ?

I have just uploaded the simplest project on https://github.com/chenhengjie123/ReproduceJacocoWithMoco. It can reproduce this issue.

@Godin
Copy link
Member

Godin commented Jan 5, 2019

And it shows different stack trace:

java.lang.IllegalArgumentException: unknown field [$jacocoData]
        at com.github.dreamhead.moco.parser.model.DynamicResponseHandlerFactory.createResponseHandler(DynamicResponseHandlerFactory.java:116)
        at com.github.dreamhead.moco.parser.model.DynamicResponseHandlerFactory.access$000(DynamicResponseHandlerFactory.java:37)
        at com.github.dreamhead.moco.parser.model.DynamicResponseHandlerFactory$1.apply(DynamicResponseHandlerFactory.java:70)
        at com.github.dreamhead.moco.parser.model.DynamicResponseHandlerFactory$1.apply(DynamicResponseHandlerFactory.java:65)
        at com.google.common.collect.Iterators$7.transform(Iterators.java:750)
        at com.google.common.collect.TransformedIterator.next(TransformedIterator.java:47)
        at com.google.common.collect.Iterators.size(Iterators.java:198)
        at com.google.common.collect.Iterables.size(Iterables.java:106)
        at com.google.common.collect.FluentIterable.size(FluentIterable.java:342)
        at com.github.dreamhead.moco.parser.model.DynamicResponseHandlerFactory.getResponseHandler(DynamicResponseHandlerFactory.java:53)
        at com.github.dreamhead.moco.parser.model.DynamicResponseHandlerFactory.createResponseHandler(DynamicResponseHandlerFactory.java:49)
        at com.github.dreamhead.moco.parser.model.ResponseSetting.getResponseHandler(ResponseSetting.java:62)
        at com.github.dreamhead.moco.parser.model.SessionSetting.getResponseHandler(SessionSetting.java:66)
        at com.github.dreamhead.moco.parser.model.SessionSetting.bindToSession(SessionSetting.java:111)
        at com.github.dreamhead.moco.parser.model.SessionSetting.bindTo(SessionSetting.java:78)
        at com.github.dreamhead.moco.parser.model.SessionSetting.newHttpServer(SessionSetting.java:144)
        at com.github.dreamhead.moco.parser.HttpServerParser.createServer(HttpServerParser.java:23)
        at com.github.dreamhead.moco.parser.HttpServerParser.createServer(HttpServerParser.java:12)
        at com.github.dreamhead.moco.parser.BaseParser.parseServer(BaseParser.java:19)
        at com.github.dreamhead.moco.MocoJsonRunner.parseHttpServer(MocoJsonRunner.java:60)
        at com.github.dreamhead.moco.MocoJsonRunner.jsonHttpServer(MocoJsonRunner.java:21)
        at com.github.dreamhead.moco.junit.MocoJunitRunner.jsonHttpRunner(MocoJunitRunner.java:37)
        at demo.TestForReproduceMocoWithJacoco.<init>(TestForReproduceMocoWithJacoco.java:14)

whose most relevant part is - DynamicResponseHandlerFactory.java:49, three lines above there is call of getFields and two lines above there is call of isValidFilter that filters final fields, but doesn't not filter out synthetic fields, whereas should - please read https://www.jacoco.org/jacoco/trunk/doc/faq.html :

My code uses reflection. Why does it fail when I execute it with JaCoCo?

To collect execution data JaCoCo instruments the classes under test which adds two members to the classes: A private static field $jacocoData and a private static method $jacocoInit(). Both members are marked as synthetic.

Please change your code to ignore synthetic members. This is a good practice anyways as also the Java compiler creates synthetic members in certain situation.

JaCoCo <=0.7.7 works, because in addition to field being synthetic, it was also incorrectly always final - see #434 that was fixed in 0.7.8.


As a workaround seems possible to exclude classes from instrumentation as following:

                 <execution>
                     <goals>
                         <goal>prepare-agent</goal>
                     </goals>
+                    <configuration>
+                        <excludes>com/github/dreamhead/moco/*</excludes>
+                    </configuration>
                 </execution>

with and without update to latest

-        <jacoco.maven.plugin.version>0.7.8</jacoco.maven.plugin.version>
+        <jacoco.maven.plugin.version>0.8.2</jacoco.maven.plugin.version>

@Godin Godin closed this as completed Jan 5, 2019
@Godin Godin added declined: otherproject 👽 This issue should be reported to other project and removed reproducer required Further information is requested labels Jan 5, 2019
@chenhengjie123
Copy link
Author

OK,I got it. The trace is different maybe cause by the origin project has other configs. Since it's a company project, I could not share the whole project, so I create this new project instead.

Thanks a lot!

@jacoco jacoco locked as resolved and limited conversation to collaborators May 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
declined: otherproject 👽 This issue should be reported to other project
Projects
None yet
Development

No branches or pull requests

2 participants