Leider kann Ihr Browser nicht alle Designelemente dieser Seite korrekt darstellen. Damit Sie trotzdem die kompletten Inhalte sehen können, bieten wir Ihnen diese abgerüstete Version an. Um das korrekte Design der Seite zu sehen, probieren Sie bitte einen anderen Browser.

 

Demo

Step 1 | Step 2 | Step 3 | Step 4 | Step 5 | Step 6

drucken top

 

Step 1 - Installation and Setup

Make sure tools.jar (part of JDK), junit.jar, and junitdoclet.jar are in the class path. Doublecheck the directories in the build.xml.

The junit task in ANT is optional, but we need it to execute the demo. Please make the optional.jar available to ANT (e.g. put it to %ANT_HOME%/lib). It's worth it.

Define an ANT target for JUnitDoclet in the build.xml and assign the package to be processed and the name of the main test (the TestSuite covering the processed package).

Note: By default, the name of a TestSuite contains the name of the parent package (package org.junitdoclet.demo leads to DemoSuite). Please read the FAQ if you want to change that to AllTests or something else. (There seems to be no general rule about naming TestSuites because they are related to packages by JUnitDoclet only but not by JUnit.)

Fasten your seat belt (but don't put the tables in front of you to their upright position ;-).

 
demo/
  build.xml
  lib/
    junit.jar
    junitdoclet.jar
  src/
  junit/
  build/
drucken top

 

Step 2 - Application Classes

If you are here for the first time, just create the application classes (that you know of already), define their methods and make them compilable. You can do more, but if you prefer "test first" you don't do that now. ("test first" means, you synchronize with your tests first, implement them, see them failing... not before then you start to implement the application classes and make them pass the tests.) For now let's compile the application classes.

If you are here for the second time, chances are some tests have failed. Implement and run the tests again. If you need to create a new class or a public method, don't forget to synchronize your tests (see above).

 
demo/
  build.xml
  lib/
  src/
    org/
      junitdoclet/
        demo/
          SampleClass.java
          sub/
            SampleSubClass.java
  junit/
  build/
drucken top

 

Step 3 - Compiled Application Classes

The application classes are compilable. This is necessary for javadoc to be able to parse the source code of them.

 
demo/
  build.xml
  lib/
  src/
  junit/
  build/
    classes/
      org/
        junitdoclet/
          demo/
            SampleClass.class
            sub/
              SampleSubClass.class
drucken top

 

Step 4 - Generating Tests With JUnitDoclet

Javadoc is parsing the source code of the application classes. From the collected information, JUnitDoclet writes TestCases and TestSuites preferably to a different directory tree (here: junit/).

  • For each package there is a TestSuite.
  • For each public, non-abstract class there is a TestCase.
  • For each public method there is a test method. In case of accessor tests, set and get are tested together.

Not all the tests can be compiled directly after they have been generated for the first time. But most of them will. The compiler will help you with the others by pointing to code where:

  • A class does not have a public constructor.
  • A class does not have a constructor without parameters (default constructor).
  • An accessor for double or float need some epsilon when comparing two values. (see SampleSubClassTest.java)

There is no wizard but you to fix these problems. But don't be sad, it is easy to solve these problems. Did you ever try to outsmart a wizard that did not produce what you wanted? And after all, the fix remains even during re-generation because it is inside a marker pair.

 
demo/
  build.xml
  lib/
  src/
  junit/
    org/
      junitdoclet/
        demo/
          SampleClassTest.java
          DemoSuite.java
          sub/
            SampleSubClassTest.java
            SubSuite.java
  build/
drucken top

 

Step 5 - Compiled Tests and Application Classes

If you've reached this state without any warning, you managed to synchronize tests and application classes.

Go to the next step to perform the tests.

 
demo/
  build.xml
  lib/
  src/
  junit/
  build/
  classes/
  org/
  junitdoclet/
  demo/
  SampleClass.class
  SampleClassTest.class
  DemoSuite.class
  sub/
  SampleSubClass.class
  SampleSubClassTest.class
  SubSuite.class
drucken top

 

Step 6 - Running the Tests

Do your tests pass? You will see the result in the testresult.txt (but you can direct it to the console as well; see build.xml)

If the tests fail, go back to step 2.

Still here? Congratulation. It's time to implement the next feature. Go to step 2 for the first time again. ;-)

 
demo/
  build.xml
  lib/
  src/
  junit/
  build/
  testresults.txt
  classes/
  org/
  junitdoclet/
  demo/
  SampleClass.class
  SampleClassTest.class
  DemoSuite.class
  sub/
  SampleSubClass.class
  SampleSubClassTest.class
  SubSuite.class
drucken top