JUnit----Unit Tests

Source: Internet
Author: User

Why unit Testing?

1. Reuse the test to cope with future implementation changes.

2. It is no problem to specify my things clearly.

Failure, what is the difference between error?

Failure only test failed, error means program itself error

1. New->java PROJECT:JUNIT4

2. src Right-click->new->class T, PACKAGE:COM.BJSXT.JUNIT4

Package Com.bjsxt.junit4;public class T {public int add (int x, int y) {return x+y;} public int divide (int x, int y) {return x/y;}}

3. Com.bjsxt.junit4 Right-new->package-> com.bjsxt.junit4.test, right-click New->junit test Case (New JUnit 4 Test, Name:ttest, class under Test:t)->next Select the T method to test->finish-> Select the default MyEclipse jar package,

4. Build Path->remove ... Delete the native jar, add your own downloaded jar, build-> add external archieves.

5. Open Ttest.java and write the test code:

Package Com.bjsxt.unit4.test;import static Org.junit.assert.assertthat;import static Org.hamcrest.matchers.*;import Org.junit.test;import Com.bjsxt.u2.t;public class TTest {@Testpublic void Testadd () {int z = new T (). Add (5,3); Assertthat ( Z,is (8));
Assertthat (z, AllOf (GreaterThan (1), LessThan (Ten)));} }

Keep Bar Green to keep bugs clean.

If you do not delete the package first, the following error occurs:

1. Prompt error When using the is method above, need to introduce hamcrest jar package (core and license package): Right-click Project-Build path, introduce two packages

then found that appeared: java.lang.SecurityException:class "Org.hamcrest.Matchers" ' s signer information does not match signer Information of other classes in the same package

Because the hamcrest bag and the ClassLoader of the Hamcrest package in the JUNIT4 are not the same. Workaround:

Remove the MyEclipse from the package and introduce your own package junit.

What if an exception occurs when testing divide?

Add the expected exception type to the annotation. The back timeout is the judgment of time.

@Test (Expected=java.lang.arithmeticexception.class, timeout=100) public void Testdivide () {int z=new T (). Divide (5,0);}

  

Annotation:

1. @Test: Test method

2. @Ignore: Ignored test methods

3. @Before: Before each test method run usage: Open a file before executing the method

4. @After: Run usage after each test method: Close a file after executing the method

5. @BeforeClass: Run usage before all test methods: Configure work, establish database connection, etc. time-consuming work

6. @AfterClass: Run the usage after all test methods: such as shutting down the database

Example:

Package Com.bjsxt.junit4.test;import static Org.hamcrest.matchers.is;import static Org.junit.assert.*;import Org.junit.after;import Org.junit.afterclass;import Org.junit.before;import Org.junit.beforeclass;import Org.junit.test;import Com.bjsxt.junit4.t;public class TTest {@BeforeClass  //Pre-configuration work, such as database connection time-consuming resources, When carrying a complex environment, public static void Beforeclass () {System.out.println ("Beforeclass");} @AfterClass   //Close database connection public static void Afterclass () {System.out.println ("Afterclass");} @Before   //Open file using public void before () {System.out.println ("before");} @Testpublic void Testadd () {int z=new T (). Add (5,3); Assertthat (Z,is (8));//asserttrue ("Z is not bigger than 3", z>3);} @Ignore @test (Expected=java.lang.arithmeticexception.class, timeout=100) public void Testdivide () {int z=new T (). Divide (5,0);} @After   //close file using public void after () {System.out.println ("after");}}

Results:

Beforeclassbeforeafterbeforeafterafterclass

You can also create another class, User:

Package Com.bjsxt.junit4;public class User {public String getName () {return "Songhuiqiao";}}

Right-click in the test package, create a new unit test, Usertest

Package Com.bjsxt.junit4.test;import static Org.junit.assert.assertthat;import static org.hamcrest.Matchers.is; Import Org.junit.test;import Com.bjsxt.junit4.user;public class Usertest {@Testpublic void Testgetname () {Assertthat ( New User (). GetName (), is ("Songhuiqiao"));}}

  

If two classes run at the same time, in the test package right-click, run->configuration->run All:

JUnit----Unit Tests

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.