Java cultivation path -- Junit and junit

Source: Internet
Author: User

Java cultivation path -- Junit and junit
JUnit super beginner case

 

Go directly to the Code Demo:

 

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

public class ArrayList implements List {     private int size = 0;        private Object[] elementData = new Object[100];        public void add(Object o){            }    public void add(int index, Object o){            }        public Object get(int index){        return Object;    }        public Object remove(int index){        return Object;    }        public int size(){        return -1;    }  }

What should I do if I want to test it now? At this time, the Junit tool can be used;

First, create a Junit class:

 

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

 

Then, you can directly add the method to it:

Public class ArrayListTest {
// Here @ Test is a required annotation, that is, to tell JUnit that this is a Test method @ Test public void testGet () {static Object [] Data = new Object [] {1, 2, 3, 4, 5, 6, 7, 8}; ArrayList test; // Add data to test .................. // test. Let's talk about it here,
// If assertEqual is required to determine whether the value is equal, there are also assertFalse, assertNull and other methods to determine assertEqual (Data [1], test. get (1 ));}}

Yes, that's just that simple. If your class is okay, you should be able to return the following figure:

 

 

However, have you found that if you are testing other methods, such as add and remove, there must be an ArrayList with data pre-filled. Do I need to perform this operation every time I perform a test?

This is too redundant. At this time, JUnit has its own tips:

Public class ArrayListTest {static Object [] Data = new Object [] {1, 2, 3, 4, 5, 6, 7, 8}; ArrayList test; // This before annotation can be understood: // execute this setUp Before executing each @ Test modifier, which is equal to the same as the precondition @ Before public void setUp () throws Exception {test = new ArrayList (); for (Object data: Data) {test. add (data) ;}// test function ;}

Now it seems to be almost done, but what should I do when there are multiple test classes and run them one by one? Isn't this the same as creating a main method for testing?

Therefore, Junit provides another test suite group:

// You now have three Test classes: public class Test1 {@ test public void Test (){//... test1} public class Test2 {@ Test public void test (){//... test2} public class Test3 {@ Test public void test (){//... test3 }}// create a test Suite class (the test Suite can be stacked with each other): @ RunWith (Suite. class) @ Suite. suiteClasses ({test1.class, test2.class, test3.class}) public class SuitTest {// must be public-modified, empty class}

 

In addition to the comments written above, there are actually a lot of comments, but I won't talk about it before I use it. (Too scum to use these, escape ..), I will post it to show the audience:

 

That's all. It's just getting started. There is no advanced technology or thought. Simple :-/.

I want to share one sentence at the end:

Test Cases are used to prove that you are not wrong, rather than proving that you are right.

I personally think this is really true.

 

Get it done, retreat (. Required _ parameters ._.)/~~~

 

 

 

 

 

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.