Coder Social home page Coder Social logo

junit3-junit4-comparison's Introduction

junit3-junit4-comparison

1 Junit 3

Junit3

1.1 How to write test case

1. extends TestCase
2. test prefix method name

1.2 How to run

Compile & Run

  • Compile: mvn compile or "javac -cp ../lib/junit.jar com/depeng/Money.java"
  • Run:
    1. cd target
    2. text-based UI
      • java -cp /Users/depeng/github/junit3-junit4-comparison/junit3-showcase/target/test-classes:/Users/depeng/github/junit3-junit4-comparison/junit3-showcase/target/classes:/Users/depeng/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar junit.textui.TestRunner com.depeng.MoneyTest
    3. GUI
      • java -cp /Users/depeng/github/junit3-junit4-comparison/junit3-showcase/target/test-classes:/Users/depeng/github/junit3-junit4-comparison/junit3-showcase/target/classes:/Users/depeng/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar junit.swingui.TestRunner com.depeng.MoneyTest

maven run: mvn test

 depeng@congs-mbp ~/github/junit3-junit4-comparison/junit3-showcase master  mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building junit-showcase 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ junit3-showcase ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/depeng/github/junit3-junit4-comparison/junit3-showcase/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ junit3-showcase ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ junit3-showcase ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/depeng/github/junit3-junit4-comparison/junit3-showcase/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ junit3-showcase ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ junit3-showcase ---
[INFO] Surefire report directory: /Users/depeng/github/junit3-junit4-comparison/junit3-showcase/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.depeng.MathTest
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.017 sec
Running com.depeng.MoneyTest
Tests run: 2, Failures: 1, Errors: 1, Skipped: 0, Time elapsed: 0.004 sec <<< FAILURE!
testCurrency(com.depeng.MoneyTest)  Time elapsed: 0.001 sec  <<< FAILURE!
junit.framework.AssertionFailedError: expected:<1> but was:<2>
        at junit.framework.Assert.fail(Assert.java:47)
        at junit.framework.Assert.failNotEquals(Assert.java:282)
        at junit.framework.Assert.assertEquals(Assert.java:64)
        at junit.framework.Assert.assertEquals(Assert.java:201)
        at junit.framework.Assert.assertEquals(Assert.java:207)
        at com.depeng.MoneyTest.testCurrency(MoneyTest.java:29)

testAmount(com.depeng.MoneyTest)  Time elapsed: 0 sec  <<< ERROR!
java.lang.AssertionError
        at com.depeng.MoneyTest.testAmount(MoneyTest.java:24)


Results :

Failed tests:   testCurrency(com.depeng.MoneyTest): expected:<1> but was:<2>

Tests in error:
  testAmount(com.depeng.MoneyTest)

Tests run: 4, Failures: 1, Errors: 1, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.712s
[INFO] Finished at: Tue Mar 25 22:06:01 SGT 2014
[INFO] Final Memory: 8M/222M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project junit3-showcase: There are test failures.
[ERROR]
[ERROR] Please refer to /Users/depeng/github/junit3-junit4-comparison/junit3-showcase/target/surefire-reports for the individual test results.
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

IDEA run

   java -ea
    -classpath "/Applications/IntelliJ IDEA 13.app/lib/idea_rt.jar:
    /Applications/IntelliJ IDEA 13.app/plugins/junit/lib/junit-rt.jar:
    /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/lib/ant-javafx.jar/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/lib/dt.jar:
    /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/lib/javafx-doclet.jar:
    /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/lib/javafx-mx.jar:
    /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/lib/jconsole.jar:
    /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/lib/sa-jdi.jar:
    /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/lib/tools.jar:
    /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/charsets.jar:
    /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/deploy.jar:
    /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/htmlconverter.jar:
    /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/javaws.jar:
    /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/jce.jar:
    /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/jfr.jar:
    /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/jfxrt.jar:
    /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/jsse.jar:
    /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/management-agent.jar:
    /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/plugin.jar:
    /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/resources.jar:
    /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/rt.jar:
    /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/ext/dnsns.jar:
    /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/ext/localedata.jar:
    /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/ext/sunec.jar:
    /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:
    /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:
    /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/ext/zipfs.jar:
    /Users/depeng/github/junit3-junit4-comparison/junit3-showcase/target/test-classes:
    /Users/depeng/github/junit3-junit4-comparison/junit3-showcase/target/classes:
    /Users/depeng/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar"

    com.intellij.rt.execution.application.AppMain
    com.intellij.rt.execution.junit.JUnitStarter
    -ideVersion5
    com.depeng.MoneyTest

1.3 How to package test case and run?

    public class PackageTestCases {
        public static Test onlyOne() {
            return new TestSuite(MathTest.class);
        }

        public static Test multiple() {
            TestSuite suite = new TestSuite("multiple tests");

            suite.addTestSuite(MathTest.class);
            suite.addTestSuite(MoneyTest.class);

            return suite;
        }

        public static void main(String args[]) {
            junit.textui.TestRunner.run(onlyOne());
            junit.textui.TestRunner.run(multiple());
        }
    }

1.4 How Junit3 find the test method? use java reflection?

  • read source code from junit.textui.TestRunner
    • main() --> start(args): 很简洁的main方法,退出处理做的不错
    • start(args) : maybe the junit3 only have few arguments, so the args processing is easy and raw
      • Usage: TestRunner [-wait] testCaseName, where name is the name of the TestCase class
    • after processing the arguments,
          	try {
          			Test suite= getTest(testCase);
          			return doRun(suite, wait);
          		}
    • Test getTest(String suiteClassName):
    • Test interface have 2 abstract method, run() and countTestCases()
  • Above all, junit 3 use reflection to find test suite and run.

1.5 junit 3 packages

  • textui
  • swingui: //
  • runner
  • framework
    • Test interface: run(TestResult result) method, result as a parameter for collect all result
    • TestCase: very important class that implements Test.
  • extensions
  • awtui: //

junit3-junit4-comparison's People

Contributors

congdepeng avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.