3

I'm trying to run some new parameterized Android tests with null in the parameter set. Unfortunately, the values are showing up as the string "null" instead of null. How can I keep this from happening?

@RunWith(Parameterized.class)
public class PedigreeProviderTest {

    @Parameter()
    @SuppressWarnings("WeakerAccess")
    public String mVin;
    @Parameter(value = 1)
    @SuppressWarnings("WeakerAccess")
    public String mDap;
    @Parameter(value = 2)
    @SuppressWarnings("WeakerAccess")
    public int mAddress;
    @Parameter(value = 3)
    @SuppressWarnings("WeakerAccess")
    public int mBusId;

    @Parameters
    public static Collection<Object[]> parameters() {
        return Arrays.asList(new Object[][]{
                {null, null, 0x7F, 0},
                {"1FUYFXYB3XPA96364", null, 0x7F, 0},
                {"1HD4CAM30YK190948", "00D06948C67C", 0x7F, 0},
                {null, "00D06948C67C", 0x7F, 0},
        });
    }

    @Test
    public void testSomething(){
        "null".equals(mVin); //is true for parameter 0 and 3
        mVin == null; //is never the case
    }

    ...
}
Jon
  • 9,156
  • 9
  • 56
  • 73

1 Answers1

0

I ended up initializing the parameters in a constructor and using a conditional check to set the fields to null when they are "null"

@RunWith(Parameterized.class)
public class PedigreeProviderTest {
    private String mVin;
    private String mDap;
    private int mAddress;
    private int mBusId;

    @Parameters
    public static Iterable<Object[]> parameters() {
        return Arrays.asList(new Object[][]{
                {"1FUYFXYB3XPA96364", null, 0x7F, 0},
                {"1HD4CAM30YK190948", "00D06948C67C", 0x7F, 0},
                {null, "00D06948C67C", 0x7F, 0},
                {null, null, 0x7F, 0},
        });
    }

    public PedigreeProviderTest(String vin, String dap, int address, int busId) {
        mVin = "null".equals(vin) ? null : vin;
        mDap = "null".equals(dap) ? null : dap;
        mAddress = address;
        mBusId = busId;
    }

    /*tests...*/

}
Jon
  • 9,156
  • 9
  • 56
  • 73