src/test/java
directoryFraction.java
public class Fraction {
@Getter private int numerator;
@Getter private int denominator;
public Fraction(int numerator, int denominator) {
if (denominator == 0)
throw new IllegalArgumentException("Denominator cannot be zero.");
this.numerator = numerator;
this.denominator = denominator;
}
public Fraction invert() {
if (numerator == 0)
throw new ArithmeticException("Cannot invert a fraction with a zero numerator.");
return new Fraction(denominator, numerator);
}
}
FractionTest.java
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class FractionTest {
@Test
void testInvertValid() {
// GIVEN
Fraction fraction = new Fraction(2, 3);
// WHEN
Fraction inverted = fraction.invert();
// THEN
assertEquals(3, inverted.getNumerator());
assertEquals(2, inverted.getDenominator());
}
@Test
void testInvertZeroNumerator() {
Fraction fraction = new Fraction(0, 5);
assertThrows(ArithmeticException.class, fraction::invert);
}
}
@Test
: Marks a method as a test case@BeforeEach
: Executes before each test method@AfterEach
: Executes after each test method@BeforeAll
: Executes once before all test methods@AfterAll
: Executes once after all test methods