MyClass
or any name you prefer).@Test
annotation. For example:@Test
public void checkingEvenValues() {
...
}
assertTrue
→ verifies that the argument expression is true. If the argument expression is false
the test fails; otherwise execution is successful.assertEquals
→ takes two arguments (expected value and actual value). If the values are not equal
the test fails; otherwise execution is successful.assertFalse
→ verifies that the argument expression is false. If the argument expression is true
the test fails; otherwise execution is successful.JUnitExample.zip
) below.Example 1:
public class AuxMath {
public static int maximum(int x, int y) {
if (x > y)
return x;
return y;
}
public static int minimum(int x, int y) {
if (x < y)
return x;
return y;
}
}
import static org.junit.Assert.*;
import org.junit.Test;
public class JUnitTestExample {
@Test
public void oneMaximum() {
int expectedResults = 20;
assertEquals(expectedResults, AuxMath.maximum(10, 20));
}
@Test
public void twoMinimum() {
int expectedResults = 5;
assertTrue(expectedResults==AuxMath.minimum(30, 5));
}
}
Example 2: JUnitExample.zip
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
before the class name. For example:
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class JUnitTestExample {
Make sure you add the following imports to the file:
import org.junit.FixMethodOrder;
import org.junit.runners.MethodSorters;