A preliminary understanding of JUnit

Source: Internet
Author: User

junit4.x

(1), when using the junit4.x version of unit testing, do not have to test the class to inherit the TestCase parent class, because, junit4.x fully introduced annotation to perform the tests we write.

(2), junit4.x version, quoting the way of annotations, unit testing;

(3), junit4.x version of our commonly used annotations:

A, @Before annotations: As with the setup () method function in junit3.x, execute before each test method;

B, @After annotations: As with the teardown () method function in junit3.x, execute after each test method;

C, @BeforeClass Annotations: Execute before all methods are executed;

D, @AfterClass annotations: Executed after all methods have been executed;

E, @Test (timeout = xxx) Note: Set the current test method to run within a certain time, or return an error;

F, @Test (expected = exception.class) Note: Sets whether the method being tested throws an exception. The exception type thrown is: Exception.class;

G, @Ignore annotations: Comment out a test method or a class, the annotated method or class, will not be executed.


Package test;


Import Java.util.Arrays;

Import java.util.Collection;


Import Org.junit.After;

Import Org.junit.AfterClass;

Import Org.junit.Assert;

Import Org.junit.Before;

Import Org.junit.BeforeClass;

Import Org.junit.Test;

Import Org.junit.runner.RunWith;

Import org.junit.runners.Parameterized;

Import Org.junit.runners.Parameterized.Parameters;


(1) Step one: The test class specifies a special runner org.junit.runners.Parameterized

@RunWith (Parameterized.class)

public class TestT1 {

T1 t1;

//(2) Step two: Declare several variables for the test class, respectively, to hold the expected value and the data used for the test.

int x, y, z,expected;

//(3) Step three: Declare a public constructor with parameters for the test class and assign values to several variables declared in the second link.

public TestT1 (int x, int y, int z,int expected) {

System.out.println ("Initialize");

this.x = x;

this.y = y;

this.z = z;

this.expected = expected;

}


//(4) Step four: Declare for the test class A org.junit.runners.Parameterized.Parameters decorated with annotations, with a return value of

///Java.util.Collection public static method, and in this method initializes all parameter pairs that need to be tested.

@Parameters

public static Collection Data () {


}

@BeforeClass

//execute before all methods are executed

public static void Globalinit () {

//system.out.println ("init all Method ...");

}


@AfterClass

//Execute after all methods have been executed

public static void Globaldestory () {

//system.out.println ("Destory all Method ...");

}

@Before

//execute before each test method

public void setUp () {

//system.out.println ("Start SetUp method");

t1=new T1 ();

}


@After

//executed after each test method

public void TearDown () {

//system.out.println ("End Method");

}

@Test

public void TestWork1 () {

Int act = t1.work (x, y, z);

assert.assertequals (expected, ACT);

}


@Test

public void TestWork2 () {

Int act = t1.work (x, y, z);

assert.assertequals (expected, ACT);

}


@Test

public void TestWork3 () {

Int act = t1.work (x, y, z);

assert.assertequals (expected, ACT);

}


@Test (timeout = 1000)

public void TestWork4 () {

Int act = t1.work (x, y, z);

assert.assertequals (expected, ACT);

}

}


A preliminary understanding of JUnit

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.