Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 this issue Mar 15, 2016 · 2 comments

Comments

@GoogleCodeExporter
Copy link

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 [email protected] on 13 Sep 2013 at 3:25

@GoogleCodeExporter
Copy link
Author

Original comment by [email protected] on 26 Oct 2013 at 3:56

  • Changed state: Accepted

@GoogleCodeExporter
Copy link
Author

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 [email protected] on 5 Dec 2013 at 2:57

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant