public int add(int a, int b) { return a + b; }
@Test public void testAdd() { int result = add(2, 3); assertThat(result, equalTo(5)); }
public class Student { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } }
@Test public void testStudent() { Student s1 = new Student("Alice", 20); Student s2 = new Student("Alice", 20); assertThat(s1, equalTo(s2)); }In this example, we're using the equalTo matcher to compare the values of our two Student objects. The org.junit.Assert package is included in the JUnit library, which is a popular testing framework for Java.