Jmockit Learning Notes (two, mocked type)

Source: Internet
Author: User

declaring and using mocked types

A mock type can be passed in either an instance field or a parameter declaration. For the first case, this field belongs to the test class or Mockit. Subclass of expectations (a local field in a expectation). For the second case, the parameter must be the term one test method.

In all cases, a mock Field/parameter declaration must use Mockit. mocked annotation (@Mocked). In the expectation module, it is optional for mockfield/parameter to use this annotation. This annotation (or other annotation, such as @nonstrict) is necessary only if the mock field is declared in the test class. This creates a conflict to prevent other field in the test class from needing a mock.

For both Mockfields and parameters, all kinds of Java types are valid, except for primitive types and array types. The following types are therefore valid: Interfaces,concrete classes,abstract classes,finalclasses,enum types, and annotationtypes. Includes types in JRE (such as Java.lang, classes in Java.util).

For a mock Parameter,jmockit declared in a test method, an instance is automatically created for it and passed in by Junit/testngtest runner when the test method is invoked. So the parameter will never be null.

For a mock field,jmockit, an instance is not automatically created and passed to the field (except final field). In this scenario, (except finalfiled). This is the creation that needs to be shown in the test code and assigns a value to the field. The value may be null. mockedinstances

Mocked instances created by Jmockit can be used normally in test code. You can also pass it to the object being tested. Of course, it can not be used. By default, Jmockit does not care who invokes a mock instance. Allows you to create an instance of a class directly when you perform a new operation in the code being tested. These classes must be included in the mock type in the test code.

Mockingmultiple interfaces at the same time

Assume that the code being tested requires an object that implements two or more interfaces. Whether we can get a mocked instance that satisfies these conditions.

One way to do this is to write a test-specific interface that inherits all the interfaces needed in the test code. It is then used as a mock type.

But a better approach is as follows, and the method does not need to define a new interface.

public interface Dependency//a arbitrary custom interface
{
   String dosomething (boolean b);
}

Public final class Multimockstest<multimock extends Dependency & runnable>
{
   @Mocked multimock Multimock;

   @Test public
   void Mockfieldwithtwointerfaces ()
   {
      new nonstrictexpectations () {{}
         Multimock.dosomething (FALSE); result = "Test";
      }};

      Multimock.run ();
      Assertequals ("Test", multimock.dosomething (false));

      New Verifications () {{multimock.run ();

   }}} @Test public
   <m extends Dependency & serializable> void mockparameterwithtwointerfaces (final M mock)
   {
      new expectations () {{
         mock.dosomething (true);

      Assertequals ("", mock.dosomething (True));
   }


In any of the above test methods, two interfaces are both mock:

Dependency and java.lang.Runnable in a mock field

The dependency and java.io.Serializable in a mockparameter

When we use multimock and M -type variables, Jmockit knows all the interfaces he contains.

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.