Testing - Java
- Unit Testing
- Done by developer during development phase
- Tests single indivisible entity to increase “Programing Speed” and “Quality of Code”
- Junit => Open-source Java testing framework
- Features
- Assertions => Assumed to be true
- Test fixtures => Fixed state for a set if objects
- Test suites
- Modules
- Junit Platform
- Junit Jupiter
- Junit Vintage
- Supports parameterized & repeated test cases
- Architecture – Pic in Laptop
- Annotations
@RunWith(UnitPlatform.class) => Used to use the platform to run junit5 test code
@Test => Defines the test method
@Displayname => Change the name of the test result in the console
@BeforeAll => Defines annotated method which is executed before all @Test, @RepeatedTest, @TestFactory or @Parameterised Test
@AfterAll => Defines annotated method which is executed after all @Test, @Repeated Test, @TestFactory or @Parameterised Test
@BeforeEach => Defines annotated method which is executed before each @Test, @Repeated Test, @TestFactory or @Parameterised Test
@AfterEach => Defines annotated method which is executed after each @Test, @Repeated Test, @TestFactory or @Parameterised Test
@RepeatedTest(int numberOfRepetitions) => Defines a test template for repeated tests
- Placeholders =>
displayName, currentRepetition, totalRepetition
@Parameterised Test => Defines a test method that is parameterised test case
@Disabled => Used to disable a test class or test method from the execution
@Tag => Defines tags to filter tests either at class or method level
@Nested => Annotated class is nested, non-static test class
@TestFactory => Designates the method to be a test factory for dynamic tests
- Source of Arguments
@valueSource
@EnumSource
@MethodSource
@CsvSource
@CsvFileSource => CSV file for parameterized test
- Assertions
Assertions.assertEquals(“Message1”, “Message1”, “Both value not equal”);
Assertions.assertNotEquals(“Message1”, “Message2”, “Both value equal”);
Assertions.assertArrayEquals()
Assertions.assertNotNull()
Assertions.assertNull()
Assertions.assertNotSame()
Assertions.assertSame()
Assertions.assertTrue()
Assertions.assertFalse()
Assertions.fail()
Assertions.assertThrows() Assertions.assertAll()
Assertions.assertTimeout()
- Assumptions
assumeTrue() => Validates the assumptions to be true. If true, proceeds with test, else abort
assumeFalse() => Validates the assumptions to be false. If false, proceeds with the test, else abort
assumingThat() => If the provided assumption is valid, execute the test method
- Integration Testing
- System Testing