Musisz skorzystać z JUnit, które służy do tworzenia tego typu testów. Przykładową metodę
public class Hello {
public String printMessage(){
return "Hello world";
}
}
Można przetestować w następujący sposób
public class HelloTest {
String message = "Hello world";
String wrongMessage = "nope";
Hello hello = new Hello();
@Test
public void testPrintMessage() {
assertEquals(message, hello.printMessage());
}
@Test
public void testInvalidPrintMessage() {
assertNotEquals(wrongMessage, hello.printMessage());
}
}
Tutaj przykładowy kurs