I am trying to write a JUnit test for a function that finds the median for a given array of double
elements.
However, I am struggling with passing the parameters. My code looks like this:
@RunWith(Parameterized.class)
public class MedianParameterizedTest extends TestCase {
private Double[] numbers;
private Double expectedMedian;
public MedianParameterizedTest(Double[] numbers, double expectedMedian){
this.numbers = numbers;
this.expectedMedian = expectedMedian;
}
@Parameterized.Parameters
public static Collection medianArrays() {
return Arrays.asList(new Object[][] {
{ {-5.0,-4.0,3.0}, -4.0}
});
}
@Test
public void test1() {
//doing test
}
}
But this gives me an illegal initializer for java.lang.Object error for the medianArrays collection and I can't find out why.
You need to create the first (array) argument for each test using new Double[]
.
You have three levels of nested arrays in your code:
After new Object[][]
, Java knows that two levels of nested arrays are to be expected. After new Object[][] {
, it still knows that there is one more level of array to expect, but after new Object[][] { {
, it's not expecting any more further levels of nested array. If you want to create an array at this point you must specify the type.
Try the following instead:
@Parameterized.Parameters
public static Collection medianArrays() {
return Arrays.asList(new Object[][] {
{ new Double[] {-5.0,-4.0,3.0}, -4.0}
});
}
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.