Junit test + JunitParams does not read csv



  • Test

    @RunWith(JUnitParamsRunner.class)
    public class ParamTest {
    
        @Test
        @FileParameters("src/test/resources/param.csv")
        public void test(int size, String[] arr) {
            assertEquals(size, arr.length);
        }
    }
    

    and the file from which read:

    2, asdf, asdf

    In this case, the code works, the value of 2 as int other values fall into the Strings.

    Change the arguments and order of values in the file

    @RunWith(JUnitParamsRunner.class)
    public class ParamTest {

        @Test
        @FileParameters("src/test/resources/param.csv")
        public void test(String[] arr, int size) {
            assertEquals(size, arr.length);
        }
    }
    

    File:

    "asdf, asdf", 2

    Separate, according to the format of the csv file, one field, the code makes a mistake.

    java.lang.IllegalArgumentException: wrong number of arguments

    I mean. How do I know if there's a primitive, and then a mass, all the meanings to end the file, if it's not possible to indicate where the body ends in the file or the functionality of JunitParams does not allow that?



  • I mean. I understand if there's a primitive and then a mass, that's all. the value until the end of the file to enter this area, if otherwise I can't tell where the file ends.

    Absolutely.

    or the functionality of JunitParams prevents this from happening?

    It allows, but only using the method as a data source. In the case of CSV, that's impossible.

    Options for decision two are:

    1. No JUnitParams, pass the CSV on your own, using the data reading rules you need. Example can be seen http://blog.wedoqa.com/2014/03/reading-test-data-from-a-csv-file-using-junit/ ♪
    2. Use the method as a data source. https://github.com/Pragmatists/JUnitParams/blob/master/src/test/java/junitparams/usage/SamplesOfUsageTest.java :

      public Object mixedParameters() {
          boolean booleanValue = true;
          int[] primitiveArray = {1, 2, 3};
          String stringValue = "Test";
          String[] stringArray = {"one", "two", null};
          return $(
              $(booleanValue, primitiveArray, stringValue, stringArray)
          );
      }
      

      @Test
      @Parameters(method = "mixedParameters")
      @TestCaseName("{0}, {1}, {2}, {3}")
      public void usageOfMultipleTypesOfParameters(
      boolean booleanValue, int[] primitiveArray, String stringValue, String[] stringArray) {
      }




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2