Missing jacoco.exec file when using jacoco offline instrumentation with Powermock

你说的曾经没有我的故事 提交于 2019-12-04 16:59:55
<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>${jacoco.version}</version>
    <configuration>
        <append>true</append>
    </configuration>
    <executions>
        <execution>
            <id>default-instrument</id>
            <goals>
                <goal>instrument</goal>
            </goals>
            <configuration>
                <includes>
                    <include>**/*test*</include>
                </includes>
            </configuration>
        </execution>
        <execution>
            <id>default-restore-instrumented-classes</id>
            <goals>
                <goal>restore-instrumented-classes</goal>
            </goals>
            <configuration>
                <includes>
                    <include>**/*test*</include>
                </includes>
            </configuration>
        </execution>
        <execution>
            <id>Prepare-Jacoco</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <excludes>
                    <exclude>**/*test*</exclude>
                </excludes>
            </configuration>
        </execution>
    </executions>
</plugin>

you can remove Check for now, we can try to get it working with out that first.

Update your Jacoco dependency to look like this

<dependency>
    <groupId>org.jacoco</groupId>
    <artifactId>org.jacoco.agent</artifactId>
    <version>${jacoco.version}</version>
    <scope>test</scope>
</dependency> 

Make you maven surefire plugin look like this:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <argLine>${argLine}</argLine>
    </configuration>
</plugin>

in your properties section Add this as, and change your jacoco Version:

<jacoco.version>0.7.5.201505241946</jacoco.version>
<jacoco.outputDir>${project.basedir}/target/jacoco.exec</jacoco.outputDir>

@codependent Heres an example of why you want to mock.

Lets assume your Util.class has a lot more code, a lot of instantiations and maybe it even touches some server side stuff, hell maybe even its a UI. You don't want to instantiate User.class just to test a single method in some other class called UtilUser.class

protected class UtilUser {
    protected UtilUser() {}

    public Boolean checkSay(String s) {
        String expectedString = "hello:" + s;
        String actualString = Util.say(s);

        if (expectedString.equals(actualString){
           return true;
        } else { 
           return false;
        }

    }
}

To unit test UtilUser, you want to hit all branches. So in your test you would mock the Util.class, and in one unit test force it to return the correct string, and in another test have it return an incorrect string. You don't care about testing User.class, you know it will behave accordingly or you will have another test to take care of that. For unit testing UtilUser, you can expect Util to either return a correct string or an incorrect string, and you want to test both possibilities.

Theres a time and a place for mocks and they are a great tool to know, but DO NOT think they should be used often. Please refer to the following article, it will give you good insight into when you should use mocks: When To Mock

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