Use Powermockito to mock private methods

Source: Internet
Author: User

Say we do the so-called interface test is really nondescript ah, the test is controller layer, that is called interface wood?!!! But the eldest brother said is the interface test, then interface it!

Since I took over to write this interface test soon, a lot of people, including a better relationship with the architect told me that writing this is very troublesome Ah, the code is messy, you have to mock a heap.

Now that you have jumped in, swim for a while. In order to mock, it is a way to do, from the general mock tool Mockito, to the reflection of Java, and finally found a powerful tool--powermockito he actually can mock private methods. Although, at last, the boss said that private methods should not be mock, well, then I can always record, perhaps later will be used. Here is an example:


Doshowchildrens private method for simulating Threadcontroller
Val postcountof24hourmap= new Hashmap[string, Int] ()
Postcountof24hourmap.put ("1009", 26)
Postcountof24hourmap.put ("1010", 90)
Mav.addobject ("Postcountof24hourmap", Postcountof24hourmap)


    var childrensmap =new Hashmap[string,forumdynamicinfo] ()
     val forumdynamicinfo:forumdynamicinfo  = new Forumdynamicinfo
     Forumdynamicinfo.setrsscount (50)//Set number of followers
    childrensmap.put ("1010", null)
     childrensmap.put ("1009", Forumdynamicinfo)
    mav.addobject (" Childrensmap ", Childrensmap)

var childrens = new Linkedlist[forum] ()
Val childforum = new Forum
Childforum.setfid (1009)
Childforum.setname ("Test section")
Childforum.setdescription ("Record Your Mood")
Childforum.seticon ("Http://www.icon.com/icon.jpg")
Childforum.seticonurl ("http://www.19lou.com")
Val Moderator = new Moderator
Moderator.setusername ("moderator")
Childforum.setmoderatorlist (List (moderator))
Childrens.add (Childforum)
Mav.addobject ("childrens", childrens)


Powermockito.doreturn (MAV). When (Threadcontrollerspy, "Doshowchildrens", Mav,forum);

Http://blog.sina.com.cn/s/blog_6176c38201014dg1.html

Use Powermockito to mock private methods (GO)

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.