Run Sample Program with junit-4.12.jar

Q

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

✍: FYIcenter.com

A

junit-4.12.jar is the version 4.12 of JUnit JAR library file. It requires hamcrest-core-1.3.jar.

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.12.jar;
  \local\lib\hamcrest-core-1.3.jar org.junit.runner.JUnitCore
  CalculatorTest
JUnit version 4.12
.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:57)
  at junit.framework.Assert.failNotEquals(Assert.java:329)
  at junit.framework.Assert.assertEquals(Assert.java:78)
  at junit.framework.Assert.assertEquals(Assert.java:234)
  at junit.framework.Assert.assertEquals(Assert.java:241)
  at junit.framework.TestCase.assertEquals(TestCase.java:409)
  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:176)
  at junit.framework.TestCase.runBare(TestCase.java:141)
  at junit.framework.TestResult.protect(TestResult.java:122)
  at junit.framework.TestResult.runProtected(TestResult.java:142)
  at junit.framework.TestResult.run(TestResult.java:125)
  at junit.framework.TestCase.run(TestCase.java:129)
  at junit.framework.TestSuite.runTest(TestSuite.java:252)
  at junit.framework.TestSuite.run(TestSuite.java:247)
  at org.junit.internal.runners.JUnit38ClassRunner.run
     (JUnit38ClassRunner.java:86)
  at org.junit.runners.Suite.runChild(Suite.java:128)
  at org.junit.runners.Suite.runChild(Suite.java:27)
  at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
  at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
  at org.junit.runners.ParentRunner.access__answer__0(ParentRunner.java:58)
  at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
  at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
  at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
  at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
  at org.junit.runner.JUnitCore.runMain(JUnitCore.java:77)
  at org.junit.runner.JUnitCore.main(JUnitCore.java:36)
        
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:176)
  at junit.framework.TestCase.runBare(TestCase.java:141)
  at junit.framework.TestResult.protect(TestResult.java:122)
  at junit.framework.TestResult.runProtected(TestResult.java:142)
  at junit.framework.TestResult.run(TestResult.java:125)
  at junit.framework.TestCase.run(TestCase.java:129)
  at junit.framework.TestSuite.runTest(TestSuite.java:252)
  at junit.framework.TestSuite.run(TestSuite.java:247)
  at org.junit.internal.runners.JUnit38ClassRunner.run
     (JUnit38ClassRunner.java:86)
  at org.junit.runners.Suite.runChild(Suite.java:128)
  at org.junit.runners.Suite.runChild(Suite.java:27)
  at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
  at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
  at org.junit.runners.ParentRunner.access__answer__0(ParentRunner.java:58)
  at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
  at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
  at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
  at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
  at org.junit.runner.JUnitCore.runMain(JUnitCore.java:77)
  at org.junit.runner.JUnitCore.main(JUnitCore.java:36)

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.11.jar

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

Download and Install JUnit (Java Unit) Testing

⇑⇑ FAQ for JUnit (Java Unit) Testing

2016-02-26, 2399🔥, 0💬