Powermock is more powerful than Easymock and Mockito.

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.