【发布时间】:2013-12-30 22:33:21
【问题描述】:
我对 Java 还很陌生,而且我肯定是在我的头上。好吧,抛开免责声明,这是我的故事:
我在 XML 文件中有大量视频文件名和互补字符串,称为参考 ID。 XML 如下所示:
<testdata>
<testcase displayName="video1" refId="vid1xxyyzz" />
<testcase displayName="video2" refId="vid2aabbcc" />
.
.
<testcase displayName="video499" refId="vid499ffoooo" />
</testdata>
我已经使用 XMLBeans 将 XML 模式转换为一个类,并且可以使用此处记录的方法将数据导入几个数组:http://docs.oracle.com/javase/tutorial/reflect/special/arraySetGet.html
// 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 < tests.length; i++){
displayName = tests[i].getDisplayName();
refId = tests[i].getRefId();
Array.set(row0, i, displayName);
Array.set(row1, i, refId);
}
这一切都发生在我的测试类中的 @Parameterized 方法中,该方法必须返回一个集合。这是整个事情,缩小了:
@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<Object> 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 < 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));
}
}
当我按原样运行时,我得到java.lang.IllegalArgumentException: argument type mismatch at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)。
我认为我可能使数据结构过于复杂,但现在它是以这种方式构建的并且似乎成功地提取了数据元素,我不知道如何重建。有人看到我在这里做错了吗?
这是整个血腥的例外:
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)
【问题讨论】:
-
矩阵在哪里/如何定义?异常还有更多信息吗?
-
我会在上面添加。在类的开头,它被定义为
static Object matrix;Exception 添加。 -
嗯,问题,第 186 行是哪一行?
-
这很奇怪。我深入到
Parameterized.class,它只到第 65 行。让我看看我的 JUnit 库是否是最新的...... -
我们开始吧,
Parameterized.class来自 185..187:private Object createTestUsingConstructorInjection() throws Exception {return getTestClass().getOnlyConstructor().newInstance(fParameters);}
标签: java collections multidimensional-array junit parameterized