JUnit is currently used in a number of large companies or relatively standardized software, and quite a few of the smaller companies do not see the unit test too important. At the big point of the company developers work every day, the first thing is to checkout down the code of their own responsibility from SVN, and then run the unit test, if the unit test passes, then the code is not a problem, then the code block to modify and add, complete and then test with JUnit, If there is no problem after the test is completed, then submit the corresponding code block to SVN.
Testing is generally divided into: unit testing, integration testing (mainly see a piece of code added, the system will not have problems), acceptance testing and stress testing.
JUnit was used in previous projects, using JUnit as a function with more than one main method. If a project is very large, the test is very much, if not junit, then the workload is very large. One of the most basic functions of unit testing is the ability to perform automated testing. Unit tests are asserted to determine whether the result is correct, that is, to use assert.
1, download the new version of JUnit from the website, http://search.maven.org/#search%7cgav%7c1%7cg%3a%22junit%22%20and%20a%3a%22junit%22 Download the corresponding version, The download here is junit4.10
2. Create a new Java project in MyEclipse, named Junit01, and create a new folder named Lib, copy the downloaded junit-4.10 to the inside, and build path, which is added to the classpath, as shown in:
3, a new class calcuate, its functions are mainly implemented subtraction, as shown in the package is Cn.whp.util
4. Create a new test class in the above class Calcuate, first create a source folder in MyEclipse, name it test, and under it create a package with the class Calcuate class in the same package as shown in:
5, Junit3 and JUNIT4 difference is still more obvious, in Junit3, if a class is a test class, it must inherit the class TestCase, if a method is a test method, you must let this method start with Testxx, If you want to specify a test method to run before running an initialization method, the name of the method must be setup, and if you want to run a method that frees resources after a test method is run, the name of the method must be teardown.
In Junit4, a Pojo class is a test class, the test method is identified by the @test, the initialization method is identified by the @before, the method of releasing the resource is identified by @after, but in order for the JUNIT4 test class to be used in junit3, It is customary to name the initialization method Setup, and the method of releasing the resource is named Teardown. The test method in test is usually started with the tests. A method that is identified as a before annotation, and each time a test class is run, a method that is identified as @after and @before is executed. As shown in the following:
6, in JUNIT4 provides an Assert class, this class has a large number of static methods to assert the processing, in junit3 due to inherit the testcase, the testcase can assert directly, and JUNIT4 need to first introduce the Assert class. Such as:
The Assertequals method in the Assert class is used in, and the first parameter of this method means: If the result of the method Cal.add (12,22) is not 34, then the "additive problem" message is printed. The second parameter is the result of the execution of method Cal.add (12,22), the third parameter is the result of the developer's expected function Cal.add (12,22), where 12 and 22 are expected to be added after the result is 34, if Rel is not equal to 34 after executing the test method, Then you will be quoted as having problems with addition. If the result equals 34, then this method is tested. Change the value of the last parameter in 34 to 35, then the following error will occur after performing the JUnit test:
If you want the above test class to run in Junit3, you can import the Assert class statically, so that when each static method is called, it is not necessary to write the Assert class, as shown in:
Then complete all the other test methods, using JUnit testing is a lot different than using the Main method test, each method identified as @test is a running method, and they do not affect each other, such as the TESTADDD method has problems, Does not affect the operation of the Testminus method. This is the benefit of unit testing, such as:
7, in the Test division Cal.divide (3,0), if the divisor is 0, this method should throw an exception. The test goal now is that if the test method does not throw an exception after running the test method, then the test method cannot pass. This will require the use of JUnit's arithmeticexception. As shown in the following:
If you change Cal.divide (20,0) to Cal.divide (20,10), then divide is no problem, but the Testdivideexception method in the test class does not pass after JUnit test. Because this test method has asserted that the method to be tested divide to throw an exception, the result does not throw an exception, so the JUnit test cannot be passed. As shown in the following:
The test results suggest that an exception should be thrown and the result is not thrown.
8, sometimes in the test need to have such a requirement, is to test the time of a method, for example, to let a method, 200 milliseconds to run, if the method 200 milliseconds can not run out, then this method should throw an exception, the example will be the method of the thread sleep 300 milliseconds, This method cannot be completed within 200 milliseconds, so this method throws an exception. This can be done in some way performance test, the thread is removed, then the test will be passed normally. As shown in the following:
10. The most basic thing about JUnit is this, assert, test exception, catch exception, test method performance. The example code is in the attachment.
A basic introduction to JUnit learning JUnit