jmockit搭配testng与junit的对比(Comparison of jmockit with TestNG and JUnit)

jmockit搭配testng与junit的对比

maven依赖

<dependencies>
    <dependency>
        <groupId>org.jmockit</groupId>
        <artifactId>jmockit</artifactId>
        <version>1.36</version>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.1.0</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
</dependencies>

java代码

import mockit.Mocked;
import java.util.Locale;

public class LocaleTest {

    @org.testng.annotations.Test
    public void test1(@Mocked Locale locale) {
        System.out.println(locale);//null
    }

    @org.junit.Test
    public void test2(@Mocked Locale locale) {
        System.out.println(locale);//java.util.Locale@224aed64
    }
}

结论

当使用jmockit+testng做单元测试时,传入测试方法的@Mocked参数为空;当使用jmockit+junit做单元测试时,传入测试方法的@Mocked参数不为空。

————————

jmockit搭配testng与junit的对比

maven依赖

<dependencies>
    <dependency>
        <groupId>org.jmockit</groupId>
        <artifactId>jmockit</artifactId>
        <version>1.36</version>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.1.0</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
</dependencies>

java代码

import mockit.Mocked;
import java.util.Locale;

public class LocaleTest {

    @org.testng.annotations.Test
    public void test1(@Mocked Locale locale) {
        System.out.println(locale);//null
    }

    @org.junit.Test
    public void test2(@Mocked Locale locale) {
        System.out.println(locale);//java.util.Locale@224aed64
    }
}

conclusion

When jmockit + TestNG is used for unit testing, the @ mocked parameter of the test method passed in is null; When jmockit + JUnit is used for unit testing, the @ mocked parameter passed into the test method is not empty.