Detailed steps for using JUnit with Eclipse
- Launch eclipse. If you do not see your bj4 project from
assignment 7, follow these
steps to set it up.
- Add junit to your project classpath:
- Open the "Project" menu, chose the "Properties" command
- In the "Properties for bj4" dialog, click "Java Build Path" at left
- Select the "Libraries" tab, you will see a list of libraries currently in your project
- Click "Add External JARs...", then drill into "plugins" and "org.junit_3.8.1", and select "junit.jar".
- Click OK to select the junit.jar file, and OK to exit "Properties for bj4"
- Add a new file to your project
- Switch to the "Java perspective" by using the icon with a "J" at the extreme left
edge of the Eclipse window.
- From the "File" menu, choose "New", and "Class"
- In the "New Java Class" dialog, type the name "HandTest"
- Enter "junit.framework.TestCase" as the superclass (you have to type it out, you cannot use "Browse...")
- Click Finish
- You will now see an empty class definition
- Add member variable "public Hand testHand"
- Add setUp and tearDown methods
- In "public void setUp()" set testHand to a new Hand object.
- In "public void tearDown()" set testHand to null.
- Add test methods
- Name each method testXXX were "XXX" is something descriptive.
- Declare each test method as "public void" and with no arguments.
- Each test method can use local variables to create cards, and
call methods on testHand
- Each test method should end with one or more calls to:
assertTrue(condition), assertFalse(condition),
assertEquals(object1, object2), assertNull(object),
assertNonNull(object).
- Remember that the ranks of cards are zero-based, so new
Card(Card.CLUBS, 0) is an Ace, and new Card(Card.CLUBS, 3) is the
"Four of clubs".
- Also, remember that assertEquals() with Strings considers
spacing, newlines, and capitalization.
- Run the tests
- Save your HandTest.java file
- From the "Run" menu, choose "Run as" and "JUnit Test"
- Or, you can choose "Debug as" and "JUnit Test"
- You should see a new "JUnit" pane with a bright red and/or
green progress bar that indicates how many tests have been passed
succesfully.
- If you have any test methods fail, correct HandTest.java (not
Hand.java) and rerun the tests. If you don't know why a test
method is failing, you can set a breakpoint and use the
debugger.
- Keep adding test methods until you are sure that you have 100% branch coverage of Hand.java:
- Cover the methods: addCard(), getValue(), busted(), and toString()
- For each of those methods, create test cases that exercise all
branches. I.e., both the "then" and "else" parts of every
if-statement, and all loops should be both executed and skipped
(meaning zero loop iterations).
- If you run your junit tests and see error messages that look like
"invoke0(Method, Object, Object[])line: not available [native
method]" then you need to exit Eclipse and reload it.
example use case templatesample test plan templateProject plan template