Use junit4 in eclipse for unit testing (1)

Source: Internet
Author: User

Use junit4 in eclipse for unit testing (1)

First of all, let's take a stupid quick tutorial. Don't ask why, follow meFirst, let's experience the pleasure of unit testing!

Create a project named junit_test, We compile a calculator class, which is a calculator class that can easily implement addition, subtraction, multiplication, division, square, and square, and then perform unit tests on these functions. This class is not perfect. We intentionally reserved some bugs for demonstration. these bugs are described in comments. This classCodeAs follows:

 
Public class calculator {Private Static int result; // static variable used to store the running result public void add (int n) {result = Result + N;} public void substract (int n) {result = Result-1; // BUG: correct result = Result-n} public void multiply (int n) {}// public void divide (int n) {result = Result/N;} public void square (int n) {result = N * n ;} public void squareroot (int n) {for (;); // BUG: endless loop} public void clear () {// clear result = 0 ;} public int getresult () {return result ;}}

Step 2: Set junit4The unit test package introduces this project: Right-click the project and click "properties ",

In the displayed Properties window, select "javabuild path" on the left.", Select the" Libraries "tab in the upper right corner, and then click" add Library… "on the rightmost side ..." As shown in:

Then select junit4 in the pop-up dialog box.Click OK, as shown in. The junit4 software package is included in our project.

Step 3: Generate JUnitTest Framework: In the eclipse package explorer, right-click the class pop-up menu and select "New à JUnit test case ". As shown in:

In the pop-up dialog box, make the appropriate selection, as shown in:

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

The system automatically generates a new class calculatortest.Contains some empty test cases. You only need to modify these test cases. The complete calculatortest code is as follows:

Import staticorg. JUnit. assert. assertequals; importorg. JUnit. before; importorg. JUnit. ignore; importorg. JUnit. test; Public classtestcalculator {Private Static calculator CA = newcalculator (); @ before public void setup () throws exception {ca. clear () ;}@ test public void testadd () {ca. add (2); CA. add (3); assertequals (5, CA. getresult () ;}@ test public void testsubstract () {system. out. println (ca. getresult (); CA. add (10); system. out. println (ca. getresult (); CA. substract (2); assertequals (8, CA. getresult () ;}@ ignore ("multiply () Not yetimpelemented") @ test public void testmultiply () {}@ test public void testdivide () {ca. add (8); CA. divide (2); assertequals (4, CA. getresult ());}}

Step 4: run the test code.Right-click the class and select "Run as à JUnit test" to run our test, as shown in:

The running result is as follows:

The progress bar is red to indicate that an error is found. The specific test result is displayed on the progress bar, indicating that "a total of 4 errors have been performed.Tests, one of which is ignored, and one test fails"

Now, we have completely experiencedUsing the JUnit method.

Reprinted from:Http://developer.51cto.com/art/200906/127655.htm

 

 

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.