JUnit & JMockit unit tests

JUnit & JMockit unit test summary

1.JUnit Profile

More Java unit testing framework for industry applications JUnit, it was founded by Kent Beck and Erich Gamma, becoming the xUnit family from Kent Beck's sUnit the most successful one. JUnit JUnit extension has its own ecosystem. Most Java development environment have been integrated as a tool for JUnit unit testing.

Currently JUnit mainstream versions 3.x, 4.x and 5.x, 4.x version currently continue to recommend the use of each IDE also automatically integrates it.

2.JMockit Profile

There are many frame-based Junit unit testing, including EasyMock, JMock, mockito, JMockit like. Mock frame stub can be performed in the test method. But by comparing various testing framework, JMockit for popular test methods all support and powerful. Thus as the frame selection JMockit Junit unit testing.

JMockit is to help create a mock object tool, which is based on Java development, Java has unparalleled advantages in test and development environments, more importantly, it greatly simplifies the use of the virtual object.

3.Maven integrated unit testing tool

<!‐‐版本号‐‐>
<properties>
    <junit.versioin>4.12</junit.versioin>
    <jmockit.version>1.34</jmockit.version>
</properties>
<!‐‐依赖‐‐>
<dependencies>
   <dependency>
     <groupId>org.jmockit</groupId>
             <artifactId>jmockit</artifactId>
             <version>${jmockit.version}</version>
             <scope>test</scope>
      </dependency>
         <dependency>
     <groupId>junit</groupId>
             <artifactId>junit</artifactId>
             <version>${junit.versioin}</version>
             <scope>test</scope>
         </dependency>
</dependencies>

4. Use of the test unit

  • JUnit4 basic grammar

    Statement test class Junit4.x difference with the previous version, without displaying the inherited TestCase. For the preparation of unit test cases by using Junit annotation.

    • @Test: identify test methods
    • @Ignore: test methods overlooked or test class
    • @Before: initialization method, the code must be executed before any test execution
    • @After: free up resources, after finishing work at any test execution need to make. Executed once after each test method execution, the annotation can only be modified public void method;

    • @BeforeClass: For all tests, performed only once and must be public static void; generally used to initialize the necessary large consumption of
      resources, such as database connections, etc.
    • @AfterClass: For all the tests will be performed once at the end of the implementation of all the test methods, and must be public static void;
    • @Parameters: unit testing time, a method usually requires several test case, we are facilitates parameterization Junit provide various combinations of test parameters for the methods
      introduced @Rule @ClassRule and after 4.9, is essentially @BeforeClass, @AfterClass, @Before, @After another implementation, etc., but the function is more flexible, easy to expand and facilitate sharing between classes and projects.
      When using two notes on the need to achieve a Rule variables TestRule interface or return on the Rule of methods and modifiers are necessary for the public. @Rule variable or method level annotations will be called by the modified Rule annotated each test method execution; @ClassRule for the class-level annotation, the annotation will only be called once when the modified Rule to perform a single test class.

  • JMockit basic grammar

    JMockit annotation-based test is more to achieve. Jmockit commonly used notes are:
    • @Mocked: modified Mock object will be, and the corresponding class instances are affected (in the same test a)
      @Mocked against type, it can be used to test the class members mock fi eld with mock parameter parameter parameter embodiment. Notes the role of mock fi eld test of the entire class, and annotate mock parameter is only for the test case.

    • @Injectable: Mock only modified object
      @Injectable is directed to a single instance, can be used to test the class members mock fi eld with mock parameter parameter parameter embodiment. Notes the role of mock fi eld test of the entire class, and annotate mock parameter is only for the test case.

    • @Capturing: mock interface and all its possible implementation class
      @Capturing is a Mock object identifier, the object is derived from the subclass is also the Mock. The method may be specified by the maximum number of layers maxInstances subclasses also mock. Of course, and as @Mocked and @Injectable, may be used as class fi eld annotation parameters and test cases. At the same time, it is for the type.

    • @Tested: specifies the test object
      @Tested identifies an instance of a measured object. @Tested tag identification by the measured object instance, it will be initialized before performing the test method.

    • @Mock: MockUp mode, the specified method is Fake

Guess you like

Origin www.cnblogs.com/leton/p/11896021.html
Recommended