fork download
  1. import org.junit.Test;
  2. import org.junit.runner.JUnitCore;
  3. import org.junit.runner.Result;
  4. import org.junit.runner.notification.Failure;
  5.  
  6. import static org.junit.Assert.*;
  7.  
  8. public class Main {
  9.  
  10. // Example class with a method that throws a specific exception
  11. public class ExampleClass {
  12. public int divide(int a, int b) {
  13. if (b == 0) {
  14. throw new ArithmeticException("Cannot divide by zero");
  15. }
  16. return a / b;
  17. }
  18. }
  19.  
  20. // Main function to run JUnit tests
  21. public static void main(String[] args) {
  22. Result result = JUnitCore.runClasses(Main.class);
  23.  
  24. // Check if there are any failures
  25. if (result.getFailureCount() > 0) {
  26. System.out.println("Test failed:");
  27.  
  28. // Print details of failures
  29. for (Failure failure : result.getFailures()) {
  30. System.out.println(failure.toString());
  31. }
  32. } else {
  33. System.out.println("All tests passed successfully.");
  34. }
  35. }
  36. }
Success #stdin #stdout 0.2s 59860KB
stdin
Standard input is empty
stdout
Test failed:
initializationError(Main): No runnable methods