Sunday, 15 April 2012

java - Unit test for try-finally block -



java - Unit test for try-finally block -

how should write test geturl method?

public class urllist { private final string[] urls; private int index; private securerandom random; public static enum mode { value_1, value_2, value_3; } public urllist(string... urls) { if (urls == null || urls.length == 0) { throw new illegalargumentexception("the url list may bot null or empty!"); } this.urls = urls; this.index = 0; this.random = new securerandom(); } public string geturl(mode mode) { switch (mode) { case value_1: homecoming urls[0]; case value_2: homecoming urls[random.nextint(urls.length)]; case value_3: seek { homecoming urls[index]; } { index = (index + 1) % urls.length; } default: throw new runtimeexception("unknown mode!"); } } }

in above code urls array of string.

the main question how should test case value_3:?

because first time test index = 0 after value of index alter else in finally block , want test 1 time again new index value in same unit test class.

have considering creating new instance of class (the 1 encapsulating index & geturl) , each test method? thus, each test method starts freshly index=0 ...

java unit-testing switch-statement try-finally

No comments:

Post a Comment