实例化异常抛出,针对我的类扩展。

huangapple go评论85阅读模式
英文:

InstantiationException thrown for my Class Extension

问题

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);
    }
}
英文:

I have written the following class, there rest of the code within the class is @Overrides, please let me know if the rest of the code is relevant here and I shall add it.

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

Which when my tests are run I get thrown InstantiationException's.

Looking at the API I have tried to go through what throws this error and narrow it down but to no avail, here are the logs for one of the errors.


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)

I am running this test

import org.junit.Before;
import java.util.ArrayList;
import java.util.List;
public abstract class ListCmdTest extends CommandTest {
protected static final String SHORT_ARGUMENT = &quot;short&quot;;
protected static final String LONG_ARGUMENT = &quot;long&quot;;
@Override
protected CommandType getCmdType() {
return CommandType.LIST;
}
@Before
public void setup() {
testCommand = new ListCmd(SHORT_ARGUMENT);
testLibrary = new LibraryData();
List&lt;BookEntry&gt; bookData = new ArrayList&lt;&gt;();
bookData.add(new BookEntry(&quot;TitleA&quot;, new String[]{&quot;AuthorA&quot;}, 3.2f, &quot;ISBNA&quot;, 500));
bookData.add(new BookEntry(&quot;TitleB&quot;, new String[]{&quot;AuthorB&quot;}, 4.3f, &quot;ISBNB&quot;, 400));
bookData.add(new BookEntry(&quot;TitleC&quot;, new String[]{&quot;AuthorC&quot;}, 1.3f, &quot;ISBNC&quot;, 300));
FieldTestUtils.setPrivateField(testLibrary, testLibrary.getClass(), &quot;books&quot;, bookData);
}
}

And then these tests fail


import org.junit.Test;
import static org.junit.Assert.assertEquals;
public abstract class CommandTest {
protected static final String TITLE_ARGUMENT = &quot;TITLE&quot;;
protected static final String AUTHOR_ARGUMENT = &quot;AUTHOR&quot;;
protected static final String BLANK_ARGUMENT = &quot;&quot;;
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() + &quot; has unexpected superclass.&quot;, 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);
}
``
</details>
# 答案1
**得分**: 2
来自 [`InstantiationException`][1] 的 Java 文档:
&gt; 当应用程序尝试使用 `Class` 类中的 `newInstance` 方法创建类的实例时,如果指定的类对象无法实例化,则会抛出此异常。实例化可能因各种原因而失败,包括但不限于:
&gt;
&gt;  - 类对象表示一个**抽象类**、一个接口、一个数组类、一个基本类型或 void
&gt;  - 类没有无参数构造函数
你的 `ListCmdTest` 类是抽象的,所以 jUnit 无法实例化它。
[1]: https://docs.oracle.com/javase/8/docs/api/java/lang/InstantiationException.html
<details>
<summary>英文:</summary>
From javadoc of [`InstantiationException`][1]:
&gt; 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:
&gt;
&gt;  - the class object represents an **abstract class**, an interface, an array class, a primitive type, or void
&gt;  - the class has no nullary constructor
Your `ListCmdTest` class is `abstract`, so jUnit cannot instantiate it.
[1]: https://docs.oracle.com/javase/8/docs/api/java/lang/InstantiationException.html
</details>

huangapple
  • 本文由 发表于 2020年4月7日 17:02:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/61076374.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定