Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The JUnitParams library is the perfect way to create the type of parameterized test you're describing. I include it by default with every project because this pattern is so common. See <a href="http://code.google.com/p/junitparams/" rel="nofollow">http://code.google.com/p/junitparams/</a> for the complete documentation (it's so easy it all fits on one page).</p> <p>With JUnitParams, each String included in the String[] passed to the @Parameters annotation is parsed as a CSV and is split on commas, then leading and trailing whitespace is removed from each token. The number of tokens must also match the number of arguments to the test method. Since you need to include a null, an empty string and a string that consists only of whitespace, you'll need to use a method to provide your parameters as follows:</p> <pre><code> private static final Object DEFAULT_VALUE = new String("It works!"); private static final Object OTHER_VALUE = new String("It's broken!"); private Object foo(String input) { Object output = DEFAULT_VALUE; if(input != null &amp;&amp; !"".equals(input.trim())) { output = OTHER_VALUE; } return output; } @SuppressWarnings("unused") private Object[] parameters() { return $( new Object[] { null }, new Object[] { "" }, new Object[] { " " } // ,"Other value" ); } @Test @Parameters(method = "parameters") public void testing(String input) { Object obj = foo(input); assertEquals(obj, DEFAULT_VALUE); } </code></pre> <p>The three tests defined in your original question will pass with this example code, but if you uncomment the fourth value ("Other value"), then fourth test will (properly) fail.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload