JUnit 测试给出 NullPointerException

发布于 2024-12-12 15:43:16 字数 5247 浏览 1 评论 0原文

我的 JUnit 测试之一给了我一个 NullPointerException,但我不明白为什么。 这是测试类 PacketWrapperTest 的内容:

/**
 * Mock Node for Packet A.
 */
private Node nA;

/**
 * Packet A.
 */
private PacketWrapper packetA;

/**
 * Mock Node for Packet B.
 */
private Node nB;

/**
 * Packet B.
 */
private PacketWrapper packetB;

/**
 * Relationship A
 */
Relationship RelA;

/**
 * Relationship B
 */
Relationship RelB;

@Before
public void setup() {
    nA = mock(Node.class);
    nB = mock(Node.class);
    packetA = new PacketWrapper(nA);
    packetB = new PacketWrapper(nB);
    RelA = mock(Relationship.class);
    RelB = mock(Relationship.class);
}

@After
public void tearDown() {
    packetA = null;
    packetB = null;
}
/*
 * ---------------- Test hashContents() ---------------
 */
@Test
public void testHashContents() {//TODO: Fix
    when(nA.getProperty(PacketWrapper.KEY_CONTENTS)).thenReturn(new byte[] {1});
    packetA.hashContents();
    verify(nA).setProperty(PacketWrapper.KEY_CONTENTS, packetA.getContents().hashCode());
    verify(nA).setProperty(PacketWrapper.IS_HASH, true);
}

这是 PacketWrapperTest 的相关内容:

/**
 * DB key for the contents property.
 */
static final String KEY_CONTENTS = "contents";

/**
 * DB key for the is_hashed property.
 */
static final String IS_HASH = "is_hashed";

/**
 * Underlying neo4j node.
 */
private final Node neo4jNode;

/**
 * Creates a new Packet wrapping the specified node
 * 
 * @param neo4jNode
 *            underlying neo4j node.
 */
public PacketWrapper(Node neo4jnode) {
    this.neo4jNode = neo4jnode;
}

@Override
public byte[] getContents() {
    return (byte []) neo4jNode.getProperty(KEY_CONTENTS);
}

@Override
public void setContents(byte[] newContents) {
    neo4jNode.setProperty(KEY_CONTENTS, newContents);
}

@Override
public void hashContents() {
    neo4jNode.setProperty(KEY_CONTENTS, ((byte[])getContents()).hashCode());
    neo4jNode.setProperty(IS_HASH, true);
}

@Override
public int hashCode() {
    return neo4jNode.hashCode();
}

这是堆栈跟踪:

java.lang.NullPointerException
    at org.whispercomm.manes.server.graph.PacketWrapperTest.testHashContents(PacketWrapperTest.java:124)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

虽然 Java 由于某种原因没有给我那么长的堆栈跟踪,但我只有前两行当我复制和粘贴时看到了其余部分...

编辑:抱歉,忘记提及第 124 行

verify(nA).setProperty(PacketWrapper.KEY_CONTENTS, packetA.getContents().hashCode());

也是,我正在使用 neo4j 库。

EDIT2:添加一些调试语句后,这是测试代码:

@Test
public void testHashContents() {//TODO: Fix
    byte[] testByte = new byte[] {1};
    when(nA.getProperty(PacketWrapper.KEY_CONTENTS)).thenReturn(testByte);
    packetA.hashContents();
    System.out.println(testByte.hashCode());
    System.out.println(packetA.getContents());
    System.out.println(packetA.getContents().hashCode());
    verify(nA, times(3)).setProperty(PacketWrapper.KEY_CONTENTS, packetA.getContents().hashCode());
    verify(nA).setProperty(PacketWrapper.IS_HASH, true);
}

这是输出:

