Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I return a multidimensional array as a collection of parameters for a JUnit test?
    primarykey
    data
    text
    <p>I'm fairly new to Java, and I'm certainly in over my head. Okay, disclaimer aside, here's my story:</p> <p>I have a big list of video filenames and complementary strings called reference ID's in an XML file. The XML looks like this:</p> <pre><code>&lt;testdata&gt; &lt;testcase displayName="video1" refId="vid1xxyyzz" /&gt; &lt;testcase displayName="video2" refId="vid2aabbcc" /&gt; . . &lt;testcase displayName="video499" refId="vid499ffoooo" /&gt; &lt;/testdata&gt; </code></pre> <p>I've converted the XML schema into a class using XMLBeans and I'm able to import the data into a couple of arrays using the method documented here: <a href="http://docs.oracle.com/javase/tutorial/reflect/special/arraySetGet.html" rel="nofollow">http://docs.oracle.com/javase/tutorial/reflect/special/arraySetGet.html</a></p> <pre><code>// File import: File refIdFile = new File("C:\testdata.xml"); DisplayNameReferenceIdDoc = DisplayNameReferenceIdDocument.Factory.parse(refIdFile); displayNameReferenceId = DisplayNameReferenceIdDoc.getDisplayNameReferenceId(); tests = displayNameReferenceId.getTestcaseArray(); // Multi-dimensional array get/set: matrix = Array.newInstance(String.class, 2, tests.length); Object row0 = Array.get(matrix, 0); Object row1 = Array.get(matrix, 1); for (int i = 0; i &lt; tests.length; i++){ displayName = tests[i].getDisplayName(); refId = tests[i].getRefId(); Array.set(row0, i, displayName); Array.set(row1, i, refId); } </code></pre> <p>That's all happening in a <code>@Parameterized</code> method in my test class, which must return a collection. Here's the whole thing, zoomed out:</p> <pre><code>@RunWith(Parameterized.class) public class ValidateDisplayNameReferenceIdTest { static DisplayNameReferenceIdDocument DisplayNameReferenceIdDoc; static DisplayNameReferenceId displayNameReferenceId; static DisplayNameReferenceId.Test[] tests; static String displayName; static String refId; static Object matrix; @Parameterized.Parameters(name="{index}: {0}") public static Collection&lt;Object&gt; getTestParameters() throws IOException, XmlException { File refIdFile = new File("C:\testdata.xml"); DisplayNameReferenceIdDoc = DisplayNameReferenceIdDocument.Factory.parse(refIdFile); displayNameReferenceId = DisplayNameReferenceIdDoc.getDisplayNameReferenceId(); tests = displayNameReferenceId.getTestArray(); matrix = Array.newInstance(String.class, 2, tests.length); Object row0 = Array.get(matrix, 0); Object row1 = Array.get(matrix, 1); for (int i = 0; i &lt; tests.length; i++){ displayName = tests[i].getDisplayName(); refId = tests[i].getRefId(); Array.set(row0, i, displayName); Array.set(row1, i, refId); } System.out.println("tweet"); return Arrays.asList(matrix); // NOT SURE ABOUT THIS! } private String displayNameInput; private String refIdExpected; public ValidateDisplayNameReferenceIdTest(String input, String expected ) { displayNameInput = input; refIdExpected = expected; } @Test public void test() throws IOException { // send the API URL with 'displayNameInput', validate result for 'refIdExpected' URLConnection connection = new URL(url1 + displayNameInput + url2 + token).openConnection(); connection.setRequestProperty("Accept-Charset", charset); InputStream response = connection.getInputStream(); StringWriter writer = new StringWriter(); IOUtils.copy(response, writer); String responseString = writer.toString(); System.out.println(responseString); //Here goes something like assert(response.contains(refIdExpected)); } } </code></pre> <p>When I run this as is I get <code>java.lang.IllegalArgumentException: argument type mismatch at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)</code>.</p> <p>I think I might be over-complicating the data structure, but now that it's built that way and seems to be successfully pulling in the data elements, I don't know how else to rebuild. Anyone see what I'm doing wrong here? </p> <p>Here's the whole gory exception:</p> <pre><code>java.lang.IllegalArgumentException: argument type mismatch at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at org.junit.runners.Parameterized$TestClassRunnerForParameters.createTestUsingConstructorInjection(Parameterized.java:186) at org.junit.runners.Parameterized$TestClassRunnerForParameters.createTest(Parameterized.java:181) at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:244) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:241) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.junit.runners.Suite.runChild(Suite.java:127) at org.junit.runners.Suite.runChild(Suite.java:26) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.junit.runner.JUnitCore.run(JUnitCore.java:160) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:202) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:65) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload