Annotation Interface MockOsgiService


@Target(FIELD) @Retention(RUNTIME) public @interface MockOsgiService
Annotation used to mock OSGi services using Mockito. To mock an OSGi service, add the ServiceRegistrationRule to the Pax Exam test class and annotate any OSGi service field that needs to be mocked.

Service properties can be assigned to the service using the properties() attribute, and all supported Mockito Answers modes can be specified using the answer() attribute.

For instance, the following code would mock the Catalog Framework and Mime Type Resolver services in a Pax Exam test class:

 public class TestClass {
    @Rule
     public final ServiceRegistrationRule serviceRegistrationRule = new ServiceRegistrationRule();

    @MockOsgiService
     private CatalogFramework catalogFramework;

    @MockOsgiService(properties = {
        @Property(key = "name", value = "tikaMimeTypeResolver")})
     private MimeTypeResolver mimeTypeResolver;

     // ...
 }
 
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static @interface 
    Annotation used to assign a property to an OSGi service
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    org.mockito.Answers
    Answer mode to use for the mock
    Properties to assign to the service when registering the mock with OSGi.
  • Element Details

    • answer

      org.mockito.Answers answer
      Answer mode to use for the mock
      Default:
      RETURNS_DEFAULTS
    • properties

      Properties to assign to the service when registering the mock with OSGi.
      Default:
      {}