Unit testing with JUNIT4 in Eclipse (text tutorial one)

Source: Internet
Author: User
Tags throw exception

unit Testing with JUNIT4 in Eclipse

    Unit test, JUNIT4.

What is the relationship between the two? This is like unit testing (grass) and (lawn mower). Use this JUNIT4 tool to help us with the test. In fact, do not understand this also does not matter, listen to more see more with more, naturally will understand.

One might think that I could just write a test myself. For example, write a System.out.print output to see if it is equal to what we expected. Bingo! It's also possible. But you don't have a lawn mower, and that's a duang effect.

At first I also felt that there was no need to use the mower (JUnit), but after I learned about the lawn mower (junit), I liked the feeling of the swish (green bar).

If you are still a face puzzled don't know what is JUnit4, do not know unit test, Ok,don ' t worry. Just follow me. Take one step at a pace, and first let you experience the thrill of the swish.

The first step:

Open Eclipse.exe, create a project called "Ives", and create a class named "UnitTest". Write a few methods: two number +-*/method and return result method.

The code is as follows:

1 ImportJava.util.*;2  Public classExpression {3     intA;4     intb;5     Static intC//User Answers6     intAnswer//Answer7     StaticScanner in=NewScanner (system.in);8 9 Ten  Public intExpression () { OneA =NewRandom (). Nextint ()%10; Ab =NewRandom (). Nextint ()%10; -System.out.print ("+a+" + "+b+" = "); -     returnAnswer = a +b; the  - } -  Public Static voidMain (string[] args) { -     intanswer; +Expression expression =NewExpression (); -Answer =expression. Expression (); +     Try{ AEXPRESSION.C =in.nextint (); at     } -     Catch(inputmismatchexception e) -{System.err.println ("\nerror, please Enter a Int number")); -     } -     if(answer==c) -     { inSystem.out.print ("Correct it"); -     } to     ElseSystem.out.print ("Wrong Answer"); +     //System.out.print ("answer=" +answer); - } the}
View Code

Step Two:

duang! The cool effect came, with a tight pace.

will be The JUnit4 Unit Test package introduces this project: Right-click on the item "Ives", Point "Properties" (Properties),

(Java build  Path) < Span style= "FONT-SIZE:14PT;" >, " (Add Libraries) button as shown in:

Click Finish and theJUNIT4 package will be included in our project.

Step Three:

GeneratedJUnit Test framework: In Eclipse 's Package Explorer, Right-click the class " UnitTest "Pop-up menu, select New →junit test case (new à JUnit )。 As shown in the following:

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


When you click Done, it will appear as follows:

Do not delete @test This flag is very important!!!!! Then delete the fail ("not implemented") and knock your code in. I have chosen to test 4 methods, so it will automatically generate 4 test methods.

duang!!!

How do you write the test method? For example, I want to know if the plus method in my unittest is written right, then I'll write a test on this method. Pass a A, b two number, for example A=1 and b=1, that A+b will wait 2, so here we have to understand new things again. That's the JUnit API, and here's the JUnit API, and I'll see if I can put in a few common methods and add examples to make it easier to understand.

To continue our topic: we know a+b=2, so if the result of the Plus method is 2, then it means that our plus does not have this type of bug for the time being. So we're going to use the method.assertEquals(double expected, double actual)这个方法里的double expected是我们期望的值,double actual是实际的值。例如assertEquals(2, a+b),这样就可以测试方法有没有算错了。其实电脑一般都不会算错吧?哈哈

The complete test code is as follows:

1 Import Staticorg.junit.assert.*;2 Importorg.junit.Test;3 4  Public classUnittesttest {5      Public StaticUnitTest Puls =Newunittest ();6      Public Static intanswer;7 @Test8      Public voidTestplus () {9Puls.plus ();TenAssertequals (2, Puls.getresult ()); One     } A  - @Test -      Public voidTestminus () { thePuls.minus (); -Assertequals (0, Puls.getresult ()); -     } -      + @Test -      Public voidtestmultiply () { +Puls.multiply (); AAssertequals (1, Puls.getresult ()); at          -     } -      -@Test (expected=arithmeticexception.class) -      Public voidTestDivide1 () { -Puls.divide (0, 1); in     } -      to @Test +      Public voidtestdivide () { -Puls.divide (); theAssertequals (1, Puls.getresult ()); *     } $}
View Code

A child's shoes that may be carefully watched may find that I have also written a test about whether the exception was thrown. If the divisor is 0 then throw an exception, and then test it with a divisor of 0 that there is no throw exception. Someone might ask, I don't know what's abnormal ah, very simple, look

After we have written the code, the thrill of the swish will soon come.

I changed the "minus" in the Minus method to "plus":

Then run the test class unittesttest. such as:

The code being tested has a bug, that is, it was previously modified to +, so it will fail.

So when all is green is not very cool, there is no feeling of the lawn mower (JUNIT4) of the swish swish of pleasure.

Haha, the tutorial is over here. Do you want more? Keep an eye on my blog, and it may be more detailed in the update.

Personal experience:

1. Test class must see clearly there is no write right, sometimes copy paste without modification will appear error, so to see clearly the method of the call is not.

2. This seems to be really better than writing a test class to test yourself, because there is a green thing that feels fun.

3. I think these small programs do not need to write unit tests, and see a lot of people on the Internet to say that the basic development is not going to write unit testing, but, write more points no matter, learn points better. So I do not want to go after the use, now can learn to learn that to learn, can write to write, then go to write. So sometimes you really don't need to ask why? There are a lot of things you can do now may not be profitable, and may also have no income, then why do you want to do it?

Kotten said, "You believe that after ten years, I even do not know what log is, but also can live well." Shen Jiayi said, I know. Ke Jingteng said, you still study so hard. Shen Jiayi said, life is a lot of things is futile ah! Not every thing must have meaning, experience is also very valuable! 

The words were quoted when they were made. Ha ha

---------This is the end of this blog post------------

--------If there are any errors or shortcomings, please point out. --------

PS: Long-term recruitment of small partners together to form a team to learn software development (Java), can cooperate in the development of software, can also discuss communication, have like-minded small partners can send messages in the station.

Report:

Junit api:http://www.udel.edu/cis/software/dist/junit4.1/javadoc/

Unit testing with JUNIT4 in Eclipse (text tutorial one)

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.