I've got following code in IAdder.java:
public interface IAdder {
int add(int a, int b);
}
Then the following implementations (one in SimpleAdder.java and another in AbsAdder.java):
public class SimpleAdder implements IAdder {
@Override
public int add(int a, int b) {
return a+b;
}
}
public class AbsAdder implements IAdder {
@Override
public int add(int a, int b) {
return Math.abs(a) + Math.abs(b);
}
}
Now I want to test with Junit5 so I start writing the following in SimpleAddertest.java:
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class SimpleAdderTest {
IAdder ia = new SimpleAdder();
@Test
void add() {
assertEquals(10, ia.add(7, 3));
}
}
To test AbsAdder I could add the following test class AbsAdderTest:
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class AbsAdderTest {
IAdder ia = new AbsAdder();
@Test
void add() {
assertEquals(10, ia.add(7, 3));
}
}
What's the best way to avoid this code repetition in Junit5? I ve seen other posts on SO but no one answered this simple question in a simple way.
add(-5,-7)
in both, though with different results.