JUnit monomer Test (Powermockito) II

Source: Internet
Author: User

The distance from the last time to do junit monomer test for several months, and sure enough to use, and learn some new things, summed up.

  1. The first is the understanding of the spy, has been used to mock, did not use the spy, so the spy does not understand, recently used a few times, also in Google learning a bit, online said are more written, mock: all Mock;spy: part of the mock; What is called part but always can not understand, their actual use after feeling, all the mock should be the whole method to mock, do not actually walk method, directly return the mock return value, and the spy part of the mock should be although mock this method, and return the mock value we set, But in fact, the way to go to the mock, if the method is wrong, the same will report abnormal, affecting the current method of testing. Pure personal understanding!!! Misleading and irresponsible!!!!!

  2. The use of verify, has not used this thing, the general assertion is enough, this need to use, understand the role of the next he, I know the main four role: ① whether to invoke the success of the method; ② the number of times the method was invoked, the ③ parameter is correct ④ the certificate timeout.

  3. When using the spy Y should pay attention to a point, when the mock, do not use when the first, but with Doreturn ();

    E.g:powermockito.doreturn ("1234"). When (instance, "method", args); √

    E.g:powermockito.when (Instance, "method", args). Thenreturn (""); x

  4. Mock Private Method (public method down with private method case) (all mock)

    ① previous methods, using Whitebox.invoke ();

    ②powermockito.docallrealmethod (). When (instance). Method ();

    Powermockito.doreturn (). When (instance, "method", args);

  5. Mock Private Method (public method down with private method case) (partial mock)

    e.g:

    public class A () {


    Public String b (int id) {

    return C ();

    }


    Private String C () {

    Return "ABC"

    }}

    Test

    A = new A ();

    A aspy = Powermockito.spy (a);

    Powermockito.doreturn ("CCC"). When (Aspy, "C", 1);

    String RetValue = aspy.b (1);

    Powermockito.verifyprivate (Aspy). Invoke ("C");

  6. Mock static method

    ① @RunWith (Powermockrunner.class)

    @PrepareForTest ({staticclass.class})

    .......

    Powermockito.mock (Staticclass.class);

    Powermockito.when (Staticclass.method (args)). Thenreturn ("123")//Making Parameters Mockito.any ()

  7. Checking the static method verify

    Powermocito.verifystatic (Mockito.times (1));

    Staticclass.method (ARGS1,ARGS2);

  8. The static method under the Mock final class, the operation is the same as 5.

  9. Suppress method, skipping a line in the test

    E.g:powermockito.suppress (Powermockito.method (Staticclass.class, "method name", ARGS1,...));

  10. In the case of no more than the two mock private methods, we generally use reflection to implement, the call to the private method

    method = ClassName.class.getDeclaredMethod ("Private Method Name");

    Method.setaccessible (TRUE)//set accessible

  11. The current summary of these, new problems encountered, new receipts will continue to increase.

  12. Finally recommend a website, I learned a lot here: http://javatechnology.net/java/mockito-spy/

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

JUnit monomer Test (Powermockito) II

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.