The path of Java cultivation--junit sharp weapon

Source: Internet
Author: User

JUnit Super Super Starter case

Directly on the code demo:

For example, you have now written a ArrayList class method:

 Public classArrayListImplementsList {Private intSize = 0; Privateobject[] Elementdata =Newobject[100];  Public voidAdd (Object o) {} Public voidAddintindex, Object o) {            }         PublicObject Get (intindex) {        returnObject; }         PublicObject Remove (intindex) {        returnObject; }         Public intsize () {return-1; }  }

What if we want to test now? At this time the JUnit weapon can be used;

First we set up a JUnit class :

If you right-click directly on the class you want to test, you can tick the test method as follows:

And then just start adding methods to it:

 Public classArraylisttest {
The @Test here is a must-have comment that tells JUnit that this is a test method @Test Public voidTestget () {Staticobject[] Data =Newobject[]{1,2,3,4,5,6,7,8}; ArrayList test; //add data to test ......... ......... //testing test here to say,
Out to assertequal judgment value is equal, in fact there are assertfalse,assertnull and other methods to judgeAssertequal (Data[1],test.get (1)); } }

Yes, that's the simple thing. If your class is not a problem, you should be able to return to this diagram:

However, there is no finding that if you are testing other methods, such as Add,remove, it is necessary to have a pre-populated data ArrayList bar. Do I need to do this every time I run a test?

This is too redundant. This time, JUnit has its own tricks:

 Public classArraylisttest {Staticobject[] Data =Newobject[]{1,2,3,4,5,6,7,8};           ArrayList test; //This before annotation can be interpreted as://This setup is executed before each @test modification method is equal to the pre-condition@Before Public voidSetUp ()throwsexception{Test=NewArrayList ();  for(Object data:data) {test.add (data); }    }        //test function;}

Now it's almost done, but what happens when there are multiple test classes, running one after the other? Isn't that the same as creating the Main method test?

So JUnit also provides a test suite group:

//You now have 3 test classes Public classtest1{@Test Public voidTest () {//... Test1    }} Public classtest2{@Test Public voidTest () {//... Test2    }} Public classtest3{@Test Public voidTest () {//... Test3    }}//Create a test suite class (the test suite can overlay each other):@RunWith (Suite.class) @Suite. suiteclasses ({test1.class,test2.class,test3.class                    }) Public classSuittest {//Must be public decorated, empty class}

In addition to the comments written above actually there are quite a lot of, but it is not useless to say the first. (The dregs don't use these, escape.) ), I'll post it to let the crossing look first:

That's all you have to say, just getting started. There is no advanced technology ah, thought or anything. Simple:-/.

In the end want to share a sentence:

Test cases are used to prove that you are not wrong, not to prove that you are right.

I personally feel that this is really speaking to the heart.

To take care of, retreat from (.? _?) /~~~

The path of Java cultivation--junit sharp weapon

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.