JUnit 4 uses

1. Import junit4jar package:

In eclipse, right-click bulid path on the project, then click Add libraries, and select JUnit

2. First use

First, create a java project as follows:

Demo. Java content:

Then right-click SRC to add a source folder named test, and create the package CN. Under test orlion. Demo (the package name should be consistent with the package name to be tested), then right-click a new JUnit test case in this package and name it demotest.

There are some specifications to follow when writing test classes:

(1) the test method must be decorated with @ test; (2) the test method must be decorated with public void without parameters; (3) a new source code directory needs to be created to store the test code; (4) the package name of the test class must be consistent with the package name of the tested class; (5) each method in the test unit must be able to test independently and there can be no dependency between methods (6) The test class uses test as the suffix (7) and the test method uses test as the prefix

The code is as follows:

The current project structure is shown in the figure

In demotest Right click Run as - > JUnit test for Java class, and the results are as follows:

(failures are generally caused by test method interrupts, which indicates that there is a problem at the test point and proves that the results returned by the tested method are different from what we expected

Errors are generally caused by the tested method or exceptions in the test method.)

The status bar is green, and all three methods are tested successfully

3. JUnit operation process

(1) The @ beforeclass modified method will be executed before all methods are executed. This method is static, so it will be run after the test class is loaded. There will only be one copy in memory, which is suitable for loading the configuration file

(2) The method modified by @ afterclass modifier will be executed after all methods are executed. It is usually used to clean up resources, such as closing the flow

(3) @ before and @ after will be executed once before and after each test method.

4. JUnit common annotations

(1)@Test

@ test has an expected attribute and a timeout attribute (@ test (expected = NullPointerException. Class), @ test (timeout = 1000))

The expected attribute can capture the exceptions expected to be thrown in the test method and the tested method, and the timeout limits the execution time of the method

(2)@Ignore

The @ ignore modified method will not be executed. You can make a comment @ ignore ("this method is ignored"): this method is ignored

(3) @ runwith can change the test runner org.junit.runner.runner

5. Test Suite

With the gradual improvement of project functions, there will be more and more test classes. When testing, we can't execute them one by one, so we have a test suite, that is, we can include several test classes in an entry class, and then we can execute several test classes as long as we execute the entry class

The entry class must be empty, as follows:

6. JUnit parameterization settings

In the test, many test codes are basically the same, so parametric settings can be used

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>