26281671
[B@19106c7
26281671

该行:

verify(nA, times(3)).setProperty(PacketWrapper.KEY_CONTENTS, packetA.getContents().hashCode());

抛出异常。

One of my JUnit tests is giving me a NullPointerException and I can't figure out why.
This is the contents of the test class, PacketWrapperTest:

/**
 * Mock Node for Packet A.
 */
private Node nA;

/**
 * Packet A.
 */
private PacketWrapper packetA;

/**
 * Mock Node for Packet B.
 */
private Node nB;

/**
 * Packet B.
 */
private PacketWrapper packetB;

/**
 * Relationship A
 */
Relationship RelA;

/**
 * Relationship B
 */
Relationship RelB;

@Before
public void setup() {
    nA = mock(Node.class);
    nB = mock(Node.class);
    packetA = new PacketWrapper(nA);
    packetB = new PacketWrapper(nB);
    RelA = mock(Relationship.class);
    RelB = mock(Relationship.class);
}

@After
public void tearDown() {
    packetA = null;
    packetB = null;
}
/*
 * ---------------- Test hashContents() ---------------
 */
@Test
public void testHashContents() {//TODO: Fix
    when(nA.getProperty(PacketWrapper.KEY_CONTENTS)).thenReturn(new byte[] {1});
    packetA.hashContents();
    verify(nA).setProperty(PacketWrapper.KEY_CONTENTS, packetA.getContents().hashCode());
    verify(nA).setProperty(PacketWrapper.IS_HASH, true);
}

This is the relevant contents of PacketWrapperTest:

/**
 * DB key for the contents property.
 */
static final String KEY_CONTENTS = "contents";

/**
 * DB key for the is_hashed property.
 */
static final String IS_HASH = "is_hashed";

/**
 * Underlying neo4j node.
 */
private final Node neo4jNode;

/**
 * Creates a new Packet wrapping the specified node
 * 
 * @param neo4jNode
 *            underlying neo4j node.
 */
public PacketWrapper(Node neo4jnode) {
    this.neo4jNode = neo4jnode;
}

@Override
public byte[] getContents() {
    return (byte []) neo4jNode.getProperty(KEY_CONTENTS);
}

@Override
public void setContents(byte[] newContents) {
    neo4jNode.setProperty(KEY_CONTENTS, newContents);
}

@Override
public void hashContents() {
    neo4jNode.setProperty(KEY_CONTENTS, ((byte[])getContents()).hashCode());
    neo4jNode.setProperty(IS_HASH, true);
}

@Override
public int hashCode() {
    return neo4jNode.hashCode();
}

And here is the stack trace:

java.lang.NullPointerException
    at org.whispercomm.manes.server.graph.PacketWrapperTest.testHashContents(PacketWrapperTest.java:124)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Although Java didn't give me that long of a stack trace for some reason, I only had the first two lines and saw the rest when I copied and pasted...

EDIT: Sorry, forgot to mention line 124 is

verify(nA).setProperty(PacketWrapper.KEY_CONTENTS, packetA.getContents().hashCode());

Also, I'm using the neo4j libraries.

EDIT2: After adding in some debugging statements this is the test code:

@Test
public void testHashContents() {//TODO: Fix
    byte[] testByte = new byte[] {1};
    when(nA.getProperty(PacketWrapper.KEY_CONTENTS)).thenReturn(testByte);
    packetA.hashContents();
    System.out.println(testByte.hashCode());
    System.out.println(packetA.getContents());
    System.out.println(packetA.getContents().hashCode());
    verify(nA, times(3)).setProperty(PacketWrapper.KEY_CONTENTS, packetA.getContents().hashCode());
    verify(nA).setProperty(PacketWrapper.IS_HASH, true);
}

And this is the output:

26281671
[B@19106c7
26281671

The line:

verify(nA, times(3)).setProperty(PacketWrapper.KEY_CONTENTS, packetA.getContents().hashCode());

is throwing the exception.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凡间太子 2024-12-19 15:43:16

第 124 行必须是以下行之一:

when(nA.getProperty(PacketWrapper.KEY_CONTENTS)).thenReturn(new byte[] {1});
packetA.getContents().hashCode()
verify(nA).setProperty(PacketWrapper.KEY_CONTENTS, packetA.getContents().hashCode());

如果 verify(nA) 或 when(...) 返回 null,则这些行将为 NPE;如果未定义属性KEY_CONTENTS,则第二个将抛出 NPE。其他行将给出堆栈跟踪,并在顶部提供其他方法。有太多不同的方式可能会导致这些事情出错,所有这些都与此处未显示的其他代码有关;你必须自己追踪这个问题。

Line 124 would have to be one of these:

when(nA.getProperty(PacketWrapper.KEY_CONTENTS)).thenReturn(new byte[] {1});
packetA.getContents().hashCode()
verify(nA).setProperty(PacketWrapper.KEY_CONTENTS, packetA.getContents().hashCode());

If verify(nA) or when(...) returns null, then those lines would NPE; if the property KEY_CONTENTS is not defined, then the second will throw an NPE. Other lines would give stack traces with other methods on top. There are too many different ways where these things could go wrong, all having to do with other code not shown here; you'll have to track this down yourself.

朦胧时间 2024-12-19 15:43:16

在该语句中,

verify(nA).setProperty(KEY_CONTENTS, packetA.getContents().hashCode());

您实际上是在调用 setProperty 之前在 nA 上调用 hashCode (通过委托),这使 Mockito 无法确定正在使用哪个方法已验证。将您的测试更改为

    @Test
public void testHashContents() {//TODO: Fix
    when(nA.getProperty(PacketWrapper.KEY_CONTENTS)).thenReturn(new byte[] {1});
    packetA.hashContents();
    int hash = packetA.getContents().hashCode();
    verify(nA).setProperty(PacketWrapper.KEY_CONTENTS, hash);
    verify(nA).setProperty(PacketWrapper.IS_HASH, true);
}

,您将看到它通过。

In the statement

verify(nA).setProperty(KEY_CONTENTS, packetA.getContents().hashCode());

you are actually calling hashCode on nA (through delegation) before you are calling setProperty which is confusing Mockito as to which method is being verified. Change your test to

    @Test
public void testHashContents() {//TODO: Fix
    when(nA.getProperty(PacketWrapper.KEY_CONTENTS)).thenReturn(new byte[] {1});
    packetA.hashContents();
    int hash = packetA.getContents().hashCode();
    verify(nA).setProperty(PacketWrapper.KEY_CONTENTS, hash);
    verify(nA).setProperty(PacketWrapper.IS_HASH, true);
}

and you will see it passes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文