Tuesday 15 June 2010

junit - How can I use an assertion to test this java code? -



junit - How can I use an assertion to test this java code? -

my homework assignment read url , print hyperlinks @ url file. need submit junit test case @ to the lowest degree 1 assertion. have looked @ different forms of assert can't come utilize of them applies code. help steering me in right direction great.

(i'm not looking write test case me, little guidance on direction should looking in)

public void savehyperlinktofile(string url, string filename) throws ioexception { url pagelocation = new url(url); scanner in = new scanner(pagelocation.openstream()); printwriter out = new printwriter(filename); while (in.hasnext()) { string line = in.next(); if (line.contains("href=\"http://")) { int = line.indexof("\""); int = line.lastindexof("\""); out.println(line.substring(from + 1, to)); } } in.close(); out.close(); }

}

try decompose method simpler ones:

list<url> readhyperlinksfromurl(url url); void writeurlstofile(list<url> urls, string filename);

you test first method saving sample document resource , running against resource, comparing result known list of urls.

you can test sec method re-reading file.

but can decompose things farther on:

void writeurlstowriter(list<url> urls, author writer); writer createfilewriter(string filename);

now can test first method writing stringwriter , checking, written there asserting equality of writer.tostring() sample value. not methods becoming simpler , simpler.

it excercise write whole thing test-first or play ping-pong yourself.

good luck , happy coding.

java junit assertions

No comments:

Post a Comment