Add JUnit unit testing in the Intellij IDEA

Intellij IDEA added JUnit unit testing

JUnit is a very famous Unit Testing framework is used in a lot of projects
on the Internet to find a lot of use JUnit in IDEA Tutorial do not fly
and finally a personal blog reference test is successful, as a record, after use for easy access
is also available to those who need access to
the main content of this article reproduced from: the pilot's personal blog

Download jar package

You first need to download the JUnit jar package and a dependent package hamcrest.core, they download the following address:
JUnit: JUnit: 4.13-Beta-1
hamcrest.core: 1.3.0.1
Note that hamcrest.core package version 2.1 can not be used because the 4.13 version of JUnit and it is not compatible, it will throw an ClassNotFoundExceptionexception.

Recommended junit-4.13.jarand hamcrest-core-1.3.jar, otherwise there will be unexpected trouble

In addition, the two addresses above is particularly slow, basically not been downloaded, here is another of a third-party website https://jar-download.com/

Add jar wrapped in Intellij IDEA project

First, we need to add just download the IDEA good package:

Remember to check the Export option.

Download the plug-in and set

Then you need to download a plug-in called JUnitGeneratorV2.0 of:

Then set it up:

You can change the output path to the desired path
in the position shown above change in the output path does not seem affected. . .

The picture $datachanged $todayin order to prevent the generated test class garbled.

Create a directory test file

You need to create a new folder in the Project, for storing the automatically generated test .java files, such as Factorial.javathe class corresponding to the FactorialTest.javastorage location of the file
where I create a new directory, and the same level directory scr, FIG.
image-20200410001446930

Next you need this folder to store directory generate test file
to open the project settings
image-20200410001551607

image-20200410001659216

Using JUnit

When it is desired to add a test code for the current class, only need to use the Alt + inset (or click the navigation bar Code-Generator) shortcuts the current class, select JUnit-JUnit4, automatically generated test class of the current class:

Plug-default test all methods, using shortcuts Ctrl+Shift+Tto test part of the method of selectivity, is very easy:

In the output path can see the test class is automatically generated, the method comprising testing required, then you can write code to test it for the class

Guess you like

Origin www.cnblogs.com/tangg/p/12670753.html