dreamhead / moco

Easy Setup Stub Server
MIT License
4.36k stars 1.08k forks source link

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

Open chenhengjie123 opened 5 years ago

chenhengjie123 commented 5 years ago

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);
    }
}

error when running with mvn test:

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)

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>

I have test with several versions, here is the result:

jacoco maven plugin version result
0.7.2.201409121644 success
0.7.7.201606060606 success
0.7.8 error
0.8.2 error

Almost the same as this issue: ERROR : Maven test fails to run moco server when using jacoco maven plugin.. Since it is closed, so I created this new issue instead.

dreamhead commented 5 years ago

It seems jacoco inserts a field $jacocoData into Moco code which is supposed to be ignored by Jacoco. Could you please try to find some way to ignore it?

chenhengjie123 commented 5 years ago

It seems jacoco inserts a field $jacocoData into Moco code which is supposed to be ignored by Jacoco. Could you please try to find some way to ignore it?

Do you know where I should try to modify so this error could be ignored? Since the stack trace just tell me the line of @Test throws this error, but didn't tell the detail in MocoJunitRunner or jacoco, it's not easy for me to find where to modify.

chenhengjie123 commented 5 years ago

Also I created an issue for jacoco: https://github.com/jacoco/jacoco/issues/816 .

chenhengjie123 commented 5 years ago

Jacoco has located the root cause and give a workaround method: https://github.com/jacoco/jacoco/issues/816

It seems moco need to change codes related to reflection to resolve this issue. @dreamhead