JUnit使用手册
参考资料
- JUnit Tutorial for Beginners: Learn in 3 Days
- junit4 github
- Unit Testing with JUnit 5 - Tutorial
- JUnit 5 User Guide
JUnit4
package: org.Junit
Concepts
Test Case
A test case is a set of actions executed to verify a particular feature or functionality of your software application. A Test Case contains test steps, test data, precondition, postcondition developed for specific test scenario to verify any requirement.
For a Test Scenario: Check Login Functionality there many possible test cases are:
- Test Case 1: Check results on entering valid User Id & Password
- Test Case 2: Check results on entering Invalid User ID & Password
- Test Case 3: Check response when a User ID is Empty & Login Button is pressed, and many more
Test fixture
1 | public class OutputFileTest { |
A test fixture is a context where a Test Case runs. Typically, test fixtures include:
- Objects or resources that are available for any test case.
- Activities required that makes these objects/resources available, such as setup and teardown.
Test Suites
Test suite allows us to aggregate all test cases from multiple classes in an order in one place and run it together.
To run the suite test, you need to annotate a class using below-mentioned annotations:
@Runwith(Suite.class)
@SuiteClasses(test1.class,test2.class……) or @Suite.SuiteClasses ({test1.class, test2.class……})
例子:
Test Runner
a tool for execution of the test cases.
1 | public class Test { |
Test Scenario
A test scenario is defined as any functionality that can be tested. It is also called Test Condition or Test Possibility.
Error Collector
With JUnit error collector, you can still continue with the test execution even after an issue is found or test fails. Error collector collects all error objects and reports it only once after the test execution is over.
Annotations
@Before and @After
@Before annotation is used on a method containing code to run before each test case. i.e it runs before each test execution. @After annotation is used on a method containing java code to run after each test case. These methods will run even if any exceptions are thrown in the test case or in the case of assertion failures.
- All the methods annotated with @Before will run before each test case, but they may run in any order.
- You can inherit @Before and @After methods from a super class, Execution is as follows: It is a standard execution process in JUnit.
- Execute the @Before methods in the superclass
- Execute the @Before methods in this class
- Execute a @Test method in this class
- Execute the @After methods in this class
- Execute the @After methods in the superclass
@BeforeClass and @AfterClass
Run a method only once for the entire test class before(after) any of the tests are executed. They are used for “once-only setup“ and “once-only teardown“, such as start & stop servers and open & close communication.
Annotations | Description |
---|---|
@Test | This annotation is a replacement of org.junit.TestCase which indicates that public void method to which it is attached can be executed as a test Case. |
@Ignore | This annotation can be used if you want to ignore some statements during test execution for e.g. disabling some test cases during test execution. |
@Test(timeout=500) | This annotation can be used if you want to set some timeout during test execution for e.g. if you are working under some SLA (Service level agreement), and tests need to be completed within some specified time. |
@Test(expected=IllegalArgumentException.class) | This annotation can be used if you want to handle some exception during test execution. For, e.g., if you want to check whether a particular method is throwing specified exception or not. |
@Ignore
can be used in two scenarios as given below:
- If you want to ignore a test method, use @Ignore along with @Test annotation.
- If you want to ignore all the tests of class, use @Ignore annotation at the class level.
For exception testing, you can use
- Optional parameter (expected) of @test annotation and
- To trace the information ,”fail()” can be used
@Rule
is used to create an object of error collector. Once the object for error collector is created, you can easily add all the errors into the object using method addError (Throwable error), these errors will be logged in JUnit test result.
example:
1 | package guru99.junit; |
@Parameters
Parameterized test is to execute the same test over and over again using different values. It helps developer to save time in executing same test which differs only in their inputs and expected results. Using Parameterized test, one can set up a test method that retrieves data from some data source.
example:
- Create a parameterized test class
@RunWith annotation is used to specify its runner class name. If we don’t specify any type as a parameter, the runtime will choose BlockJunit4ClassRunner by default.
Create a constructor that stores the test data. It stores 3 variables
Create a static method that generates and returns test data.
- Create a test runner class to run parameterized test
Assertions
- org.junit.Assert
Method | Description |
---|---|
void assertEquals(boolean expected, boolean actual) | It checks whether two values are equals similar to equals method of Object class |
void assertFalse(boolean condition) | functionality is to check that a condition is false. |
void assertNotNull(Object object) | “assertNotNull” functionality is to check that an object is not null. |
void assertNull(Object object) | “assertNull” functionality is to check that an object is null. |
void assertTrue(boolean condition) | “assertTrue” functionality is to check that a condition is true. |
void fail() | If you want to throw any assertion error, you have fail() that always results in a fail verdict. |
void assertSame([String message] | “assertSame” functionality is to check that the two objects refer to the same object. |
void assertNotSame([String message] | “assertNotSame” functionality is to check that the two objects do not refer to the same object. |
- assertArrayEquals(expected, actual): if arrays have the same length
- assertEquals( aDoubleValue, anotherDoubleValue, 0.001 ): Math.abs( expected – actual ) <= delta
Class and Methods
org.junit.TestCase
Method | Description |
---|---|
int countTestCases() | This method is used to count how many number of test cases executed by run(TestResult tr) method. |
TestResult createResult() | This method is used to create a TestResult object. |
String getName() | This method returns a string which is nothing but a TestCase. |
TestResult run() | This method is used to execute a test which returns a TestResult object |
void run(TestResult result) | This method is used to execute a test having a TestResult object which doesn’t returns anything. |
void setName(String name) | This method is used to set a name of a TestCase. |
void setUp() | This method is used to write resource association code. e.g. Create a database connection. |
void tearDown() | This method is used to write resource release code. e.g. Release database connection after performing transaction operation. |
org.junit.TestResult
Method | Description |
---|---|
void addError(Test test, Throwable t) | This method is used if you require add an error to the test. |
void addFailure(Test test, AssertionFailedError t) | This method is used if you require add a failure to the list of failures. |
void endTest(Test test) | This method is used to notify that a test is performed(completed) |
int errorCount() | This method is used to get the error detected during test execution. |
Enumeration<TestFailure> errors() | This method simply returns a collection (Enumeration here) of errors. |
int failureCount() | This method is used to get the count of errors detected during test execution. |
void run(TestCase test) | This method is used to execute a test case. |
int runCount() | This method simply counts the executed test. |
void startTest(Test test) | This method is used to notify that a test is started. |
void stop() | This method is used to test run to be stopped. |
org.junit.TestSuite
Method | Description |
---|---|
void addTest(Test test) | This method is used if you want to add a test to the suite. |
void addTestSuite(Class<? extends TestCase> testClass) | This method is used if you want to specify the class while adding a test to the suite. |
int countTestCases() | This method is used if you want to count the number of test cases. |
String getName() | This method is used to get the name of the test suite. |
void run(TestResult result) | This method is used to execute a test and collect test result in TestResult object. |
void setName(String name) | This method is used to set the name of TestSuite. |
Test testAt(int index) | This method is used if you want to return the test at given index. |
int testCount() | This method is used if you want to return a number of tests in the Suite. |
static Test warning(String message) | This method returns a test which will fail and log a warning message. |
JUnit 5
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage
Annotations
Unless otherwise stated, all core annotations are located in the org.junit.jupiter.api
package in the junit-jupiter-api
module.
Annotation | Description |
---|---|
@Test |
Denotes that a method is a test method. Unlike JUnit 4’s @Test annotation, this annotation does not declare any attributes, since test extensions in JUnit Jupiter operate based on their own dedicated annotations. Such methods are inherited unless they are overridden. |
@ParameterizedTest |
Denotes that a method is a parameterized test. Such methods are inherited unless they are overridden. |
@RepeatedTest |
Denotes that a method is a test template for a repeated test. Such methods are inherited unless they are overridden. |
@TestFactory |
Denotes that a method is a test factory for dynamic tests. Such methods are inherited unless they are overridden. |
@TestTemplate |
Denotes that a method is a template for test cases designed to be invoked multiple times depending on the number of invocation contexts returned by the registered providers. Such methods are inherited unless they are overridden. |
@TestMethodOrder |
Used to configure the test method execution order for the annotated test class; similar to JUnit 4’s @FixMethodOrder . Such annotations are inherited. |
@TestInstance |
Used to configure the test instance lifecycle for the annotated test class. Such annotations are inherited. |
@DisplayName |
Declares a custom display name for the test class or test method. Such annotations are not inherited. |
@DisplayNameGeneration |
Declares a custom display name generator for the test class. Such annotations are inherited. |
@BeforeEach |
Denotes that the annotated method should be executed before each @Test , @RepeatedTest , @ParameterizedTest , or @TestFactory method in the current class; analogous to JUnit 4’s @Before . Such methods are inherited unless they are overridden. |
@AfterEach |
Denotes that the annotated method should be executed after each @Test , @RepeatedTest , @ParameterizedTest , or @TestFactory method in the current class; analogous to JUnit 4’s @After . Such methods are inherited unless they are overridden. |
@BeforeAll |
Denotes that the annotated method should be executed before all @Test , @RepeatedTest , @ParameterizedTest , and @TestFactory methods in the current class; analogous to JUnit 4’s @BeforeClass . Such methods are inherited (unless they are hidden or overridden) and must be static (unless the “per-class” test instance lifecycle is used). |
@AfterAll |
Denotes that the annotated method should be executed after all @Test , @RepeatedTest , @ParameterizedTest , and @TestFactory methods in the current class; analogous to JUnit 4’s @AfterClass . Such methods are inherited (unless they are hidden or overridden) and must be static (unless the “per-class” test instance lifecycle is used). |
@Nested |
Denotes that the annotated class is a non-static nested test class. @BeforeAll and @AfterAll methods cannot be used directly in a @Nested test class unless the “per-class” test instance lifecycle is used. Such annotations are not inherited. |
@Tag |
Used to declare tags for filtering tests, either at the class or method level; analogous to test groups in TestNG or Categories in JUnit 4. Such annotations are inherited at the class level but not at the method level. |
@Disabled |
Used to disable a test class or test method; analogous to JUnit 4’s @Ignore . Such annotations are not inherited. |
@Timeout |
Used to fail a test, test factory, test template, or lifecycle method if its execution exceeds a given duration. Such annotations are inherited. |
@ExtendWith |
Used to register extensions declaratively. Such annotations are inherited. |
@RegisterExtension |
Used to register extensions programmatically via fields. Such fields are inherited unless they are shadowed. |
@TempDir |
Used to supply a temporary directory via field injection or parameter injection in a lifecycle method or test method; located in the org.junit.jupiter.api.io package. |
Assertions
JUnit Jupiter comes with many of the assertion methods that JUnit 4 has and adds a few that lend themselves well to being used with Java 8 lambdas. All JUnit Jupiter assertions are static
methods in the org.junit.jupiter.api.Assertions
class.
1 | import static java.time.Duration.ofMillis; |
Assumptions
1 | import static org.junit.jupiter.api.Assertions.assertEquals; |
Migrating from JUnit4
- Annotations reside in the
org.junit.jupiter.api
package. - Assertions reside in
org.junit.jupiter.api.Assertions
. - Assumptions reside in
org.junit.jupiter.api.Assumptions
.- Note that JUnit Jupiter 5.4 and later versions support methods from JUnit 4’s
org.junit.Assume
class for assumptions. Specifically, JUnit Jupiter supports JUnit 4’sAssumptionViolatedException
to signal that a test should be aborted instead of marked as a failure.
- Note that JUnit Jupiter 5.4 and later versions support methods from JUnit 4’s
@Before
and@After
no longer exist; use@BeforeEach
and@AfterEach
instead.@BeforeClass
and@AfterClass
no longer exist; use@BeforeAll
and@AfterAll
instead.@Ignore
no longer exists: use@Disabled
or one of the other built-in execution conditions instead.@Category
no longer exists; use@Tag
instead.@RunWith
no longer exists; superseded by@ExtendWith
.@Rule
and@ClassRule
no longer exist; superseded by@ExtendWith
and@RegisterExtension