JUnit monomer Test (Powermockito)

Source: Internet
Author: User

The last 1 months of the project at the end, in the company to do monomer testing, from the beginning of imitation, accumulated a number of things, recorded, prepared for a rainy day;

Jar Package Required:

Javassist-3.16.1-ga.jar

Mockito-all-1.9.5.jar

Powermock-api-mockito-1.5.6.jar

Powermock-api-support-1.5.6.jar

Powermock-core-1.5.6.jar

Powermock-module-junit4-1.5.6.jar

Powermock-module-junit4-common-1.5.6.jar

Powermock-reflect-1.5.6.jar

Junit-4.10.jar

When using a mock, the first thing to do is to add the annotation @preparefortest (class name. Class}) to the test class, where "class name. Class" is the class called in the class that needs to be mock

    1. When you need a private method called by a mock private method, you need to add @runwith (Powermockrunner.class), the rest does not need to be added, and you can use new when you create it.

    2. Public calls a private method with a return value

      Powermockito.when (Object, "Private method name", parameter). Thenreturn (expected value); This method is added to @runwith (Powermockrunner.class), where the object of the class cannot be created directly with new. You need to use Powermockito.mock (class name), and you need to put the class name. class in the annotation @preparefortest ({class name. class}) on the class;

    3. Replace objects in the original code with mock objects

      e.g:

      public class aa{

      Private User u;

      public void A () {

U.getname ();

}

}

@PrepareForTest ({user.class})

public class test{

@Test

public void test_001 () {

AA a = new AA ();

User u1 = Powermockito.mock (User.class);

Whitebox.setinternalstate (A, "U", U1);

Powermockito.when (U1.getname ()). Thenreturn (value);

}

}

4. To mock a method called by a method within a class

E.g:public class aa{

Private User U;

Private Ver V;

public void GetData () {

String a = U.getname ();

String B = v.getage ();

}

}

The process of testing: ①. First mock the entire class

②. Then call the method

③. The class that is then called by the Mock

④. The name of the new object in place of the code.

⑤. Mock a method

⑥. Adjustment method.

E.g:①.aa a = Powermockito.mock (Aa.class);

②.powermockito.docallrealmethod.when (a). GetData ();

③.user u1 = Powermockito.mock (User.class);

④.whitebox.setinternalstate (A, "U", U1);

⑤.powermockito.when (U1.getname ()). Thenreturn (value);

⑥.a.getdata ();

5. Test the anomaly

Powermockito.dothrow (New Exception ()). When (object). GetName ();

New Exception () is the anomaly you expect, and GetName is an unusual method;

6 ..... It's too hot, it's cold in writing ....


This article is from the "Riding monkey on the Tree" blog, please be sure to keep this source http://qihoushangshu.blog.51cto.com/7872138/1662073

JUnit monomer Test (Powermockito)

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.