Run Sample Program for junit-4.8.1.jar

Q

How to run sample program with junit-4.8.1.jar? I have Calculator.java and the JUnit test program, CalculatorTest.java ready.

✍: FYIcenter.com

A

junit-4.8.1.jar is the version 4.8.1 of JUnit JAR library file.

To run the JUnit test program, CalculatorTest.java, you need launch the org.junit.runner.JUnitCore class and specify the test program class as the parameter:

\fyicenter>java -version
java version "1.8.0_45"

\fyicenter>java -cp .;\local\lib\junit-4.8.1.jar
   org.junit.runner.JUnitCore CalculatorTest

JUnit version 4.8.1
..E.E
Time: 0.007

There were 2 failures:
1) testFraction(CalculatorTest)
junit.framework.AssertionFailedError: expected:<5> but was:<4>
  at junit.framework.Assert.fail(Assert.java:47)
  at junit.framework.Assert.failNotEquals(Assert.java:283)
  at junit.framework.Assert.assertEquals(Assert.java:64)
  at junit.framework.Assert.assertEquals(Assert.java:195)
  at junit.framework.Assert.assertEquals(Assert.java:201)
  at CalculatorTest.testFraction(CalculatorTest.java:12)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke
     (NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke
     (DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:497)
  at junit.framework.TestCase.runTest(TestCase.java:168)
  at junit.framework.TestCase.runBare(TestCase.java:134)
  at junit.framework.TestResult.protect(TestResult.java:110)
  at junit.framework.TestResult.runProtected(TestResult.java:128)
  at junit.framework.TestResult.run(TestResult.java:113)
  at junit.framework.TestCase.run(TestCase.java:124)
  at junit.framework.TestSuite.runTest(TestSuite.java:232)
  at junit.framework.TestSuite.run(TestSuite.java:227)
  at org.junit.internal.runners.JUnit38ClassRunner.run
     (JUnit38ClassRunner.java:83)
  at org.junit.runners.Suite.runChild(Suite.java:128)
  at org.junit.runners.Suite.runChild(Suite.java:24)
  at org.junit.runners.ParentRunner.run(ParentRunner.java:193)
  at org.junit.runners.ParentRunner.schedule(ParentRunner.java:52)
  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
  at org.junit.runners.ParentRunner.access__answer__0(ParentRunner.java:42)
  at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:184)
  at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
  at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
  at org.junit.runner.JUnitCore.run(JUnitCore.java:136)
  at org.junit.runner.JUnitCore.run(JUnitCore.java:117)
  at org.junit.runner.JUnitCore.runMain(JUnitCore.java:98)
  at org.junit.runner.JUnitCore.runMainAndExit(JUnitCore.java:53)
  at org.junit.runner.JUnitCore.main(JUnitCore.java:45)

2) testSubtraction(CalculatorTest)
java.lang.NumberFormatException: For input string: "10-1"
  at sun.misc.FloatingDecimal.readJavaFormatString
     (FloatingDecimal.java:2043)
  at sun.misc.FloatingDecimal.parseFloat(FloatingDecimal.java:122)
  at java.lang.Float.parseFloat(Float.java:451)
  at java.lang.Float.valueOf(Float.java:416)
  at Calculator.evaluate(Calculator.java:6)
  at CalculatorTest.testSubtraction(CalculatorTest.java:16)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke
     (NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke
     (DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:497)
  at junit.framework.TestCase.runTest(TestCase.java:168)
  at junit.framework.TestCase.runBare(TestCase.java:134)
  at junit.framework.TestResult.protect(TestResult.java:110)
  at junit.framework.TestResult.runProtected(TestResult.java:128)
  at junit.framework.TestResult.run(TestResult.java:113)
  at junit.framework.TestCase.run(TestCase.java:124)
  at junit.framework.TestSuite.runTest(TestSuite.java:232)
  at junit.framework.TestSuite.run(TestSuite.java:227)
  at org.junit.internal.runners.JUnit38ClassRunner.run
     (JUnit38ClassRunner.java:83)
  at org.junit.runners.Suite.runChild(Suite.java:128)
  at org.junit.runners.Suite.runChild(Suite.java:24)
  at org.junit.runners.ParentRunner.run(ParentRunner.java:193)
  at org.junit.runners.ParentRunner.schedule(ParentRunner.java:52)
  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
  at org.junit.runners.ParentRunner.access__answer__0(ParentRunner.java:42)
  at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:184)
  at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
  at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
  at org.junit.runner.JUnitCore.run(JUnitCore.java:136)
  at org.junit.runner.JUnitCore.run(JUnitCore.java:117)
  at org.junit.runner.JUnitCore.runMain(JUnitCore.java:98)
  at org.junit.runner.JUnitCore.runMainAndExit(JUnitCore.java:53)
  at org.junit.runner.JUnitCore.main(JUnitCore.java:45)

FAILURES!!!
Tests run: 3,  Failures: 2

The output shows that:

  • One test passed.
  • One test failed with Java error, because the target program threw a NumberFormatException exception.
  • One test failed with JUnit error: AssertionFailedError, because the actual result did not match the expected result.

 

Download and Install junit-4.7.jar

Run org.junit.runner.JUnitCore in junit-4.8.1.jar

Download and Install JUnit (Java Unit) Testing

⇑⇑ FAQ for JUnit (Java Unit) Testing

2016-03-02, 2208🔥, 0💬