Running Tests


This section walks you through the first part of Lesson 1, in which you create a StudentTest class and a corresponding Student class. The goal of this section is to show you how to effectively do test-driven development in IDEA.

Start by creating a new project. Click on the File menu, then click New Project. IDEA will present you with the same series of dialogs as when you created the hello project. This time, type the name of the project as lesson1. Then follow the same steps as when you created the hello projectkeep clicking Next. When you click Finish, IDEA gives you the opportunity to open the project in a new frame. Select Yes.

Before you enter code, you will need to change some project settings. From the Project tools window, select the Project (lesson1). Right-click to bring up the context menu and select Module Settings.

You should see the Paths dialog, shown in Figure C.10.

Figure C.10.


First, you must configure the lesson1 module to recognize the J2SE 5.0 language level (if it doesn't already). Near the bottom of the dialog is a drop-down list marked Language level for project (effective on restart). The parenthesized comment suggests that you'll need to exit IDEA and restart it for changes to be effective.[3] You'll do this shortly. Now, click on the drop-down list to expose the selection list. Choose the entry that indicates version 5.0.

[3] While some of the new features of J2SE 5.0 will work, others will not do so until you restart.

Next, you must specify where the module can find the JUnit JAR file. (If you're not sure what this means, refer to Lesson 1 as well as Chapter II on setting up.) Click on the tab marked Libraries (Classpath) (it appears at the top of the Paths dialog). Your Paths dialog should look like the screen snapshot shown in Figure C.11.

Figure C.11.


Currently the Paths dialog shows that you have no libraries (JAR files) on your classpath. Click the button Add Jar/Directory. You will see a Select Path dialog. Navigate to the directory in which you installed JUnit, select junit.jar, and click OK. You should be back at the Paths dialog.

Click OK to close the Paths dialog. Exit IDEA, then restart IDEA. Make sure you completely exit IDEAclose the hello project frame if it remains open.

IDEA will probably reopen with the hello project, not the lesson1 project. If it does so, select FileClose Project from the menu. Then click FileRe open and select the lesson1 project from the list of recently opened projects. If the lesson1 project does not appear, click FileOpen Project. You will then need to navigate to the directory in which you created the project. If you accepted the defaults, under Windows this directory is probably C:\Documents and Settings\userName\IdeaProjects\lesson1, where userName is your Windows login name.

Once you have reopened Lesson 1, right-click in the Project tool window. Select NewClass from the context menu. For the class name, enter StudentTest. In the StudentTest.java editor, enter the initial code from Lesson 1 for the StudentTest class:

 public class StudentTest extends junit.framework.TestCase { } 

From the Project tool window tree, right-click and select Run "StudentTest". Create an output directory if asked to do so. You should see something like the window in Figure C.12.

Figure C.12.


The pane in the lower right quadrant shows output from the test run. This is JUnit outputIDEA has integrated JUnit directly into the IDE.

If you can't see enough of the output, drag the slider bar up. The slider bar appears just above the Run tool window and separates the top half of IDEA from the bottom half. Move the mouse slowly over the slider until you see a double-headed north-south arrow, then click and drag.

Make sure you're following along with the directions in Lesson 1 as you proceed. As expected, the Output window shows a failure, since you have defined no tests in StudentTest. Modify the StudentTest code:

 public class StudentTest extends junit.framework.TestCase {    public void testCreate() {    } } 

Rerun (Shift-F10). Now, instead of a red bar and an error in the Output window, you see a green bar and two lines of output. The first line shows the actual java command passed to the operating system. The second line says that the "Process finished with exit code 0." Good. You demonstrated failure, corrected the problem, then demonstrated success.

Next, the fun part. Modify the StudentTest code again:

 public class StudentTest extends junit.framework.TestCase {    public void testCreate() {       new Student("Jane Doe");    } } 

You should see red indicators to the right. The class name Student appears in red. You can hold the mouse over any red for more information. If you click on the class name Student, you will see a little light bulb to the left of the line after a short delay. Click on the light bulb. See Figure C.13.

Figure C.13.


The light bulb represents the "intention actions" tool. It presents you with a list of actions that you can effect. As an alternative to clicking the light bulb, you can bring up the list of actions using the Alt-Enter key combination. IDEA builds the intention actions list based on what it thinks you want to do. In this case you can select the only entry in the list, Create Class "Student". When asked for a package, just click OK.

Figure C.14 shows that you now have two editors, StudentTest.java and Student.java. You can bring up either one by clicking on its tab.

Figure C.14.


The intention actions tool is one of many ways in which IDEA reduces the amount of code you must type. You have a Student class with a bunch of the relevant code already filled in. The generated class code is currently in "template" modeyou can press tab to move through various relevant fields. This gives you the opportunity to replace template code with your specifics. Tab to the argument field and replace the argument name s with name. Tab again or press the Esc key to exit template mode.

You should see no red indicators. Rerun and ensure you see green results from JUnit.

Continue with the remainder of the Lesson 1 exercise. Take advantage of the various intention actions as IDEA presents them to you. Experiment with alternate ways of effecting things. If you've been leaning toward mouse-directed options, try the key combinations to see if they help you go faster. And vice versa! Dig through the menus available and see if you can effect actions from there.



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net