当前位置: 首页 > 知识库问答 >
问题:

JUnit 5集合类型参数导致“@ParameteredTest”出现错误

郑衡
2023-03-14

我无法从之前提出的问题中找到错误的原因,因为它们是关于“@Test”(不允许自定义数据类型)的。

我有一个程序,它接受一个字符串类型的输入(通常是一个文本块),并以列表的形式返回输入的句子。为了正确测试这个程序,我试图以列表的形式存储我的输入和预期输出(这将与我程序的输出进行比较,测试的目的)。这里数据结构类由两个属性组成:字符串类型属性和列表类型属性。

总的来说,我有一些关于程序本身的必要类,SentenceSplitterTest类,DataStructure类和Collection类。在Collection类中,我处理上述列表的加载过程。我基本上做一些文件操作,并将输入和预期输出存储在Collection类中。下面是我的程序测试的内容。java文件。

package Classes;

import org.junit.Before;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

@RunWith(Parameterized.class)
public class SentenceSplitterTest  {

    private String input;
    private List<String> output = new ArrayList<>();
    private SentenceSplitter sentencesplitter;
    private static CollectionClass collectionClass;

    static {
        try {
            collectionClass = new CollectionClass();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public SentenceSplitterTest(String input, List<String> output)  {
        super();
        this.input = input;
        this.output = output;
    }

    @Before
    public void initialize() throws IOException {

        sentencesplitter = new SentenceSplitter();
    }

    @ParameterizedTest
    @MethodSource("data")
    public void testSentenceSplitterTest() {

        System.out.println("Expected output: " + output);
        assertEquals(output, sentencesplitter.sentenceSplit(input));
    }

    public static Collection data() {
        return collectionClass.getContent();
    }
}

如您所见,我使用@MethodSource(“数据”)来设置参数。我的测试屏幕是这样的

我在屏幕截图中看到以下初始化错误:

JAVA异常:类上没有公共静态参数方法。句子拆分测试

我在屏幕截图中的每个元素都有以下错误:

org.junit.jupiter.api.extension.参数解析异常:在可执行文件[公共类. SentenceSplitterTest(java.lang.String,java.util.List)]中没有为参数[java.lang.String art0]注册参数解析程序。我找不到解决问题的方法。请帮助。

编辑1:我被要求分享CollectionClass课程。

package Classes;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import static java.nio.file.Files.readAllLines;

public class CollectionClass {

    private static List<DataStructure> collection = new ArrayList<>();

    public List<DataStructure> getContent() {
        return collection;
    }

    public CollectionClass() throws IOException {

        List<String> inputs = readAllLines(Paths.get("inputs.txt"), StandardCharsets.UTF_8);
        List<String> outputs = readAllLines(Paths.get("outputs.txt"), StandardCharsets.UTF_8);

        int i = 0;
        int j = 0;
        int ctr = 0;

        while(i< inputs.size() || j< outputs.size()) {

            DataStructure structure = new DataStructure();
            String inputIncoming = null;
            List<String> outputIncoming = new ArrayList<String>();

            if(i< inputs.size()) {

                if(!inputs.get(i).equals("-")) {
                    // nothing

                }
                else {
                    i++;
                    inputIncoming = inputs.get(i);
                   structure.string = inputIncoming;
                }
            }

            if(j< outputs.size()) {


                if(!outputs.get(j).equals("-") && !outputs.get(j).equals("--")) {
                    // nothing
                }
                else {
                    if(outputs.get(j).equals("-")) {
                        j++;
                        outputIncoming.add(outputs.get(j));
                        structure.listOfString = outputIncoming;
                    }
                    else if(outputs.get(j).equals("--")) {
                        j++;
                        outputIncoming.add(outputs.get(j));
                        j++;
                        outputIncoming.add(outputs.get(j));
                        structure.listOfString = outputIncoming;
                    }
                }
            }

            collection.add(ctr,structure);

            i++;
            j++;
            ctr++;
        }
        for(DataStructure structure: collection) {
            System.out.println(structure.string);
            System.out.println(structure.listOfString);
        }
    }
}

我也想添加Data结构类。

package Classes;

import java.util.ArrayList;
import java.util.List;

public class DataStructure {

    public String string;
    public List<String> listOfString = new ArrayList<>();
}

编辑2:我被要求放置stacktrace。给你。

java.lang.NullPointerException
    at Classes.SentenceSplitterTest.testSentenceSplitterTest(SentenceSplitterTest.java:46)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:389)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:115)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:167)
    at org.junit.jupiter.engine.execution.ThrowableCollector.execute(ThrowableCollector.java:40)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:163)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:110)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:57)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$execute$3(HierarchicalTestExecutor.java:83)
    at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:77)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$null$0(HierarchicalTestExecutor.java:85)
    at org.junit.jupiter.engine.descriptor.TestTemplateTestDescriptor.execute(TestTemplateTestDescriptor.java:114)
    at org.junit.jupiter.engine.descriptor.TestTemplateTestDescriptor.lambda$execute$2(TestTemplateTestDescriptor.java:86)
    at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
    at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
    at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
    at java.base/java.util.stream.ReferencePipeline$11$1.accept(ReferencePipeline.java:442)
    at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
    at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
    at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
    at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
    at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
    at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1492)
    at java.base/java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:658)
    at java.base/java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:274)
    at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
    at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
    at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
    at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
    at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
    at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:497)
    at java.base/java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:274)
    at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
    at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
    at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
    at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1492)
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
    at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
    at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
    at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:497)
    at java.base/java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:274)
    at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1492)
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
    at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
    at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
    at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:497)
    at org.junit.jupiter.engine.descriptor.TestTemplateTestDescriptor.execute(TestTemplateTestDescriptor.java:86)
    at org.junit.jupiter.engine.descriptor.TestTemplateTestDescriptor.execute(TestTemplateTestDescriptor.java:36)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$execute$3(HierarchicalTestExecutor.java:83)
    at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:77)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$null$2(HierarchicalTestExecutor.java:92)
    at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
    at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:177)
    at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133)
    at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
    at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
    at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
    at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:497)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$execute$3(HierarchicalTestExecutor.java:92)
    at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:77)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$null$2(HierarchicalTestExecutor.java:92)
    at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
    at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:177)
    at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133)
    at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
    at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
    at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
    at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:497)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$execute$3(HierarchicalTestExecutor.java:92)
    at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:77)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:51)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:43)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:170)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:154)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:90)
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

