Powermock is an enhanced version that is based on Easymock or Mockito, so there are two types of powermock, and if you're used to Easymock, then you download Easymock-based Powermock, Instead, you like to use Mockito to download another powermock.
Powermock said it is more powerful because it solves the problem that Easymock and mockito do not solve, that is, can imitate Static,private and final method. Examples are as follows:
public class user{
Private user User=new user ();
Public User () {
System.out.println ("New User ()");
}
public static User getinstance () {
return user;
}
private String Gethello (String h)
{
Return "Hello" +h;
}
public string SayHello (String h)
{
return Gethello (h);
}
}
Public Classsubuser extends user{
Public Subuser () {
System.out.println ("New Subuser ()");
}
}
Test case, note that the following example needs to be JUNIT4 to run, if not, we recommend the next jar package: Junit-4.0.jar
@RunWith (Powermockrunner.class)//powermock pack has two powermockrunner, so if you can't run it, please replace it with another try @PrepareForTest (User.class) Public class TestUser extends TestCase {user user; Test the static method @Test public void Testverify () {mockstatic (user.class); When (User.getinstance ()). Thenreturn (New Subuser ()); User user=user.getinstance ();
Verifystatic ();
}
Testing a new Object
@Test public void Testwhennew () { try { mockstatic (user.class); Whennew (User.class). Withnoarguments (). Thenreturn (New Subuser ()); user=new user (); verifynew (user.withnoarguments (); } catch (Exception e) { //TODO Auto-generated Catch block e.printstacktrace (); }
@Test//test private method public void Testprivate () { User U = Powermockito.spy (New User ()); system.out.println (U.sayhello ("Hello"));
Powermockito.verifyprivate (U, Times (1)). Invoke ("Gethello", "Hello"); } catch (Exception e) {//TODO auto-generated catch block E.printstacktrace (); } }
}
Output---------------------------------
New Subuser ()
New Subuser ()
Hello World
For other uses, please refer here: http://code.google.com/p/powermock/wiki/MockitoUsage13
The above is a personal experience, may be expressed in error, please correct me.
http://blog.csdn.net/Ant_cc/article/details/6977367
http://dlwt.csdn.net/fd.php?i=686345406083133&s=8d1bea4adab5d744f78c6f60e13d3624
Powermock more powerful than Easymock and Mockito (RPM)