而在中运行JUNit测试时显示NullPointerException.但我的代码完全没有错误



java.lang.NullPointerExceptioncom.Accounts.Account.codeposit(Account.java:14)特色PrintStatementFeature.print_statement_containing_all_transactions(PrintStatementFunction.java:30)位于的sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)位于的sun.reflect.NativeMethodAccessorImpl.invoke(未知源)位于的sun.reflect.DelegatingMethodAccessorImpl.invoke(未知源)位于的java.lang.reflect.Method.ioke(未知源)org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)在org.junit.runners.model.FrameworkMethod.invokeExploly(FrameworkMethod.java:47)在org.junit.internal.runners.statements.InvokeMethod.eevaluate(InvokeMethod.java:17)在org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)网址:org.junit.runners.ParentRunner.runLeaf(ParentRunner:java:325)org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)网址:org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)org.junit.runners.ParentRunner.runChildren(ParentRunner:288)org.junit.runners.ParentRunner.access$000(ParentRunner:58),网址:org.junit.runners.ParentRunner$2.eevaluate(ParentRunner.java:268)org.junit.runners.ParentRunner.run(ParentRunner:363)org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:68)在org.mockito.internal.runner.DefaultInternalRunner.run(DefaultInternalRunner.java:74)在org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)在org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:161)在org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TtestReference.java:86)在org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

我的代码是这样的包装特征;

@RunWith(MockitoJUnitRunner.class)
public class PrintStatementFeature {
@Mock Console console;  
private Account account;
@Before 
public void initialise() {
TransactionRepository transactionRepository = new TransactionRepository();
account = new Account(transactionRepository); //now create its local variable.
}
@Test public void   
print_statement_containing_all_transactions ()
{
account.deposit(1000);
account.withdraw(100);
account.deposit(500);
account.printStatement();
InOrder inOrder = inOrder(console);
inOrder.verify(console).printLine("DATE.........|...AMOUNT...|...BALANCE... ");
inOrder.verify(console).printLine("17/04/2015...|....500.....|.....1400.... ");
inOrder.verify(console).printLine("15/04/2015...|...-100.....|.....1400.... ");
inOrder.verify(console).printLine("10/04/2015...|....1000....|.....1500.... ");
}

}

NullPointerExceptions总是一个错误,因为缺少一个实例。请尝试命令if(Account.deposit == null){ ... }中的解决方法,或发布更多代码。我写了很长一段时间的jUnit测试,我想你刚刚嘲笑/创建了一个包装类的假人,但你忘了为这个测试创建所需的成员。

试着为测试创建Helper类,这样我就可以为测试创建每个需要的Dummy类,空的和随机填充的。

相关内容

  • 没有找到相关文章