How to check if a method is called on the system under test (not a layout)

I am trying to write a unit test that should confirm whether a method is called or not. I use JUnit, Mockito and PowerMock.

public class Invoice
{

  protected void createInvoice ()
  {
    // random stuff here
    markInvoiceAsBilled ("57");
  }

  protected void markInvoiceAsBilled (String code)
  {
    // marked as billed
  } 
}

So here is my test system Invoice. I am doing this test:

  public class InvoiceTest
  {
    @Test
    public void testInvoiceMarkedAsBilled ()
    {
      Invoice sut = new Invoice ();
      Invoice sutSpy = spy (sut);

      sut.createInvoice ();

      // I want to verify that markInvoiceAsBilled () was called
    }
  }

This example is just an example of what the actual code looks like ....

, mockito , , ... , . , , , :

  verify(sutSpy).markInvoiceAsBilled("57");

- ? ?

:)

+5
2

, , , .

, Invoice.createInvoice() , markInvoiceAsBilled() - , createInvoice() Invoice , , .. status BILLED - .

- , createInvoice() - , , .

+5

. , , , .

, , , .

public A {
  public a() {
    b();
    c();
  }

  public b() { /** hairy code, many branches to test */ }
  public c() { /** hairy code, many branches to test */ }
}

b c , a , b c.

public A {
  private final Dep mDep;

  public a() {
    mDep.b();
    mDep.c();
  }

  public b() { 
    mDep.b(); 
  }

  public c() { 
    mDep.c();
  }

  // dependency abstraction we create to help test
  static class Dep {
    public b() { /** hairy code, many branches to test */ }
    public c() { /** hairy code, many branches to test */ }
  }
}

A.a, A.b A.c , mDep ( , ). A.Dep.b A.Dep.c.

0

All Articles