Unit testing with JUNIT4 in Eclipse (beginner)

Source: Internet
Author: User

This article transferred from: http://blog.csdn.net/andycpp/article/details/1327147

most of the content in this article is quoted from this article: HTTP://WWW.DEVX.COM/JAVA/ARTICLE/31983/0/PAGE/1

when we write large programs, we need to write thousands of methods or functions that may be powerful, but we only use a small subset of the functions of the function in our program, and by debugging we can determine that a small subset of the functionality is correct. However, we should also make sure that each function is completely correct, because if we extend the program in the future and use the other functions of a function, which is a bug , it is definitely a very depressing thing. So, after each function is written, all aspects of the function should be tested, which we call unit tests. Traditional programming, unit testing is a very troublesome thing, you have to re-write another program, in the program to call the method you need to test, and carefully observe the results of the operation to see if there are errors. Because of this trouble, programmers are not very enthusiastic about writing unit tests. So there was a cow who launched the unit test package, which greatly simplifies the work of unit testing, which is JUnit4. This article briefly describes the method of unit testing using JUnit4 in Eclipse3.2 .

first, let's take a stupid crash tutorial and don't ask why, Follow Me , let's experience the thrill of unit testing first!

First, create a new project called junit_test , we write a Calculator class, a calculator class that can simply implement subtraction, square, and root, and then unit-test these functions. This class is not perfect, and we deliberately keep some bugs for demonstration, which are described in the comments. This type of code is as follows:

Package andycpp;

public class Calculator {
private static int result;//Static variables for storing run results
public void Add (int n) {
result = result + N;
}
public void substract (int n) {
result = Result-1;//bug: The right should be the result =result-n
}
public void Multiply (int n) {
}//This method has not been written well
public void divide (int n) {
result = result/n;
}
public void Square (int n) {
result = n * n;
}
public void SquareRoot (int n) {
for (;;); //bug: Dead Loop
}
public void Clear () {//Clear the results 0
result = 0;
}
public int GetResult () {
return result;
}
}

The second step is to JUNIT4 The Unit Test package introduces this project: Right-click on the item, click "Properties",


In the pop-up Properties window, first select "Java Build Path", then go to the top right and select"Libraries"label, then click on the far right."Add Library ..."button, as shown in:


then, in the New Popup dialog box, selectJUNIT4and click OK, as shown,JUNIT4The software package is included in our project.

The third step is to generateJUnittest framework: inEclipseof thePackage ExplorerRight-click on the Category pop-up menu and select "NewàJUnit Test case". As shown in the following:


in the popup dialog box, make the appropriate selections as shown in:


after clicking "Next", the system will automatically list the methods contained in your class and choose the method you want to test. In this example, we only test the "add, subtract, multiply, divide" four methods. As shown in the following:


a new class is automatically generated after the system calculatortest , which contains some empty test cases. You only need to make these test cases slightly modified to use. The complete calculatortest code is as follows:

Package andycpp;

Import static org.junit.assert.*;
Import Org.junit.Before;
Import Org.junit.Ignore;
Import Org.junit.Test;

public class Calculatortest {

private static Calculator Calculator = new Calculator ();

@Before
public void SetUp () throws Exception {
Calculator.clear ();
}

@Test
public void Testadd () {
Calculator.add (2);
Calculator.add (3);
Assertequals (5, Calculator.getresult ());
}

@Test
public void Testsubstract () {
Calculator.add (10);
Calculator.substract (2);
Assertequals (8, Calculator.getresult ());
}

@Ignore ("Multiply () Not yet implemented")
@Test
public void testmultiply () {
}

@Test
public void Testdivide () {
Calculator.add (8);
Calculator.divide (2);
Assertequals (4, Calculator.getresult ());
}
}

Fourth step, run the test code: After the above code has been modified, we calculatortest Right-click on the class and select Run as à JUnit Test "To run our tests, as shown in:

The results of the operation are as follows:

progress bar is red color indicates found error, the specific test results on the progress bar above said "a total of 4 a test, where 1 A test was ignored and one failed "

at this point, we have fully experienced the methods of using JUnit in Eclipse. In the next article, I'll explain in detail every detail in the test code!

Unit testing with JUNIT4 in Eclipse (beginner)

Related Article

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.