Mokito detailed Three

Source: Internet
Author: User
Tags abstract

Wake up, then come to the new note (since 1.8.5) @Captor, @Spy, @InjectMocks

@Captor when capturing is a parameter of a severe generic class and you want to avoid compiler warnings

public class test{

    @Captor argumentcaptor<asynccallback<foo>> captor;

    @Before public
    Void, init () {
       mockitoannotations.initmocks (this);
    }

    @Test public void Shoulddosomethinguseful () {
       //...
       Verify (mock). Dostuff (Captor.capture ());
       Assertequals ("foo", Captor.getvalue ());
    }
 }

@Spy-You can use it to replace the Spy (Object).
@InjectMocks-Automatically injects mock objects, or injects into the spy member variable

@InjectMocksye can also be used with @spy, meaning that Mockito will inject the mock into part of the simulation test

public class Articlemanagertest extends Samplebasetestcase {

       @Mock private articlecalculator calculator;
       @Mock (name = "Database") private articledatabase Dbmock; Note the mock name attribute
       @Spy private Userprovider userprovider = new Consumeruserprovider ();

       @InjectMocks private Articlemanager Manager;

       @Test public void shoulddosomething () {
           manager.initiatearticle ();
           Verify (Database). AddListener (Any (articlelistener.class));
       }
   }

   public class Samplebasetestcase {

       @Before public void Initmocks () {
           mockitoannotations.initmocks (this);
       }
   }
Timeout validation (since 1.8.5)
Passes when SomeMethod () is called within given time span
   verify (mock, timeout). SomeMethod ();
   Above
   is a alias to: Verify (mock, timeout). Times (1)). SomeMethod ();

   Passes when SomeMethod () is called *exactly* 2 times within given time span
   verify (mock, timeout). Times (2)). Some Method ();

   Passes when SomeMethod () is called *at least* 2 times within given time span
   verify (mock, timeout). AtLeast (2)). s Omemethod ();

   Verifies SomeMethod () within given time span using given verification mode
   //useful only if you have your own custom Verification modes.
   Verify (Mock, New Timeout (Yourownverificationmode)). SomeMethod ();
Automatic injection of member variables (since 1.9.0)
Mokito will attempt to initialize the @injectmocks variable inside the @spy, by constructing the method, set method or variable injection
 Instead:
 @Spy beerdrinker drinker = new Beerdrinker ();
 You can write:
 @Spy beerdrinker drinker;

 Same applies to @InjectMocks annotation:
 @InjectMocks localpub;
One-liner stubs
public class Cartest {
   Car boringstubbedcar = when (mock (car.class). Shiftgear ()). Thenthrow (Enginenotstarted.class ). Getmock ();

   @Test public void should ... {}

Mokito allows you to create a mock object subbing, allowing you to create stubs in a single line of code, which can be a checksum of the code looking more concise mock objects (since 1.9.5)

     Mockito.mockingdetails (Someobject). Ismock ();
     Mockito.mockingdetails (Someobject). Isspy ();

Method returns a Boolean value that verifies whether an object is a mock or spy object abstract class of mock or spy (since 1.10.12)

Convenience API, new overloaded Spy () method:
 someabstract spy = Spy (Someabstract.class);

 Robust API, via Settings builder:
 otherabstract spy = Mock (Otherabstract.class, Withsettings ()
    . Useconstructor (). Defaultanswer (Calls_real_methods));

 Mocking a non-static inner abstract class:
 innerabstract spy = Mock (Innerabstract.class, Withsettings ()
    . Useconstructor (). Outerinstance (outerinstance). Defaultanswer (Calls_real_methods));

Description information for the checksum error (since 2.0.0)
Would print a custom message on verification failure
Verify (Mock, description ("This would print on Failure")). SomeMethod ();

Would work with any verification mode
Verify (Mock, times (2). Description ("SomeMethod should be called twice"). SomeMethod ();

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.