1

I am coming from Python world, so please forgive my ignorance. I am trying to learn TDD with JUnit framework in Java. I am following the method described by daniel kullmann in following thread: [How to install Junit][1].

The steps I am following are:

  1. Writing Test Cases
  2. Running command javac -cp /usr/share/java/junit4.jar Test.java
  3. then java -cp /usr/share/java/junit4.jar:. org.junit.runner.JUnitCore Test

So far So good. But my question is:

  1. Do I need to run both full line of command when I have to test my class ? I mean can't it be javac Test.java and java Test ? I have exported the path in .bashrc as below:

    export JUNIT_HOME=/usr/share/java

    export PATH=$JUNIT_HOME/junit4.jar:org.junit.runner.JUnitCore:${PATH}

  2. I have downloaded latest junit framework 4-11 and kept at location /usr/local/JUNIT. But when I am running command java -cp /usr/local/JUNIT/junit-4.11.jar:. org.junit.runner.JUnitCore Test, its giving the error below:

    Exception in thread "main" java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
    at org.junit.runner.Computer.getSuite(Computer.java:28)
    at org.junit.runner.Request.classes(Request.java:75)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:117)
    at org.junit.runner.JUnitCore.runMain(JUnitCore.java:96)
    at org.junit.runner.JUnitCore.runMainAndExit(JUnitCore.java:47)
    at org.junit.runner.JUnitCore.main(JUnitCore.java:40)
    Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLC
    

Can someone please guide me. [1]: https://askubuntu.com/questions/64213/how-to-install-use-junit

4

0 回答 0