/*
 * Copyright 2018, TeamDev. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package io.spine.test;

import org.mockito.Mockito;

import java.lang.reflect.Field;

import static com.google.common.base.Throwables.getRootCause;

/**
 * Utility for injecting a Mockito spy in a private field of an object.
 *
 * <p>Although, it's a not recommended way of testing, in some cases such an approach is needed for
 * testing interactions in complex objects.
 *
 * @author Alexander Yevsyukov
 */
public class Spy<T> {

    private final Class<T> classOfSpy;

    public static <T> Spy<T> ofClass(Class<T> spyClass) {
        return new Spy<>(spyClass);
    }

    private Spy(Class<T> classOfSpy) {
        this.classOfSpy = classOfSpy;
    }

    public T on(Object obj) {
        final String className = classOfSpy.getSimpleName();
        final String fieldName = className.substring(0, 1).toLowerCase() + className.substring(1);
        return on(obj, fieldName);
    }

    public T on(Object obj, String fieldName) {
        final Class<?> cls = obj.getClass();
        final Object spy;
        try {
            final Field field = cls.getDeclaredField(fieldName);
            field.setAccessible(true);
            final Object fieldValue = field.get(obj);
            spy = Mockito.spy(fieldValue);
            field.set(obj, spy);
            final T result = classOfSpy.cast(spy);
            return result;
        } catch (NoSuchFieldException | IllegalAccessException | ClassCastException e) {
            throw illegalArgumentWithCauseOf(e);
        }
    }

    private static IllegalArgumentException illegalArgumentWithCauseOf(Throwable throwable) {
        final Throwable rootCause = getRootCause(throwable);
        throw new IllegalArgumentException(rootCause);
    }
}

@@ -1030,16 +1030,79 @@
 .test;%0A%0A
+import com.google.errorprone.annotations.CanIgnoreReturnValue;%0A
 import o
@@ -1719,32 +1719,58 @@
 ssOfSpy;%0A    %7D%0A%0A
+    @CanIgnoreReturnValue%0A
     public T on(
@@ -1783,37 +1783,32 @@
  obj) %7B%0A        
-final
  String classNam
@@ -1847,21 +1847,16 @@
         
-final
  String 
@@ -1971,24 +1971,50 @@
 me);%0A    %7D%0A%0A
+    @CanIgnoreReturnValue%0A
     public T
@@ -2057,21 +2057,16 @@
         
-final
  Class%3C?
@@ -2089,37 +2089,32 @@
 lass();%0A        
-final
  Object spy;%0A   
@@ -2136,21 +2136,16 @@
         
-final
  Field f
@@ -2235,21 +2235,16 @@
         
-final
  Object 
@@ -2360,21 +2360,16 @@
         
-final
  T resul
@@ -2678,21 +2678,16 @@
         
-final
  Throwab

Make ignorable return of `Spy` methods