共有1个答案

欧阳昊焱
2023-03-14

您正在混合JUnit 4和JUnit Jupiter参数化测试。在JUnit JUpiter中,没有测试运行程序,构造函数保持无参数,您将参数传递给测试方法(在某种程度上有点让人想起JUnit 4的理论):

public class SentenceSplitterTest  {
    private SentenceSplitter sentencesplitter;
    private static CollectionClass collectionClass;

    // Initialization of CollectionClass moved here (instead of a static block) for two 
    // reasons:
    // 1. If the initialization fails, you can't run the test anyway - better fail 
    //    right here that print an error and continue to the test which we 
    //    know won't work
    // 2. It just looks neater
    @BeforeAll
    public static void initializeCollectionClass() throws IOException {
        collectionClass = new CollectionClass();
    }

    @BeforeEach
    public void initializeSentenceSplitter() throws IOException {
        sentencesplitter = new SentenceSplitter();
    }

    @ParameterizedTest
    @MethodSource("data")
    public void testSentenceSplitterTest(DataStructure ds) {
        String input = ds.string;
        List<String> output = ds.listOfString;
        assertEquals(output, sentencesplitter.sentenceSplit(input));
    }

    public static Stream<DataStructure> data() {
        return collectionClass.getContent().stream();
    }
}
 类似资料:
  • 这可能是一个Java101问题。但是我已经离开java十年了,所以它对我来说是新的。 而且,changeFurColor()使用实现 的比较器对输入ArrayList进行排序,因此我需要在changeFurColor()方法中使用这个参数化类型。 我用changeFurColor(dogs)调用方法; 但是,这不会编译。类型 与类型 不匹配,尽管前者实现了后者。 伪代码: 更新changeFurC

  • 我试图理解为什么当我在MyModel中为T使用更高类型的参数时,以下代码无法编译 但是如果我把它改成< code > new Bar[my model[Any]]它就会编译。这是为什么呢?

  • 问题内容: 数据输入: 存储过程的相关部分: 错误:将varchar数据类型转换为datetime数据类型导致值超出范围。 这是CONVERT(DATETIME)的唯一调用,并且我不使用CAST 如果我只是直接传递数据,则会插入该行。如果我运行该存储过程,它将超出范围。 问题答案: 在SQL Server中使用的安全日期时间字符串格式为 或。 如果SET DATEFORMAT为dmy,则转换将失败

  • 我整合了Swashbuckle。OData 3.2.0和Swashbuckle。我的WepAPI OData webservice中的Core 5.5.3来生成Swagger documentaion。当我试图访问swaggerendpoint(http://localhost:52460/swagger)时,我得到了下面的异常,这似乎是由一个函数(AppointmentsForUsers)引起的

  • 问题内容: 大家好,我正在使用dbunit。我正在尝试将db的数据集导出到xml文件中。 但是它给出了这样的错误: 问题答案: 我的猜测是您的数据库中有一个表。但是是MSSQL中的保留关键字。DBUnit创建一些查询,例如: 但这会产生您所看到的错误。相反,DBUnit必须创建如下查询: 我认为您可以使用DBUnit 的转义模式配置设置来解决此问题。我自己没有尝试过,但是类似的东西应该可以工作: