I’m trying to write a unit test that needs to confirm if a method is called or not. I’m using 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 my system under test is Invoice. I’m running 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….
My problem is that mockito says you can only verify if a method is called on a mocked object… but I don’t want to mock this object, as it’s my object under test. I know that you can spy on the object you’re testing, so here’s what I tried:
verify(sutSpy).markInvoiceAsBilled("57");
Is what I’m trying to do not possible? Or am I just going about it the wrong way?
Thanks everyone 🙂
I’m not sure if what you are attempting to do is the best way to go about things.
I wouldn’t concern myself with verifying that
Invoice.createInvoice()calls an internal, private methodmarkInvoiceAsBilled()– instead test that callingcreateInvoice()changes the state of the Invoice object in the way you expect – i.e., thatstatusis nowBILLEDor something similar.In other words – don’t test what methods are called by
createInvoice()– test that after calling this method, the state of the object is what you expect.