inotgo.com

Download area
File name File size
junit-4.9.jar 243k
junit.rar 211k
Step 1 : use main Test   
Step 2 : main The problem is   
Step 3 : junit   
Step 4 : Operation mode   
Step 5 : Run successfully   
Step 6 : Add a new test method   
Step 7 : Run failed   
Step 8 : junit The benefits of   
Step 9 : before & after   
Step 10 : Runnable project   
Step 11 : Assert   

When you first started writing code , Can use main Function to run . By the way, test the code written , Whether it operates as expected .
For example, the following code , To test sum Whether the method performs as expected .
package junit; public class SumUtil { public static void main(String[] args) { int result = sum1(1,2); System.out.println(result); } public static int sum1(int i, int j) { return i+j; } }
package junit;

public class SumUtil {

	public static void main(String[] args) {
		int result = sum1(1,2);
		System.out.println(result);
	}

	public static int sum1(int i, int j) {
		return i+j;
	}
	
}
Through main The problem with methods to test is obvious .
For example, a newly developed method , Yes 3 Sum the numbers , Then there are at least two problems :
1. Modify based on the original test , May break the original test logic
2. The test succeeded , still i The test failed , I don't even know ... It can only be observed with the naked eye , If the amount of testing is large , It's ugly. Come here
package junit; public class SumUtil { public static void main(String[] args) { int result = sum1(1,2); System.out.println(result); int result2 = sum2(1,2,3); System.out.println(result2); } public static int sum1(int i, int j) { return i+j; } public static int sum2(int i, int j, int k) { return i+j+k; } }
package junit;

public class SumUtil {

	public static void main(String[] args) {
		int result = sum1(1,2);
		System.out.println(result);
		int result2 = sum2(1,2,3);
		System.out.println(result2);
	}

	public static int sum1(int i, int j) {
		return i+j;
	}
	
	public static int sum2(int i, int j, int k) {
		return i+j+k;
	}
	
}
In order to meet the needs of this test , We need to use junit Test framework to test .
First download jar Bag : Put it in the upper right corner junit-4.9.jar, Import into project .
Then create TestCase1 class , And create a method :

@Test
public void testSum1() {
int result = SumUtil.sum1(1, 2);
Assert.assertEquals(result, 3);
}

It is made of @Test Annotated , Indicates that this method is a test method
Assert.assertEquals(result, 3); Say yes to result The numerical expectation is 3, In case of other values , You can't pass the test .
package junit; import org.junit.Test; import junit.framework.Assert; public class TestCase1 { @Test public void testSum1() { int result = SumUtil.sum1(1, 2); Assert.assertEquals(result, 3); } }
package junit;

import org.junit.Test;

import junit.framework.Assert;

public class TestCase1 {

    @Test
    public void testSum1() {
    	int result = SumUtil.sum1(1, 2);
    	Assert.assertEquals(result, 3);
    }

}
With main Methods run differently , When running test cases , You need to choose Run As -> JUnit Test Way
 Operation mode
After running successfully , It will display as shown in the figure , It's green
 Run successfully
next , We added a new testSum2 method , For testing sum2 method .
This time , We deliberately wrote

Assert.assertEquals(result, 5);

See what happens if the test fails .
package junit; import org.junit.Test; import junit.framework.Assert; public class TestCase1 { @Test public void testSum1() { int result = SumUtil.sum1(1, 2); Assert.assertEquals(result, 3); } @Test public void testSum2() { int result = SumUtil.sum2(1, 2,3); Assert.assertEquals(result, 5); } }
package junit;

import org.junit.Test;

import junit.framework.Assert;

public class TestCase1 {

    @Test
    public void testSum1() {
    	int result = SumUtil.sum1(1, 2);
    	Assert.assertEquals(result, 3);
    }

    @Test
    public void testSum2() {
    	int result = SumUtil.sum2(1, 2,3);
    	Assert.assertEquals(result, 5);
    }
}
After running , Because result The value of is 6, It doesn't mean 5, So after failure , See the effect shown in the figure , Indicates that the test failed .
It also gives the number of failed lines : TesetCase1.java:18
 Run failed
Now look back junit Solved main The problem is In 2 One question
1. New tests , No effect on the original test
2. If the test fails , Will be notified immediately
@Before @After It is also a common test framework annotation , Separate things to do before the test starts , And what you do after it .
before & after
package junit; import org.junit.After; import org.junit.Before; import org.junit.Test; import junit.framework.Assert; public class TestCase1 { @Before public void before() { System.out.println(" Preparation before test , For example, link database and so on "); } @After public void after() { System.out.println(" Work after the test , Such as closing links and so on "); } @Test public void testSum1() { int result = SumUtil.sum1(1, 2); Assert.assertEquals(result, 3); } @Test public void testSum2() { int result = SumUtil.sum2(1, 2,3); Assert.assertEquals(result, 5); } }
In the upper right corner, there is the runnable Project Download corresponding to this knowledge point , I really can't do it myself , Just download and unzip it and compare it .
about Assert The usage of , Except for judging whether the numbers are equal in the example , You can also judge boolean, Whether it is null , Common logic such as whether it is the same object .
The usage is in the screenshot , It's simple , If you are interested, just try one by one
Assert


The official account of programming , Follow and get the latest tutorials and promotions in real time , thank you .


Q & A area    
2021-08-10 Tips : If used IDEA, And the project is Maven Engineering words , The test code needs to be placed in the creation maven Automatically generated when test Under the bag
Orange watermelon

Tips : If used IDEA, And the project is Maven Engineering words , The test code needs to be placed in the creation maven Automatically generated when test Under the bag







The answer has been submitted successfully , Auditing . Please My answer Check the answer record at , thank you
answer Or code please Fill in at least one , If you have a problem , Please ask again , Otherwise, the webmaster may not see




2020-10-27 eclipse Right click No Run as junit
Ocarina

Imported jar Package, but the right-click is dead or alive junit, Baidu also failed to solve the problem , Do you have any friends who have the same problem ?




1 One answer

BianJiang
Answer time :2020-11-01
I'm right clicking with @Test The function of , There will be that junit Options for



The answer has been submitted successfully , Auditing . Please My answer Check the answer record at , thank you
answer Or code please Fill in at least one , If you have a problem , Please ask again , Otherwise, the webmaster may not see




2020-03-09 idea can't find runas option
2020-02-28 Unit testing at work
2020-01-04 about @before,@after and @beforeclass ,@afterclas


Too many questions , Page rendering is too slow , To speed up rendering , Only a few questions are displayed on this page at most . also 5 Previous questions , please Click to view

Please... Before asking questions land
The question has been submitted successfully , Auditing . Please My question Check the question record at , thank you
about JAVA application -junit- Introductory tutorial Your questions

Try to provide Screenshot code and Abnormal information , Help to analyze and solve problems . You can also enter this station QQ Group communication : 496725845
Ask questions and try to provide complete code , Environment description , The more conducive to the recurrence of the problem , The faster your question can be answered .
Have questions about the code in the tutorial , Please provide which step , Which line is in doubt , This makes it easy to quickly locate the problem , Improve the speed at which questions are answered
In the thousands of questions that already exist , A considerable proportion , Because of the use of and webmaster Different versions of the development environment Resulting in , For example jdk, eclpise, idea, mysql,tomcat Wait, the version of the software is inconsistent .
Please use the same version as the webmaster , You can save yourself a lot of learning time . The webmaster sorted out the software versions used in teaching , It's all here , Convenient for everyone to download : /k/helloworld/helloworld-version/1718.html

Upload screenshot