java - 为我的类扩展引发 InstantiationException

标签 java instantiationexception

我编写了以下类,类中的其余代码是@Overrides,请告诉我其余代码是否与此处相关,我将添加它。

public class ListCmd extends LibraryCommand {

    public ListCmd(String argumentInput) {
        super(CommandType.LIST, argumentInput);
    }

...

当我的测试运行时,我会抛出 InstantiationException。

查看 API,我尝试找出引发此错误的原因并缩小范围,但无济于事,以下是其中一个错误的日志。


java.lang.InstantiationException
    at java.base/jdk.internal.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
    at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

我正在运行此测试

import org.junit.Before;

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

public abstract class ListCmdTest extends CommandTest {

    protected static final String SHORT_ARGUMENT = "short";
    protected static final String LONG_ARGUMENT = "long";

    @Override
    protected CommandType getCmdType() {
        return CommandType.LIST;
    }

    @Before
    public void setup() {
        testCommand = new ListCmd(SHORT_ARGUMENT);

        testLibrary = new LibraryData();
        List<BookEntry> bookData = new ArrayList<>();
        bookData.add(new BookEntry("TitleA", new String[]{"AuthorA"}, 3.2f, "ISBNA", 500));
        bookData.add(new BookEntry("TitleB", new String[]{"AuthorB"}, 4.3f, "ISBNB", 400));
        bookData.add(new BookEntry("TitleC", new String[]{"AuthorC"}, 1.3f, "ISBNC", 300));
        FieldTestUtils.setPrivateField(testLibrary, testLibrary.getClass(), "books", bookData);
    }
}

然后这些测试失败


import org.junit.Test;

import static org.junit.Assert.assertEquals;

public abstract class CommandTest {

    protected static final String TITLE_ARGUMENT = "TITLE";
    protected static final String AUTHOR_ARGUMENT = "AUTHOR";

    protected static final String BLANK_ARGUMENT = "";

    protected LibraryCommand testCommand;
    protected LibraryData testLibrary;

    public CommandTest() {
        testCommand = null;
        testLibrary = null;
    }

    protected abstract CommandType getCmdType();

    // ------------------------- initialisation tests --------------------

    @Test
    public void testClassCommandExtension() {
        assertEquals(testCommand.getClass() + " has unexpected superclass.", LibraryCommand.class,
                testCommand.getClass().getSuperclass());
    }

    @Test
    public void testCtorSuperclassCall() {
        CommandTestUtils.checkCtorSuperclassCall(testCommand, getCmdType());
    }

    // ------------------------- parseArguments tests --------------------

    @Test
    public void testIsParseArgumentsOverridden() {
        CommandTestUtils.checkIfParseArgumentsIsOverridden(testCommand);
    }

    // ------------------------- execute tests --------------------

    @Test(expected = NullPointerException.class)
    public void testExecuteLibraryDataNull() {
        testCommand.execute(null);
    }

``

最佳答案

来自InstantiationException的javadoc :

Thrown when an application tries to create an instance of a class using the newInstance method in class Class, but the specified class object cannot be instantiated. The instantiation can fail for a variety of reasons including but not limited to:

  • the class object represents an abstract class, an interface, an array class, a primitive type, or void
  • the class has no nullary constructor

您的 ListCmdTest 类是抽象,因此 jUnit 无法实例化它。

关于java - 为我的类扩展引发 InstantiationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61076374/

相关文章:

java - 如何在 Java 中处理 Play 2 框架内的异常

java - 为什么 InstantiationException 是检查异常?

Android Activity 无法实例化

java - 无法导入@Inject 注释

java - 使用 Ant 编译和运行时出现 NoClassDefFoundError

java - 为什么我在访问最终局部变量时在 Java 中出现此 InstantiationException?

java - XMLEncoder 跳过 ActionListener

Android "java.lang.RuntimeException: Unable to instantiate activity"异常

java - 它需要在 Criteria API 中创建带有日期的谓词,但仍然出现异常。如何构建这样的谓词?

java - hibernate :查找重复项