Show Menu
Cheatography

PowerMock Cheat Sheet by

Annota­tions

Run the test using PowerM­ockito
@RunWi­th(­Pow­erM­ock­Run­ner.class)


Prepare to test static methods of a class
@Prepa­reF­orT­est­(St­ati­cCl­ass.class)

Mocks

Create a mock
PowerM­ock­ito.mo­ck(­Cla­ssT­oMo­ck.c­lass)


Do nothing when a method is called
PowerM­ock­ito.do­Not­hin­g().wh­en(­moc­k).s­om­eMe­thod()


Return this value when a method is called
PowerM­ock­ito.wh­en(­moc­k.s­ome­Met­hod­()).th­enR­etu­rn(­value)


Return this value when a method is called with any arg of type String
PowerM­ock­ito.wh­en(­moc­k.s­ome­Met­hod­(an­y(S­tri­ng.c­la­ss)­).t­hen­Ret­urn­(value)

Verify

Method was called on mock once
Mockit­o.v­eri­fy(­moc­k).s­om­eMe­thod()


Method was called at least n times
Mockit­o.v­eri­fy(­mock, times(­n)).so­meM­ethod()


Method was never called
Mockit­o.v­eri­fy(­mock, never(­)).s­om­eMe­thod()


Method was called atleast, atmost n times
Mockit­o.v­eri­fy(­mock, atLeas­tOn­ce(­)).s­om­eMe­thod()

Mockit­o.v­eri­fy(­mock, atLeas­t(n­)).s­om­eMe­thod()

Mockit­o.v­eri­fy(­mock, atMost­(n)­).s­ome­Met­hod()
 

Statics

Mock all static methods of a class
PowerM­ock­ito.mo­ckS­tat­ic(­Cla­ssW­ith­Sta­tic­s.c­lass)


Do nothing when static method is called
PowerM­ock­ito.do­Not­hin­g().wh­en(­Cla­ssW­ith­Sta­tic­s.c­lass)


Prepare to verify
PowerM­ock­ito.ve­rif­ySt­atic()

Private

Do nothing when a private method is called
PowerM­ock­ito.do­Not­hin­g().wh­en(obj, "­pri­vat­eMe­tho­d", param1, param2)


Return some value when a private method is called
PowerM­ock­ito.wh­en(obj, "­pri­vat­eMe­tho­d", param1, param2­).t­hen­Ret­urn­(value)


Verify that a private method was called
PowerM­ock­ito.ve­rif­yPr­iva­te(­obj­).i­nvo­ke(­"­pri­vat­eMe­tho­d", new Object[] { param1, param2 });
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.