Maven project introduces JMockit

Step 1 to introduce JMockit in Maven project

1 Create a standard Maven project, remember to select <packaging>war</packaging> war or jar in Pom, but not pom, otherwise the maven test command will only build the project. This is the pit I encountered when building a test project

2 Introducing JMockit dependencies
<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <!--<scope>test</scope>-->
        </dependency>
        <dependency>
            <groupId>org.jmockit</groupId>
            <artifactId>jmockit</artifactId>
            <version>1.38</version>
            <!--<scope>runtime</scope>-->
        </dependency>


3 Introduce maven-surefire-plugin, otherwise, after the Jmockit test runs, the HTML test coverage report will not be generated
<plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.5.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <!-- Bind to maven test phase -->
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.20</version>
                    <configuration>
                        <systemPropertyVariables>
                            <coverage-output>html</coverage-output>
                            <coverage-outputDir>target/coverage-report</coverage-outputDir>
                        </systemPropertyVariables>
                    </configuration>
                </plugin>
        </plugins>


Special attention should be paid here. The maven test command will only run the test classes under src/test/java. The naming rules are as follows:
The test classes included by default are:
**/*Test.java
**/Test*.java
**/*TestCase .java
default excluded test classes:
**/Abstract*Test.java
**/Abstract*TestCase.java

4 For example:
@RunWith(JMockit.class)
public class TestJApp1_1 {

    @Tested
    Service testedService;

    @Injectable
    Dependency dependency;

    @Test
    public void exeUnitTest() {
        new Expectations() {{
            dependency.findSomeData(anyString);
            result = new Data("345");
        }};

        testedService.doSomething("...");
    }
}


The test class is placed under src/test/java
public class Service {

    private Dependency dependency;

    public Data doSomething(String someData) {

        Data data = dependency.findSomeData(someData);
        System.out.println(data.getText());
        return data;
    }

    public String fun() {
        return "call original method";
    }
}
public class Dependency {

    public Data findSomeData(String anyString) {
        System.out.println("Dependency -->"+anyString);
        log(anyString);
        return new Data(anyString);
    }

    private void log(String text){
        System.out.println("Dependency private log -->"+text);
    }
}
public class Data {
    private String text;

    public Data(String text) {
        this.text = text;
    }

    public String getText() {
        return text;
    }
}

This class is placed under src/main/java. Right-clicking runas or maven test will generate a test report, the effect is as follows



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326221518&siteId=291194637