Junit learning and junit Learning

Source: Internet
Author: User

Junit learning and junit Learning
I. Core assertion Methods

Sample Code:

Package com. test; import org. junit. assert; import org. junit. test;/*** @ Title: testAssert. java * @ Package com. test * @ Description: Common assertions in junit * @ author lky * @ date 9:15:59 on January 1, October 17, 2015 * @ version V1.0 */public class testAssert {/*** @ Title: testAssertByteArrayEqual * @ Description: determines whether the byte array is equal */@ Test public void testAssertByteArrayEqual () {Assert. assertArrayEquals ("byteArray fail to equal", "lky ". getBytes (), "lky ". getBytes ();}/*** @ Title: testAssertEqual * @ Description: checks whether two objects are equal, compares only values, and does not compare their addresses, similar to equal in java */@ Test public void testAssertEqual () {Assert. assertEquals ("fail to equql", 50, 50) ;}@ Test public void testAssertNotEqual () {Assert. assertNotEquals ("shocould be not equal", 50,49);}/*** @ Title: testAssertNotNull * @ Description: determines whether an object is empty */@ Test public void testAssertNotNull () {Assert. assertNotNull ("shocould be not null", new Object () ;}@ Test public void testAssertNull () {Assert. assertNull ("shocould be null", null);}/*** @ Title: testAssertSame * @ Description: determines whether two objects are equal, including values and addresses, similar to = */@ Test public void testAssertSame () {Integer number = Integer. valueOf (10); Assert. assertSame ("shocould be same", number, number) ;}@ Test public void testAssertNotSame () {Assert. assertNotSame ("shocould be not same", new Object (), new Object ());}}
Ii. Core annotation Methods

  • Execution sequence

The execution sequence of a test unit test is:

@BeforeClass –> @Before –> @Test –> @After –> @AfterClass

  • The call sequence of each test method is as follows:

@Before –> @Test –> @After

Sample Code:

Package com. test; import org. junit. after; import org. junit. afterClass; import org. junit. assert; import org. junit. before; import org. junit. beforeClass; import org. junit. ignore; import org. junit. test;/*** @ Title: testAnnotation. java * @ Package com. test * @ Description: annotation test in junit test * @ author lky * @ date 9:36:39 on January 1, October 17, 2015 * @ version V1.0 */public class testAnnotation {private static Demo demo = null; /*** @ Title: testBeforeClass * @ Description: runs when the class is loaded. Only one operation is executed. */@ BeforeClass public static void testBeforeClass () {demo = new Demo (); System. out. println ("Test ---------> testBeforeClass");}/*** @ Title: testBefore * @ Description: before each Test is executed, automatically called */@ Before public void testBefore () {System. out. println ("Test ----------> testBefore");}/*** @ Title: testAdd * @ Description: the code for testing */@ Test public void testAdd () {Assert. assertEquals (5, demo. add (2, 3);}/*** @ Title: testAdd1 * @ Description: ignore indicates ignoring the Test */@ Ignore @ Test public void testAdd1 () {Assert. assertEquals (4, demo. add (2, 2);}/*** @ Title: testAfter * @ Description: each test is called After execution */@ After public void testAfter () {System. out. println ("Test -----------> testAfter");}/*** @ Title: testAfterClass * @ Description) * // @ AfterClass public static void testAfterClass () {System. out. println ("Test ------------> testAfterClass ");}}
Iii. parametric Testing
Sometimes, different parameter values in a test method produce different results. To test the completeness of the test, multiple parameter values are written and tested one by one, in this way, it is sometimes time-consuming and labor-consuming, so we can use parameterized tests to solve this problem. Parameterization testing is like passing in a set of "input value, expected value" to the test method to achieve one-time testing.

Sample Code:

package com.test;public class Demo {        public int add(int a,int b){        return a+b;    }}
Package com. test; import java. util. arrays; import java. util. collection; import org. junit. assert; import org. junit. beforeClass; import org. junit. test; import org. junit. runner. runWith; import org. junit. runners. parameterized; import org. junit. runners. parameterized. parameters;/*** @ Title: testParam. java * @ Package com. test * @ Description: unit test (parametric test) for multiple groups of data * @ author lky * @ date 9:20:54 on January 1, October 17, 2015 * @ version V 1.0 * // do not use the default junit runner. Specify the specific runner @ RunWith (Parameterized. class) public class testParam {private int expected; private int input1; private int input2; private static Demo; public testParam (int expected, int input1, int input2) {this. expected = expected; this. input1 = input1; this. input2 = input2;}/*** @ Title: initData * @ Description: Test Data initialization * @ param @ return setting file * @ return Collection <? Extends Object> return type * @ throws */@ Parameters (name = "Group {index}: -------> {1} + {2 }={ 0 }") public static Collection <? Extends Object> initData () {return Arrays. asList (new Object [] [] {, 1}, {5,-}, {-7,-3,-4, 4 }}) ;}@ BeforeClass public static void loadUp () {demo = new Demo () ;}@ Test public void testAdd () {Assert. assertEquals ("shocould be equal", this. expected, demo. add (this. input1, this. input2 ));}}
Iv. Exception Test

Sample Code:

Package com. test; import java. util. arrayList; import java. util. list; import org. junit. assert; import org. junit. rule; import org. junit. test; import org. junit. rules. expectedException;/***** @ Title: testException. java * @ Package com. test * @ Description: exception Test (two methods) * @ author lky * @ date 10:00:32 on January 1, October 17, 2015 * @ version V1.0 */public class testException {@ test (expected = IndexOutOfBoundsException. class) publi C void empty () {new ArrayList <Object> (). get (0) ;}@ Rule public ExpectedException thrown = ExpectedException. none (); @ Test public void shouldTestExceptionMessage () throws IndexOutOfBoundsException {List <? Extends Object> list = new ArrayList <Object> (); thrown. expectMessage ("Index: 0, Size: 0"); thrown. reverse CT (IndexOutOfBoundsException. class); list. get (0); Assert. assertEquals (1, list. get (0 ));}}
5. Time-out testing sometimes requires time-limited testing to prevent endless loops or method execution from being too long (or to check method efficiency. As the name suggests, testing fails if the specified time is exceeded. There are two writing methods

Sample Code:

Package com. test; import org. junit. rule; import org. junit. test; import org. junit. rules. timeout;/*** @ Title: testTimeOut. java * @ Package com. test * @ Description: timeout test * @ author lky * @ date 9:58:18, January 1, October 17, 2015 * @ version V1.0 */public class testTimeOut {// defines the time parameter of the method to be tested, @ Rule public Timeout timeout = new Timeout (10000); @ Test public void test () {}@ Test (timeout = 10000) public void test1 (){}}
6. Packaging and Testing

If a project contains many test cases, it is also difficult to perform one test. Therefore, testing by packaging is all the test cases contained in the one-time test completion package.

Sample Code:

Package com. test; import org. junit. runner. runWith; import org. junit. runners. suite; import org. junit. runners. suite. suiteClasses;/*** @ Title: testSuite. java * @ Package com. test * @ Description: package test * @ author lky * @ date 10:05:25, January 1, October 17, 2015 * @ version V1.0 */@ RunWith (Suite. class) @ SuiteClasses ({testAnnotation. class, testAssert. class, testParam. class, testException. class, testTimeOut. class}) public class testSuite {}
VII. Use in Spring framework

Learning

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.