Unit Test Summary

Source: Internet
Author: User

Let's start with today's summary:

To run a unit test with JUnit, you must first have the JUnit package and other packages required by the project. In eclipse, the test package is not included by default, so you need to introduce it. Second, if it is a Maven project, the local packages may not be complete. You need to download all the dependent packages. Run MVN Eclipse: Eclipse.


1. Introduce the JUnit package: Right-click the project -- properties -- Java build path -- libraries -- add library..., and select JUnit -- next -- select version -- finish.

2. Generate a JUnit test case, select the class to be tested, right-click -- New -- JUnit test case -- confirm the test class information -- select method -- finish.

3. Run the test case, open the file, right-click the file, and choose run as (debug as) -- JUnit test.

Run a single test case, select the test case to run, and right-click Run as (debug as) -- JUnit test.

4. Test Case declaration:

@ Test is declared as a test case

@ Ignore this test case and no execution is required.

@ Before is executed before any test case is executed. It is mainly used for initialization. The results of different test cases can be compared. How many test cases are executed.

@ After is executed after any test case is executed. It is mainly used for finishing work. It's a bit like finally. How many test cases are executed.

@ Beforeclass the method that is executed before all test cases for some troublesome preparation. However, unlike before, this method is executed only once, and only one method in a test class can be labeled as brforeclass.

@ Afterclass the method to be executed after all test cases. This method is executed only once, and only one method in a test class can be labeled as @ afterclass.

@ Test (timeout = 1000) sets the execution time for the test method. If the execution time exceeds the time limit, it is forcibly terminated by the system. The Unit is milliseconds.

@ Test (expected = ioexception. Class) indicates that the method should throw an IO exception.

@ Runwith (testclassrunner. Class) is used to specify the runtime. The default value is testclassrunner. Class.

@ Runwith (parameterized. Class) parameterized test, used to run a column of parameters.

@ Runwith (suite. Class) packaging test class

Use Case:

import  org.junit.runner.RunWith; import  org.junit.runners.Suite;  @RunWith(Suite. class ) @Suite.SuiteClasses( {         CalculatorTest. class ,         SquareTest. class          } ) public   class  AllCalculatorTests  { } 


Links:

Great writing: Click to open the link


Self-criticism:

This is the first time I ran a unit test. As a code farmer who has been working for more than half a year, it was really frustrating.


Unit Test Summary

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.