All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mockito.Spy Maven / Gradle / Ivy

There is a newer version: 2.0.2-beta
Show newest version
/*
 * Copyright (c) 2007 Mockito contributors
 * This program is made available under the terms of the MIT License.
 */
package org.mockito;

import java.lang.annotation.*;

/**
 * Allows shorthand wrapping of field instances in an spy object.
 * 
 * 

* Example: * *


 * public class Test{
 *    //Instance for spying is created by calling constructor explicitly:
 *    @Spy Foo spyOnFoo = new Foo("argument");
 *    //Instance for spying is created by mockito via reflection (only default constructors supported): 
 *    @Spy Bar spyOnBar;
 *    @Before
 *    public void init(){
 *       MockitoAnnotations.initMocks(this);
 *    }
 *    ...
 * }
 * 
*

* Same as doing: * *


 * Foo spyOnFoo = Mockito.spy(new Foo("argument"));
 * Bar spyOnFoo = Mockito.spy(new Bar());
 * 
* * The field annotated with @Spy can be initialized by Mockito if a zero argument constructor * can be found in the type (even private). But Mockito cannot instantiate inner classes, local classes, * abstract classes and interfaces. * * The field annotated with @Spy can be initiatialized explicitly at declaration point. * Alternatively, if you don't provide the instance Mockito will try to find zero argument constructor (even private) and create an instance for you. * But Mockito cannot instantiate inner classes, local classes, abstract classes and interfaces. * * For example this class can be instantiated by Mockito : *
public class Bar {
 *    private Bar() {}
 *    public Bar(String publicConstructorWithOneArg) {}
 * }
*

* *

* Warning if you call MockitoAnnotations.initMocks(this) in a * super class constructor then this will not work. It is because fields * in subclass are only instantiated after super class constructor has returned. * It's better to use @Before. *

*/ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) @Documented public @interface Spy { }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy