assertEquals()
The assertEquals() method compares two objects for equality, using their equals() method.
Here is a simple example:
import org.junit.Test;
import static org.junit.Assert.*;
public class MyUnitTest {
@Test
public void testConcatenate() {
MyUnit myUnit = new MyUnit();
String result = myUnit.concatenate("one", "two");
assertEquals("onetwo", result);
}
}
First the myUnit.concatenate() method is called, and the result is stored in the variable result.
Second, the result value is compared to the expected value "onetwo", using the assertEquals() method.
If the two objects are equal according to their implementation of their equals() method, the assertEquals() method will return normally.
Otherwise the assertEquals() method will throw an exception, and the test will stop there.
This example compared to String objects, but the assertEquals() method can compare any two objects to each other. The assertEquals() method also come in versions which compare primitive types like int and float to each other.