What is the method used by JUnit for testing?
There are several main methods used in JUnit.
- @Test annotation: used to identify test methods, JUnit will execute all methods with @Test annotation.
- @Before annotation: used to identify a method that needs to be executed before each test method, typically used to initialize some shared resources.
- @After annotation: used to identify a method that needs to be executed after each test method, typically used to release some shared resources.
- @BeforeClass annotation: used to identify the method that needs to be executed before all test methods, typically used for initializing static resources.
- @AfterClass annotation: used to identify a method that needs to be executed after all test methods, typically used to release some static resources.
- The assertEquals() method is used to compare two values and throws an AssertionError if they are not equal.
- assertTrue() method: used to check if a condition is true, and throws an AssertionError if it is false.
- The assertFalse() method is used to check if a condition is false, and if it is true, it will throw an AssertionError exception.
- The assertNull() method is used to check if an object is null, and if it is not null, it will throw an AssertionError exception.
- The assertNotNull() method is used to determine if an object is not null; if it is null, an AssertionError exception is thrown.
In addition to the methods mentioned above, JUnit also offers various other assertion methods and annotations to support a wider range of testing scenarios and requirements.