01.02.2013 Views

Software Development Cross Solution - Index of - Free

Software Development Cross Solution - Index of - Free

Software Development Cross Solution - Index of - Free

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

first fail<br />

Red: write (failing) tests<br />

The first step is to write a test. The user story says we need to process<br />

and store order information, so let’s assume we’ll need a new class for<br />

that, called OrderProcessor:<br />

import org.junit.*;<br />

public class TestOrderProcessor {<br />

@Test<br />

public void testCreateOrderProcessor() {<br />

OrderProcessor orderProcessor = new OrderProcessor();<br />

}<br />

}<br />

As you would expect, this test will fail—<br />

you don’t have an OrderProcessor<br />

yet. So now you can fix that pretty easily.<br />

Green: write code to pass tests<br />

To get your first test to pass, just add an empty OrderProcessor class:<br />

public class OrderProcessor {<br />

}<br />

That’s it. Recompile, retest, and you’re<br />

back to green. The user story says<br />

you need to process and store order<br />

information. You’ve already got classes<br />

that represent order information (and<br />

a receipt), so use those now along with<br />

the OrderProcessor class that you<br />

just created.<br />

290 Chapter 8<br />

This test doesn’t even<br />

compile, let alone pass.<br />

File Edit Window Help Failure<br />

There’s nothing special<br />

about the name<br />

OrderProcessor. It’s<br />

just a place to put<br />

business logic, since<br />

the only other classes<br />

in the app are for<br />

storing data.<br />

hfsd> javac -cp junit.jar<br />

headfirst.sd.chapter8.TestOrderProcessor.java<br />

TestOrderProcessor.java:8: cannot find symbol<br />

symbol : class OrderProcessor<br />

location: class headfirst.sd.chapter8.TestOrderProcessor<br />

OrderProcessor orderProcessor = new OrderProcessor();<br />

^<br />

TestOrderProcessor.java:8: cannot find symbol<br />

symbol : class OrderProcessor<br />

location: class headfirst.sd.chapter8.TestOrderProcessor<br />

OrderProcessor orderProcessor = new OrderProcessor();<br />

^<br />

2 errors<br />

hfsd><br />

File Edit Window Help Success<br />

hfsd> javac -d bin -cp junit.jar *.java<br />

Green: test compiles<br />

and passes.<br />

hfsd> java -cp junit.jar;.\bin org.junit.runner.<br />

JUnitCore headfirst.sd.chapter8.TestOrderProcessor<br />

JUnit version 4.4<br />

Time: 0.018<br />

OK (1 test)<br />

hfsd><br />

Download at WoweBook.Com

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!