IntelliJ IDEA 2018 version installs Junit (unit test) plug-in tutorial

                Operating environment: Windows 10

                Operating software: IntelliJ IDEA 2018

        Open idea and create a new project -day01 (example).

        Step 1: Create two new classes in the src directory of the project - Calculator and StringCheck.

 

The specific code of the Calculator class is as follows:

public class Calculator {
    public int add(int one,int another){
        return one+another;
    }
    public  int multiply(int one,int another){
        return one*another;
    }
}

        The specific code of the StringCheck class is as follows:

import org.junit.Assert;

import org.junit.Test;

import java.util.Calendar;



public class StringCheck {

   @Test

   public void testAdd() throws Exception{

      Calculator calculator=new Calculator();

      int sum=calculator.add(1,2);

      Assert.assertEquals(3,sum);

   }

   @Test

   public void testMultiply()throws Exception{

      Calculator calculator=new Calculator();

      int product =calculator.multiply(2,4);

      Assert.assertEquals(8,product);

   }

}

        Open the project, click File-Settings in the upper right corner. As shown below:

        

         Click Plugins - click Browse repositories at the bottom right. As shown below:        

        Search Junit in the search box, select the one pointed by the red arrow in the picture, and then click the green Install button on the right to download. As shown below:

        After the download is complete, restart Idea, and then click File——Settings in the upper right corner. In the pop-up window, select Other Settings at the bottom. As shown below:

        Change the position indicated by the red arrow in the figure to Junit 4. As shown below:

        Then click Apply in the lower right corner to apply the modified options. As shown below:

Then click File——Project Structure in the upper right corner.

 Next, import the jar package (the jar package is located in the lib of the idea installation path)

        

Enter this page, click the + sign, then click JARs or..., find your own idea installation path, find the lib folder, and find the two jar packages corresponding to the figure below.

 

 

 

Then click Sources to enter this interface. Click on the root folder.

 

 

Right-click and select New Folder... to create a folder named test.

 

Then select the test folder you just created, and select the green Tests (as shown in the figure). Then close this page.

 

Click to open the Calculator class, then right-click, click the Go To option, and then click Test. As shown below:

Then on the pop-up page (shown below), click the Create New Test... button.

 

Click to enter this interface, and then check the functions as shown in the figure below.

 

      Click OK, and the following CalculatorTest class will pop up.

 

      Put the mouse anywhere in the CalculatorTest class, then click the right mouse button and click Run. As shown below:

 

The running results are as follows, which proves that the installation has been successful. :

 

 

        At this point, Junit has been installed successfully. The corresponding operations can be performed. If this article is helpful to you, please leave a free like and collection! ! !

        Thank you so much! ! !

Guess you like

Origin blog.csdn.net/m0_56417836/article/details/126646649