junit learning (1) - the basic use of junit

I have heard about junit for a long time, but I have only learned how to use it in general. A few days ago, I saw a related video on the MOOC online (learning link click here ), so I followed the system and took a note here. (PS: A good memory is not as good as a bad writing, and I forget it after a long time)

 

As for the basic information such as junit, I won't say it here. You can go to this link to learn, and it will be explained in detail. The use of junit is directly used here. Proceed as follows:

 

1. Package guide : I use Junit4 for testing , so I need to guide two packages: junit-4.12.jar and hamcrest-core-1.3.jar , which have been uploaded to the attachment and can be downloaded if necessary.

 

2. Create a new class to be tested : create a new package under src, create a new basic class, and write several methods . My package here is called: com.wjl.junit, the class name is: Calculate, there are four methods of addition, subtraction, multiplication and division. The code is as follows:

package com.wjl.junit;

public class Calculate {
	//addition
	public int add(int a,int b){
		return a+b;
	}
	
	//subtract
	public int subtract(int a,int b){
		return a-b;
	}
	
	//multiplication
	public int multiply(int a,int b){
		return a*b;
	}
	
	//division
	public int divide(int a,int b){
		return a/b;
	}
}

 

3. Create a new test class : first create a new Source Folder (right-click on the project---->new---->Source Folder, src is a Source Folder) and name it test, which is specially used for unit test classes. Then create a new package with the same name as the previous step , here is com.wjl.junit, and then create a new Junit Test Case (note that it is Junit Test Case, not Class, I don't know how to build it? Right-click on the newly created package ---->New---->Other....---->Search Junit Test Case in the search box of the pop-up box), my name here is: CalculateTest, and finally add each method in Calculate to test method . The code after writing is as follows:

package com.wjl.junit;

import org.junit.Test;
import static org.junit.Assert.*;
/**
 * Junit_demo_1
 * Handwritten Calculate test method for each method
 * **/
public class CalculateTest {
	/**
	 * Basic specifications:
	 * 1. The test method must be modified with @Test
	 * 2. The test method must be modified with public void and cannot take any parameters
	 * 3. Create a new source code directory to store the test code
	 * 4. The package name of the test class should be consistent with the tested class
	 * 5. Each method in the test unit must be independently testable, and there must be no dependencies between test methods
	 * 6. The test class uses Test as the suffix of the class name (not required, but it is standard to write this way)
	 * 7. The test method uses test as the prefix of the method name (not required, but it is standard)
	 * **/
	@Test
	public void testAdd(){//Addition
		assertEquals(5,new Calculate().add(3,2));//Assert: 3+2=5 (that is, the calculation result of the add() method is the previous preset result)
	}
	
	@Test
	public void testSubtract(){//Subtraction
		assertEquals(1,new Calculate().subtract(3, 2));
	}
	
	@Test
	public void testMultiplay(){//Multiplication
		assertEquals(6,new Calculate().multiply(3, 2));
	}
	
	@Test
	public void testDivide(){//Division
		assertEquals(2,new Calculate().divide(6, 3));
	}
}

 

4. Run : Right-click in the blank space of the CalculateTest file ---->Run As---->JUnit Test , if all 4 methods are asserted successfully , then the stripe on the right will be green , as shown below:

If 4one of the methods fails, the stripes will turn red , as shown in the following figure:

I personally think that this kind of prompt is very convenient. The left side tells you which method succeeded (green mark) and which method failed (blue mark) For example, it can be seen from the above figure: the three methods below are successful, only the first method fails, click on the method in question, it will automatically locate the wrong place in the Junit Test Case. The right side tells you what went wrong with that method . The result of my calculation here should be 5. I wrote it as 6, so I was wrong.

 

According to the above execution method, all the methods with @Test in the Test Case are executed, so what if I only want to execute a certain method ? two methods.

First: Select the method name in Test Case---->Right click---->Run As---->JUnit Test

Second: In the left view of MyEclipse, find CalculateTest, and then expand it, as shown below:

Then find the method you want to execute separately, select ----> right click ----> Run As----> JUnit Test .

At this point, the use of a Junit Test Case is completed.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326354329&siteId=291194637