Java knowledge accumulation-unit test and JUnit (2)

Source: Internet
Author: User

First, let's review several important knowledge points and then introduce them. In the previous article, I posted the following picture:

In which method stubs wocould you like to create? Here, we will describe the meanings of the four Annotations:

@ Beforeclass: The annotation method is executed once during class loading, and only once. This method must be public and static.

@ Afterclass: The annotation method is executed once when the class is destroyed. The method must be public or static.

@ Before: the labeled method is executed once before each test method is executed.

@ After: The labeled method is executed once after each test method is executed.

The preceding four annotations correspond to the four methods in the check list. You can select them based on the actual conditions when creating a test class.

 

During the test, multiple groups of data may need to be tested for different critical values of a method. The following shows the test method in this case:

In the example in the previous article, the square method of the calculator class needs to be tested for positive, negative, and zero. So let's proceed as follows. First, right-click a new juint test case in the calculator class and name it calculatorsquaretest. Then the code of this class is modified as follows:

1 package Org. logback. test; 2 3 Import static Org. JUnit. assert. *; 4 5 import Java. util. arrays; 6 Import Java. util. collection; 7 8 Import Org. JUnit. before; 9 Import Org. JUnit. test; 10 Import Org. JUnit. runner. runwith; 11 import Org. JUnit. runners. parameterized; 12 Import Org. JUnit. runners. parameterized. parameters; 13 14 @ runwith (parameterized. class) 15 public class calculatorsquaretest {16 17 Private Static calculator example = new calculator (); 18 19 private int Param; 20 21 private int result; 22 23 @ parameters // define the test data set. The former is Param and the latter is result24 public static collection data () {25 return arrays. aslist (new object [] [] {26 {2, 4}, 27 {0}, 28 {-3, 9}, 29}); 30} 31 32 @ before33 public void setup () throws exception {34 example. clear (); 35} 36 37 @ test38 public void testsquare () {39 example. square (PARAM); 40 assertequals (result, example. getresult (); 41} 42 43 // constructor to initialize the variables. Note that the parameter sequence should be consistent with the parameter sequence of the defined test set. 44 public calculatorsquaretest (INT Param, int result) {45 this. param = Param; 46 this. result = result; 47} 48 49}

In row 14, the runtime of this test class is set to parameterized. Class. Because this class requires multiple groups of data, the default runtime cannot be used.

Rows 23-30 define the test data set. The order of the two tuples must be consistent with that of the constructor parameters.

Lines 44-47 are constructor. By defining the dataset and constructor, the situations to be tested are passed to the test class and then tested.

That is to say, the test class is executed three times and the data in the dataset is used in sequence. In this example, the sequence of data sets is {processing value, expected processing result }.

The result is as follows:

We can see that runs is 3/3.

 

OK. Now the number of test classes has increased to 2. It may be troublesome to run two test classes and observe the results again. Some people think this is nothing. What if there are 10 test classes? Therefore, the concept of packaging testing is introduced. We can use the mechanism provided by JUnit to execute the previous two test classes at a time.

Right-click a new JUnit test case in the calculator class, select none of the methods, and click Finish. Change the code to the following:

 1 package org.logback.test; 2  3 import static org.junit.Assert.*; 4  5 import org.junit.Test; 6 import org.junit.runner.RunWith; 7 import org.junit.runners.Suite; 8  9 @RunWith(Suite.class)10 @Suite.SuiteClasses({11     CalculatorSquareTest.class,12     CalculatorTest.class13 })14 public class CalculatorAllTest {15 }

 

The runner of the test class is specified in line 9.

Lines 10-13 specify the test classes to be run at the same time.

This test class is not actually implemented

Run as JUnit test to obtain the result:

We can see that the test classes in the previous article and the test classes in this article are executed at the same time, and detailed results are listed.
These are the results of my research on JUnit. I feel that you can work with these skills. Of course, if any geek has a more advanced and convenient usage, please share it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.