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. import static org.junit.Assert.*;
  6.  
  7. class SimpleTest {
  8.  
  9. @Test
  10. public void testAddition() {
  11. int result = 2 + 2;
  12. assertEquals(4, result);
  13. }
  14.  
  15. @Test
  16. public void testSubtraction() {
  17. int result = 5 - 3;
  18. assertEquals(2, result);
  19. }
  20.  
  21. @Test
  22. public void testMultiplication() {
  23. int result = 4 * 3;
  24. assertEquals(12, result);
  25. }
  26.  
  27. @Test
  28. public void testDivision() {
  29. double result = 10 / 2;
  30. assertEquals(5.0, result, 0.0001); // third parameter is the delta for floating-point comparison
  31. }
  32. }
  33.  
  34. class Main {
  35. public static void main(String[] args) {
  36. Result result = JUnitCore.runClasses(SimpleTest.class);
  37. for (Failure failure : result.getFailures()) {
  38. System.out.println(failure.toString());
  39. }
  40. if (result.wasSuccessful()) {
  41. System.out.println("All tests passed!");
  42. }
  43. }
  44. }
  45.  
Success #stdin #stdout 0.17s 59708KB
stdin
Standard input is empty
stdout
initializationError(SimpleTest): Test class should have exactly one public constructor
initializationError(SimpleTest): Class SimpleTest should be public
initializationError(SimpleTest): Class SimpleTest should be public
initializationError(SimpleTest): Class SimpleTest should be public
initializationError(SimpleTest): Class SimpleTest should be public