gpt4 book ai didi

java - 深入 JUnit 参数化测试运行程序 : java. lang.ClassCastException : [Ljava. lang.Object;无法转换为 java.util.List

转载 作者:行者123 更新时间:2023-11-30 07:14:01 24 4
gpt4 key购买 nike

我正在尝试使用 JUnit 的 ParameterizedTestRunner 构建我的第一个参数化测试。我在 JUnit 的深处遇到了一个奇怪的 ClassCastException,我无法弄清楚。

奇怪的是:Intellij 中的回溯似乎从未触及我的代码。

我的基类叫做 MultiLocaleParserTest,我的子类是 ExpectNoAltTagsInSellerNamesTest。亚马逊抓取,高兴!

/**
* This is the base class for "multi-locale" tests. Its children are intended
* to be used with the JUnit Parameterized Test Runner.
*
* Currently, contains only boilerplate of use to child classes.
*
* @see <a href="https://github.com/junit-team/junit/wiki/Parameterized-tests">JUnit Parameterized Test Runner docs</a>
*/
public abstract class MultiLocaleParserTest extends BaseParserTest {

private static ArrayList<ArrayList> fixtureList;
private static ArrayList<ArrayList> expectedList;

/**
* Builds and returns an array that looks like this:
*
* { { String amazonLocale, ArrayList fixtureData, ArrayList expectedData },
* { ... },
* }
*
* TODO: A possible (probable) refactor involves switching the "fixture" and
* "expected" data structures to Maps of some type, because it's more reader-
* friendly to see e.g. fixture.get("seller_name") than fixture.get(2).
*
* @param fixtureList An ArrayList of fixture data for the test the programmer is setting up.
* @param expectedList An ArrayList of expected results for the test the programmer is setting up.
* @return A data structure for use with parameterized tests as in class ExpectNoAltTagsInSellerNames.
*/
protected static Object[] buildLocaleTestDataArray(ArrayList<ArrayList> fixtureList,
ArrayList<ArrayList> expectedList) throws Exception {
MultiLocaleParserTest.fixtureList = fixtureList;
MultiLocaleParserTest.expectedList = expectedList;
if (fixtureList.size() != expectedList.size()) {
throw new Exception("Need as many 'expected' entries as 'fixture' entries.");
}

int c = 0;
ArrayList<Object[]> testParamData = new ArrayList<>();
Iterator fixtureIterator = fixtureList.iterator();
Iterator expectedIterator = expectedList.iterator();
while (fixtureIterator.hasNext()) {
ArrayList myFixtureList = (ArrayList)fixtureIterator.next();
ArrayList myExpectedList = (ArrayList)expectedIterator.next();

String fLocale = (String)myFixtureList.remove(0);
String eLocale = (String)myExpectedList.remove(0);

if (!fLocale.equals(eLocale)) {
throw new Exception("List params to this method must be in the same order.");
}

ArrayList<Object> testParamList = new ArrayList<>();
testParamList.add(fLocale);
testParamList.add(myFixtureList);
testParamList.add(myExpectedList);

testParamData.add(testParamList.toArray());
c++;
}

return testParamData.toArray();
}

public void smokeTest() throws Exception {
// @todo derp
}
}

还有子类,它试图实际使用基类。很抱歉,这是我 6 年没有接触过 Java 并拥有 Python 思维方式和我们现有代码库的混合体。

/**
* Regression test vs MT-786 (alt="foo visible in MPS seller name fields)
*
* This multi-locale test checks that the string "alt=" does not appear in any
* seller names which are images, rather than text.
*
* This is the proof-of-concept use of JUnit Parameterized tests for multiple
* Amazon locales.
*
* @see MultiLocaleParserTest
* @see <a href="https://github.com/junit-team/junit/wiki/Parameterized-tests">JUnit Parameterized Test Runner docs</a>
*/
@RunWith(Parameterized.class)
public class ExpectNoAltTagsInSellerNamesTest extends MultiLocaleParserTest {

/**
* Params that are filled for each localized test by the JUnit parameterized
* test runner. These go in order? or type/signature? based on the return
* signature of data().
*/
private String locale;
private ArrayList fixtureData;
private ArrayList expectedData;

/**
* Here is where you, the programmer, set up fixture data and expected results data
* for each Amazon locale. You then feed it to MultiLocaleParserTest#buildLocaleTestData
* to transform it into a data structure for easy use by your actual test code.
*
* You must provide both fixture data and expected results for each test's Amazon locale
* as an ArrayList with the Amazon locale as a String at element 0.
*
* Create an ArrayList of these locale-specific ArrayLists, as below, and feed it to
* MultiLocalParserTest#buildLocaleTestDataArray() for your return value.
*
* The JUnit Parameterized Test Runner will then fill the private variables
* #locale, #fixtureData, and #expectedData with the contents of each array
* for every locale you define.
*
* NOTICE: JUnit 4.11+ should support an argument to the @Parameterized.Parameters()
* annotiation which makes *which* test blew up when a particular test blows up more
* understandable. I was unable to get it to work, possibly due to old versions of
* JUnit and/or Hamcrest Core. The thing to paste in the parens is this:
*
* name = "{index}: " + "expectProperHandlingOfImageSellerNames({0}) = {1}"
*
* cf. https://github.com/junit-team/junit/wiki/Parameterized-tests#identify-individual-test-cases
* cf. http://youtrack.jetbrains.com/issue/IDEA-109891#comment=27-560090
*
* @return The value is described by MultiLocaleParserTest#buildLocaleTestDataArray()
* @see MultiLocaleParserTest#buildLocaleTestDataArray(java.util.ArrayList, java.util.ArrayList)
*/
@Parameterized.Parameters()
public static Object[] data() throws Exception {
ArrayList<ArrayList> fixtureList = new ArrayList<>();

fixtureList.add(new ArrayList(Arrays.asList("UK", "/20130909-B0068ZAPB0-New--MST781.html")));
fixtureList.add(new ArrayList(Arrays.asList("DE", "/20130909-B004LR5PRQ-New--MST781.html")));
fixtureList.add(new ArrayList(Arrays.asList("FR", "/20130909-B001VL82J8-New--MST781.html")));

ArrayList<ArrayList> expectedList = new ArrayList<>();
expectedList.add(new ArrayList(Arrays.asList("UK", 10)));
expectedList.add(new ArrayList(Arrays.asList("DE", 10)));
expectedList.add(new ArrayList(Arrays.asList("FR", 10)));

return buildLocaleTestDataArray(fixtureList, expectedList);
}


public void expectNoAltTagsInSellerNamesTest(String locale, ArrayList<Object> fixtureData, ArrayList<Object> expectedData) {
this.locale = locale;
this.fixtureData = fixtureData;
this.expectedData = expectedData;
}

/**
* Actual test code for this class. Note the use of 'this.foo' variables,
* which are stuffed by JUnit's Parameterized Test Runner before execution.
*
* There are a number of potential small refactors here.
*
* @throws Exception
*/
@Test // MST-781
public void test() throws Exception {
File htmlPage = named((String) this.fixtureData.get(0));
assertThat(htmlPage, notNullValue());

OfferList ol = getOfferList(htmlPage);
assertThat(ol, notNullValue());

int testVal = ol.getOffers().size();
int expectVal = (Integer)this.expectedData.get(0);
assertThat(testVal, is(expectVal));

for (IOffer offer : ol.getOffers()) {
String sellerName = offer.getSellerName();

// look for seller names that have anything other than
// \w (word chars), \s (whitespace), and [.,'"()&!-]
// (in short, 'alt="Seller Name' should trigger this
// @todo change to checking for "alt="
Pattern pattern = Pattern.compile("[^\\w\\s.,'\"\\(\\)&!-]");
Matcher matcher = pattern.matcher(sellerName);
// System.out.println("Inspecting " + sellerName + ": " + matcher.find());
assertThat(matcher.find(), is(false));
}
}

public void smokeTest() throws Exception {
// @todo derp
}
}

回溯:

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.util.List
at org.junit.runners.Parameterized.getParametersList(Parameterized.java:149)
at org.junit.runners.Parameterized.<init>(Parameterized.java:135)
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.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:31)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:24)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:44)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
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)

进程结束,退出代码为 255

最佳答案

我假设您使用的是 JUnit 4.11。

@Parameters 方法

您必须更改代码的两个部分。首先是方法data()必须返回 Iterable<Object[]> ,但您返回的是 Object[] .这可以通过更改 buildLocaleTestDataArray 的最后一行来轻松解决。 :

Is: return testParamData.toArray();
Should be: return testParamData;

并更改方法的返回类型 buildLocaleTestDataArraydata()Iterable<Object[]> .

分配参数

@Parameterized 不会自动分配参数。您可以使用@Parameter(index) 方法

@Parameter(0)
public String locale;
@Parameter(1)
public ArrayList fixtureData;
@Parameter(2)
public ArrayList expectedData;

或者改变方法expectNoAltTagsInSellerNamesTest进入构造函数

public ExpectNoAltTagsInSellerNamesTest(String locale, ArrayList<Object> fixtureData, ArrayList<Object> expectedData) {
this.locale = locale;
this.fixtureData = fixtureData;
this.expectedData = expectedData;
}

关于java - 深入 JUnit 参数化测试运行程序 : java. lang.ClassCastException : [Ljava. lang.Object;无法转换为 java.util.List,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18754112/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com