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

时间:2017-10-10 11:53:02

标签: java junit mockito

  

java.lang.NullPointerException at   com.Accounts.Account.deposit(Account.java:14)at   feature.PrintStatementFeature.print_statement_containing_all_transactions(PrintStatementFeature.java:30)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)at   sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)at   java.lang.reflect.Method.invoke(未知来源)at   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.invokeExplosively(FrameworkMethod.java:47)     在   org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)     在   org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)     在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)at   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)at at   org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)at at   org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)at at   org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)at at   org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)at at   org.junit.runners.ParentRunner.run(ParentRunner.java:363)at at   org.mockito.internal.runners.DefaultInternalRunner $ 1.run(DefaultInternalRunner.java:68)     在   org.mockito.internal.runners.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(JUnit4TestReference.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.... ");
    }


}

1 个答案:

答案 0 :(得分:0)

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

尝试为您的测试创建Helper-Classes,这样他们就可以为您的测试空白和随机填充创建所需的所有Dummy-Class。