Eclipse already includes the junit classes. In fact it likely has both junit3.8.xxx and also junit4. I will exclusively use junit4. The functionality of junit4 is essentially an extension of junit3.8.1, but with noticeable changes in syntax since the junit4 implementation takes advantage of java's annotation feature.
When creating a project, at the Java Settings dialog, click on the Libraries tab and then the Add Library button.
A dialog with a list of libraries will appear. Choose Junit.
You then get a chance to select the version (3.8.xxx or 4). As noted earlier, I would strongly suggest selecting version 4. Then click Finish.
Eclipse will
For large projects it is a common practice to put source files in different folders. E.g., .java source files might go in a src folder; .java test files might go in a tests folder; and generate .class files might go in a bin folder.
To use separate folders, when creating a project, under Project Layout on the Create Java Project dialog, you will need to check one of these:
o Use project folder as root for sources and class filse o Create separate source and output folders
It is slightly easier to put the .java files for the class being tested and the testing class in the same directory until you are comfortable with using separate folders for a project.
public class BlobTest extends TestCase
Junit.3.8.1 (but not later version 4) contains two different ways to display output: one is just text output, the other is displayed in a graphical user interface.
The main method calls one of two methods depending on whether the text output or the graphical user interface output is desired.
(Replace BlobTest by the actual name of the testing class.) public static void main(String[] args) { junit.textui.TestRunner.run( BlobTest.class ); } public static void main(String[] args) { junit.swingui.TestRunner.run(BlobTest.class); }
assertEquals(expected_value, expression)
assertTrue(boolean_expression)
assertFalse(boolean_expresion)
assertNull(expression)
assertNotNull(expression)
fail()
If an assertXXX method fails in a test method, that test method fails.
If the fail() method is executed, it always fails. It is typically used conditionally. That is, you could write an if statement with one alternative calling fail(). This would be appropriate if that alternative should never happen. A common use is with a try .. catch construct. The catch block is only executed if an exception occurs while the try block is executed completely if no execption occurs.
If some code should throw an exception, you can test it by puting that code in a try block and put a call to fail() also in the try block just after the code. The catch block can have an empty body. If the fail() method is reached, the code did not throw an exception as it should have and the test method fails!
If a test method fails, the remaining test methods are still executed and the output reports for each method whether it succeeded or failed.
If a test failed, further information is given. E.g. if a call to assertEquals fails the expected value and the actual value of the expression are output.
Java Application JUnit Test
You can select either one.
If you select Java Application, the main routine of the test class will be executed and the results will be displayed according to the main routine:
If you select JUnit Test, Eclipse will execute is own main routine and display the results in the JUnit window.