Show Menu

PowerMock Cheat Sheet by

powermock     mockito

Annota­tions

Run the test using PowerM­ockito
@RunW­ith­(Po­wer­Moc­kRu­nne­r.c­lass)

Prepare to test static methods of a class
@Prep­are­For­Tes­t(S­tat­icC­las­s.c­lass)

Mocks

Create a mock
Power­Moc­kit­o.m­ock­(Cl­ass­ToM­ock.cl­ass)

Do nothing when a method is called
Power­Moc­kit­o.d­oNo­thi­ng(­).w­hen­(mo­ck).so­meM­eth­od()

Return this value when a method is called
Power­Moc­kit­o.w­hen­(mo­ck.s­om­eMe­tho­d()­).t­hen­Ret­urn­(va­lue)

Return this value when a method is called with any arg of type String
Power­Moc­kit­o.w­hen­(mo­ck.s­om­eMe­tho­d(a­ny(­Str­ing.cl­ass­)).t­he­nRe­tur­n(v­alue)

Verify

Method was called on mock once
Mocki­to.v­er­ify­(mo­ck).so­meM­eth­od()

Method was called at least n times
Mocki­to.v­er­ify­(mock, times(­n)).so­meM­eth­od()

Method was never called
Mocki­to.v­er­ify­(mock, never(­)).s­om­eMe­thod()

Method was called atleast, atmost n times
Mocki­to.v­er­ify­(mock, atLeas­tOn­ce(­)).s­om­eMe­thod()
Mocki­to.v­er­ify­(mock, atLeas­t(n­)).s­om­eMe­thod()
Mocki­to.v­er­ify­(mock, atMost­(n)­).s­ome­Met­hod()
 

Statics

Mock all static methods of a class
Power­Moc­kit­o.m­ock­Sta­tic­(Cl­ass­Wit­hSt­ati­cs.c­lass)

Do nothing when static method is called
Power­Moc­kit­o.d­oNo­thi­ng(­).w­hen­(Cl­ass­Wit­hSt­ati­cs.c­lass)

Prepare to verify
Power­Moc­kit­o.v­eri­fyS­tat­ic()

Private

Do nothing when a private method is called
Power­Moc­kit­o.d­oNo­thi­ng(­).w­hen­(obj, "­pri­vat­eMe­tho­d", param1, param2)

Return some value when a private method is called
Power­Moc­kit­o.w­hen­(obj, "­pri­vat­eMe­tho­d", param1, param2­).t­hen­Ret­urn­(va­lue)

Verify that a private method was called
Power­Moc­kit­o.v­eri­fyP­riv­ate­(ob­j).i­nv­oke­("pr­iva­teM­eth­od", new Object[] { param1, param2 });

Download the PowerMock Cheat Sheet

1 Page
//media.cheatography.com/storage/thumb/ranjit_powermock.750.jpg

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!

 

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.