-1

Test won't run correctly trying to run a JUnit test errors

package picocli;

import picocli.CommandLine.Option;

public class ComparatorRunnerConfig {

      @Option(names = {"-rc", "--report-class"}, required = false,
              description = "define report")
      private String report;

      public String getReport() {
            return report;
      }
}

My JUnit test:

package picocli;

import static org.junit.Assert.*;
import org.junit.Test;

public class ConfigurationTest {

    @Test
    public void testBasicConfigOptions() {
        String args = "-rc BlahBlah";
        ComparatorRunnerConfig mfc = new ComparatorRunnerConfig();
        new CommandLine(mfc).parse(args);

        String myTestValue = mfc.getReport();
        assertEquals(myTestValue, "BlahBlah");
    }
}

Test fails.

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
Neil Mc Feely
  • 29
  • 1
  • 1
  • 5

1 Answers1

5

The problem is that the test has a subtle bug: the intention is to pass two arguments: the option "-rc" and its option parameter "BlahBlah", but what the test actually does is pass a single argument "-rc BlahBlah" with an embedded space.

Picocli will not be able to match this input and will throw an exception (probably the error message says something like “unknown option -rc BlahBlah”, but I’m away from my pc now, so cannot verify).

The solution is to change the test to either this:

String[] args = "-rc BlahBlah".split(" ");

or this:

String[] args = new String[] {"-rc", "BlahBlah"};

This bug in the test is actually a fairly common mistake and I’ve made this mistake myself a few times. :-)

As a side-note: you can use picocli’s tracing feature to help with troubleshooting issues like this, by setting system property -Dpicocli.trace=DEBUG.

Remko Popma
  • 35,130
  • 11
  • 92
  • 114