ttt307307 / junitparams

Automatically exported from code.google.com/p/junitparams
0 stars 0 forks source link

Add a wiki page that shows how to create parameters when you also want to test null values and empty strings. #51

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?

Create a parameter creation method that includes nulls such as:

    private Object[] names() {
        return $(
                     $(null),
                     $(""),
                     $("Scott"),
                     $("Shawn"),
                     $("Steve")
                );
    }

What is the expected output? What do you see instead?

I'd expect the null string to be passed into the parameter on one of the test 
runs, but instead the method blows up when trying to create the Object[].  The 
reason for this is "documented" in (hidden) issues reports and/or blog posts 
and are completely understandable.

What version of the product are you using? On what operating system?

1.0.0

Please provide any additional information below.

I'd be happy to write the page.

Original issue reported on code.google.com by smoye...@gmail.com on 13 Sep 2013 at 3:25

GoogleCodeExporter commented 8 years ago

Original comment by lipinski...@gmail.com on 26 Oct 2013 at 3:56

GoogleCodeExporter commented 8 years ago
For those finding this page via Google, including objects that can't be parsed 
from a CSV string in the arguments passed to a test can be accomplished as 
shown in the following snippet:

  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 && !"".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);
  }

This code was posted as an answer to a question on StackOverflow 
(http://stackoverflow.com/questions/20389114/junit-same-testcase-with-different-
inputs) and can be used as the basis for the suggested wiki page, since my 
experience is that sending null and empty Strings is the most common 
problematic case.

Original comment by smoye...@gmail.com on 5 Dec 2013 at 2:57