简体   繁体   中英

JUnit Test to test conversion of primitive to wrapper class

What is the best way to unit test conversion of primitive to a Wrapper. I have written where i pass an array of ints and get back an array of Integers. I know I could use ClassUtils.isPrimitiveOrWrapper() to check if its a primitive or wrapper but is there another way to do this without using external dependency.

Thanks

ClassUtils.isPrimitiveOrWrapper() checks whether a class is a primitive or a wrapper.
And you want to test if the array of Integer was correctly mapped to an array of int .
It will not really help you.
What you need in your test is asserting that each Integer was correctly mapped into the corresponding int value and in the correct order.

For example :

Integer[] fixtureIntegers = new Integer[]{Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(2)};
int[] mappedInts = myClassUnderTest.map(fixtureIntegers);

// First : assert equality size
Assert.assertEquals(fixtureIntegers.length, mappedInts.length);

// Second : assert content 
for (int i=0; i<fixtureIntegers.length; i++)
    Assert.assertEquals(fixtureIntegers[i], mappedInts[i]);
}

Of course if Integer elements may be null in the original array, you have also to handle this case in the assertion.

我建议你使用guava的Ints.asList()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM