-4

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(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:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 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.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:68) at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:74) at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39) at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:161) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

My Code is this package feature;

@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.... ");
    }


}
shelja
  • 1
  • 5
  • 1
    Error free code doesn't usually display stacktraces, especially `NPEs`. – Kayaman Oct 10 '17 at 11:55
  • Well if you get a nullpointer, your code is probably not error free now, is it? Depending on what IDE you use, you might want to launch into debug mode and actually check the variable that's being passed. It would seem that it's null. If it's actually allowed to be null, handle your nullpointer exception ( `if(Account.deposit == null){ ... }` for instance...) – Wep0n Oct 10 '17 at 11:57

1 Answers1

0

NullPointerExceptions are allways an Error, because there is missing a Instance. Try the workaround from the commands if(Account.deposit == null){ ... } or post some more code. I´m writing jUnit-Tests a long time and i guess you´ve just mocked/created a dummy of a wrapper class but you forgot to create the needed members for this test.

Try to create Helper-Classes for your tests so thei can create every needed Dummy-Class for your test empty and with random fillings.

LenglBoy
  • 1,451
  • 1
  • 10
  • 24