Java Knowledge Accumulation-unit test and JUnit (ii)

Source: Internet
Author: User

First of all, review a few important points of knowledge, and then proceed to some introductions. In the previous article, I posted the following image:

In which method stubs would do you like to create? Here, now combine the meanings of the 4 annotations to be clear:

The method that @BeforeClass callout is executed once when the class is loaded, and only this time, the method must be public and static

The method that @AfterClass callout is executed once when the class is destroyed, and only this time, the method must be public and static

Methods that are @Before labeled are executed one time before each test method executes

The method @After callout executes once after each test method execution finishes

The above four annotations, which correspond to the four methods in the tick list, can be checked according to the circumstances when creating the test class.

During testing, multiple sets of data may need to be tested for different critical values of a method. Here's how to test this scenario:

In the example in the previous article, the Square method of the Calculator class needs to be tested for positive, negative, and 3 cases. So we proceed as follows, first right-click on the Calculator class on the new Juint Test case, named Calculatorsquaretest, and then the code of the class is modified as follows:

1PackageOrg.logback.test;23ImportStatic org.junit.assert.*;45ImportJava.util.Arrays;6ImportJava.util.Collection;78ImportOrg.junit.Before;9ImportOrg.junit.Test;10ImportOrg.junit.runner.RunWith;11Importorg.junit.runners.Parameterized;12ImportOrg.junit.runners.Parameterized.Parameters;13@RunWith (parameterized.Class)15PublicClasscalculatorsquaretest {1617PrivateStatic Calculator example =NewCalculator ();1819PrivateIntParam2021stPrivateIntResult22@Parameters//Defines a collection of test data, the former param the latter result24PublicStaticCollection data () {25Return Arrays.aslist (Newobject[][]{26 {2,4},27 {0,0},28 { -3,9},29});30}3132@Before33Publicvoid SetUp ()ThrowsException {34Example.clear ();35}3637@Test38PublicvoidTestsquare () {39Example.square (param);40Assertequals (result, Example.getresult ());A.    // constructor, initialize the variables, and note that the order of the parameters should be consistent with the order of the parameters of the defined test set,  calculatorsquaretest (  int param,int result) { this.param = param;  this.result = result;                  +} 

Line 14 Sets the runner for this test class to Parameterized.class, because this class requires more than a set of data, so the default runner cannot be used.

Line 23-30 defines the test data collection, where the order of the 2-tuple data is uniform and consistent with the parameter order of the constructor.

The 44-47 row is a constructor, passed to the test class, and then tested, by defining the data collection and constructors that will be tested.

That is, the test class was executed 3 times, followed by data in the data collection. The order of the data collection in this example is {process value, expected processing result}.

The results are as follows:

Can see runs there is 3/3.

OK, at this point our number of test classes has risen to 2, if you run 2 test classes and then observe the results may be more troublesome. Or if someone thinks it's not a thing, what if there are 10 test classes? So it leads to the concept of packaging testing, and we can use the mechanism provided by JUnit to execute the previous 2 test classes at once.

On the Calculator class, right-click New to a JUnit Test case, do not check anything at the method, then tap Finish. Change the code to read as follows:

1PackageOrg.logback.test;23ImportStatic org.junit.assert.*;45ImportOrg.junit.Test;6ImportOrg.junit.runner.RunWith; 7 import 9 @RunWith (Suite.) 10 @ Suite.suiteclasses ({11 calculatorsquaretest. Class,12 calculatortest.13 14 public class calculatoralltest {15}                 

9 row Specifies the runner for this test class

10-13 row specifies which test classes to run at the same time

The test class does not actually implement

The following is the result of a run as JUnit test:

We can see that the test class of our previous article, and the test class for this article, were executed at the same time, and detailed results were listed.
The above is my research on junit, I feel that the general work of these skills can be dealt with, of course, if the Geek have more advanced convenient usage also welcome to share.

Java Knowledge Accumulation-unit test and JUnit (ii)

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.