Assertions Flashcards
Packages to import
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Example
@Test
void exampleTest() {
int result = someMethod();
Assertions.assertEquals(42, result, “The result should be 42”);
}
Common Assertions
assertEquals(expected, actual): Checks if two values are equal.
assertTrue(condition): Checks if a condition is true.
assertFalse(condition): Checks if a condition is false.
assertNotNull(object): Checks if an object is not null.
Parameterized Tests
This Parameterized Test is used to test a Test case with different parameters for this we use @ParameterizedTest annotations.
@ParameterizedTest
@ValueSource(ints = {1, 2, 3, 4, 5})
void testSquare(int value) {
int result = square(value);
assertEquals(value * value, result, “Square calculation is incorrect”);
}