Hi Sargun, first we need to create a test class with a test method annotated with @Test as given below:
FirstClassTest.java
package testingDemo.JUnit;
import static org.JUnit.Assert.*;
import org.JUnit.Test;
public class FirstClassTest {
@Test
public void FirstMethod(){
String str= "JUnit is working fine";
assertEquals("JUnit is working fine",str);
}
}
To execute our above test method ,we need to create a test runner. In the test runner we have to add test class as a parameter in JUnitCore's runclasses() method . It will return the test result, based on whether the test is passed or failed:
package testingDemo.JUnit;
import org.JUnit.runner.JUnitCore;
import org.JUnit.runner.Result;
import org.JUnit.runner.notification.Failure;
public class TestRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(MyFirstClassTest.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println("Result=="+result.wasSuccessful());
}
}