Tianyi 32 -- simple Java JUnit Application

Source: Internet
Author: User
I. Step 1: Create a JUnit test project
2. Add the junit4 unit test package to introduce the project. Right-click the project and click "properties ",

3. In the pop-up property window, select "Java build path" on the left, select the "Libraries" tab on the top right, and click "add Library…" on the rightmost side ..." As shown in:

4. Select the JUnit option in the pop-up dialog box and click Next, as shown in next. Select junit4 and click Finish to include junit4 software package into our project.

5. Create a junittest test class in the test project. The Code is as follows:

Public class junittest {Private Static int results; Public int add (int I, Int J) {return I + J;} public void chengfa (int I, Int J) {Results = I * j;} public void beforemehtodd () {system. out. println ("Run this method before every execution of JUnit !!!! ");} Public void aftermehtodd () {system. Out. println (" this method is executed after JUnit is executed every time !!!! ");} Public int getresult () {return results;} public void ignore () {system. Out. println (" Ignore ");}}

6. generate JUnit test framework: In package explorer of myeclipse, right-click the junittest test class pop-up menu and choose "New ----- >>> JUnit Test Case" (if the right-click is not in the new list, select the ohers option for search ). As shown in:1)

2)

7. After the selection is complete, select the appropriate option in the pop-up dialog box, as shown in:

8: After you click "Next", the system will automatically list the methods contained in your class and select the method you want to test. As shown in:

9: click "finish". The system will automatically generate a new class junittesttest, which contains some empty test cases. You only need to modify these test cases. The complete junittesttest code is as follows:

import static org.junit.Assert.*;import org.junit.After;import org.junit.Before;import org.junit.Ignore;import org.junit.Test;public class JunitTestTest {JunitTest jt=new JunitTest();@Beforepublic void setUp() throws Exception {jt.beforeMehtod();}@Afterpublic void setUp1() throws Exception {jt.afterMehtod();}@Testpublic void testGetAdd() {//jt.getSum(1,2);assertEquals(6,jt.add(4, 2));}@Testpublic void testGetChengFa() {jt.chengFa(2, 3);assertEquals(5,jt.getResult());}@Ignore@Testpublic void testGetIgnore() {jt.ignore();}}

10. Run the test code: After modifying the code, right-click the junittesttest class and choose "Run as ---- >>> JUnit test" to run the test, as shown in:

11. Running result:

12: The result in the red box in the upper left corner indicates that the method is successfully executed, the difference indicates that the method fails, and the slash indicates that the method is ignored (three methods are executed in this test, one success, one failure, and one ignored)
Expansion (found on the Internet) I. Time-limited testing If you encounter an endless loop during the test, your face will never smile. Therefore, for programs with complicated logic and deep nested loops, an endless loop may occur. Therefore, some preventive measures must be taken. Time-limited testing is a good solution. We set an execution time for these test functions. If the execution time is exceeded, they will be forcibly terminated by the system, and the system will report to you that the reason for the function to end is timeout, in this way, you can find these bugs. To implement this function, you only need to add a parameter to @ test. The Code is as follows:
@ Test (timeout = 1000)
Public void squareroot ()... {calculator. squareroot (4 );
Assertequals (2, Calculator. getresult ());}
 
The timeout parameter table shows the time you want to set, in milliseconds. Therefore, 1000 represents 1 second.
Ii. Test exceptions Exception Handling in Java is also the focus, so you often write some functions that need to throw exceptions. So, such
If you think a function should throw an exception but it does not throw it, isn't it a bug? This is of course a bug, and JUnit has taken this into consideration to help us find this bug. For example, the calculator class we wrote has the Division function. If the divisor is 0, an exception except 0 must be thrown ". Therefore, we need to test these features. The Code is as follows:
 
@ Test (expected = arithmeticexception. Class)
 
Public void dividebyzero ()...{
Calculator. Divide (0 );
}

As shown in the above Code, we need to use the expected attribute marked by @ test to pass the exception we want to check to him, in this way, the JUnit framework can automatically help us detect whether a specified exception has been thrown.

More detailed and complex JUnit information can be searched and exchanged on the Internet !!!!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.