JUPITER
Juipter - For newer test cases
Vintage package -- Older
EXT - JAp
Core platform, Jupiter dependecies to be added
junit-jupiter-api
junit-jupiter-engine
junit-vintage-engine //optional for prev versions of junit ex: junit 4
@Test annotation: It marks the method that we want to test
@Test
void testDivide() {
assertThrows(ArithmeticException.class, () -> mUtils.divide(1, 0), "divide by Zero exception");
}
assertEquals(expected, actual) // provided by junit framework matches the expected &actual values.
assertArrayEquals(expectedArray, actualArray) //verify each item in array are equal in the right position
assertIterableEquals(expectedArray, actualArray)//verify each item in iterable are equal in the corresponding position
assertFalse()
assertTrue()
Maven Surefire plugin
When a method is throwing exceptions we use : assertThrows
Test Life Cycle:: Project instance created, managed & destroyed
Junit creates new class instance for every method / test run
Junit provides a mechanism which can be run before every test
@BeforeEach to get rid of multiple class instantiation, please run this before anything else
@BeforeEach
void init() {
mUtils = new MathUtils();
}
@AfterEach
void cleanup() {
System.out.println("Cleaning up.....");
}
@BeforeAll, @AfterAll needs to be defined static class level as there could be some methods which would be instantiating objects. Without instantiating if you want to access class these methods have to be static.
@TestInstance
@DisplayName -> method name how it is displayed in test results
@Disabled ->If we want to some test method from being executed, to prevent it from blocking the program. Skip the test. Make it disabled
Conditional Execution:
@EnabledOnOs(OS>LINUX) -> Enable test on only particular OS.
@EnabledOnJre(JRE.JAVA_11)
assertAll -> Way to run bunch of assertions altogether
@Test
@DisplayName("Multiple Addition ")
void testAdd() {
//Using lambdas multiple assert statements
assertAll(
() -> assertEquals(4, mUtils.add(2, 2)),
() -> assertEquals(2, mUtils.add(2, 0)),
() -> assertEquals(1, mUtils.add(2, -1))
);
}
@Nested -> Create a bunch of tests and group them together
@Nested
class AddTest {
@Test
@DisplayName("Multiple Addition ")
void testAdd() {
//assertEquals(4, mUtils.add(2, 2),"Addition");
//Using lambdas multiple assert statements
assertAll(
() -> assertEquals(4, mUtils.add(2, 2)),
() -> assertEquals(2, mUtils.add(2, 0)),
() -> assertEquals(1, mUtils.add(2, -1))
);
}
@Test
@DisplayName("Multiple Addition ")
void testAddPositive() {
assertAll(
() -> assertEquals(4, mUtils.add(2, 2))
);
}
}
Lazy Assert messages:
Juipter - For newer test cases
Vintage package -- Older
EXT - JAp
Core platform, Jupiter dependecies to be added
junit-jupiter-api
junit-jupiter-engine
junit-vintage-engine //optional for prev versions of junit ex: junit 4
@Test annotation: It marks the method that we want to test
@Test
void testDivide() {
assertThrows(ArithmeticException.class, () -> mUtils.divide(1, 0), "divide by Zero exception");
}
assertEquals(expected, actual) // provided by junit framework matches the expected &actual values.
assertArrayEquals(expectedArray, actualArray) //verify each item in array are equal in the right position
assertIterableEquals(expectedArray, actualArray)//verify each item in iterable are equal in the corresponding position
assertFalse()
assertTrue()
Maven Surefire plugin
When a method is throwing exceptions we use : assertThrows
Test Life Cycle:: Project instance created, managed & destroyed
Junit creates new class instance for every method / test run
Junit provides a mechanism which can be run before every test
@BeforeEach to get rid of multiple class instantiation, please run this before anything else
@BeforeEach
void init() {
mUtils = new MathUtils();
}
@AfterEach
void cleanup() {
System.out.println("Cleaning up.....");
}
@BeforeAll, @AfterAll needs to be defined static class level as there could be some methods which would be instantiating objects. Without instantiating if you want to access class these methods have to be static.
@TestInstance
@DisplayName -> method name how it is displayed in test results
@Disabled ->If we want to some test method from being executed, to prevent it from blocking the program. Skip the test. Make it disabled
Conditional Execution:
@EnabledOnOs(OS>LINUX) -> Enable test on only particular OS.
@EnabledOnJre(JRE.JAVA_11)
assertAll -> Way to run bunch of assertions altogether
@Test
@DisplayName("Multiple Addition ")
void testAdd() {
//Using lambdas multiple assert statements
assertAll(
() -> assertEquals(4, mUtils.add(2, 2)),
() -> assertEquals(2, mUtils.add(2, 0)),
() -> assertEquals(1, mUtils.add(2, -1))
);
}
@Nested -> Create a bunch of tests and group them together
@Nested
class AddTest {
@Test
@DisplayName("Multiple Addition ")
void testAdd() {
//assertEquals(4, mUtils.add(2, 2),"Addition");
//Using lambdas multiple assert statements
assertAll(
() -> assertEquals(4, mUtils.add(2, 2)),
() -> assertEquals(2, mUtils.add(2, 0)),
() -> assertEquals(1, mUtils.add(2, -1))
);
}
@Test
@DisplayName("Multiple Addition ")
void testAddPositive() {
assertAll(
() -> assertEquals(4, mUtils.add(2, 2))
);
}
}
Lazy Assert messages: