JUnit测试给出NullPointerException

时间:2011-10-28 17:12:23

标签: java junit nullpointerexception

我的一个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());

抛出异常。

2 个答案:

答案 0 :(得分:2)

第124行必须是其中之一:

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

如果验证(nA)或何时(...)返回null,那么这些行将是NPE;如果未定义属性KEY_CONTENTS,则第二个将抛出NPE。其他行将在顶部为堆栈跟踪提供其他方法。这些事情可能出错的方式太多了,所有这些都与此处未显示的其他代码有关;你必须自己跟踪这个。

答案 1 :(得分:1)

声明

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

你实际上在hashCode(通过委托)致电nA,然后致电setProperty,这使得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);
}

你会看到它通过。