package com.github.sbugat;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

import org.junit.After;
import org.mockito.Mock;
import org.mockito.Mockito;

/**
 * Generic test class to automatic check of all Mock marked with @Mock annotation by using verifyNoMoreInteractions on them.
 * 
 * @author Sylvain Bugat
 * 
 */
public abstract class GenericMockitoTest {

	@After
	public void verifyNoMoreInteractionsAllMocks() throws IllegalArgumentException, IllegalAccessException {

		Class<? extends GenericMockitoTest> currentClass = this.getClass();
		while (null != currentClass) {

			for (final Field field : currentClass.getDeclaredFields()) {

				final Annotation annotation = field.getAnnotation(Mock.class);

				if (null != annotation) {

					if (field.isAccessible()) {
						Mockito.verifyNoMoreInteractions(field.get(this));
					} else {
						field.setAccessible(true);
						Mockito.verifyNoMoreInteractions(field.get(this));
						field.setAccessible(false);
					}
				}
			}

			@SuppressWarnings("unchecked")
			final Class<? extends GenericMockitoTest> uncheckedCurrentClass = (Class<? extends GenericMockitoTest>) currentClass.getSuperclass();
			currentClass = uncheckedCurrentClass;
		}
	}
}

@@ -382,16 +382,294 @@
 Test %7B%0A%0A
+%09/**%0A%09 * Scan all instance field to get all ones with @Mock annotation and call verifyNoMoreInteractions on them.%0A%09 * %0A%09 * @throws IllegalArgumentException reflexion exception when acceding fields%0A%09 * @throws IllegalAccessException reflexion exception when acceding fields%0A%09 */%0A
 %09@After%0A
@@ -676,16 +676,22 @@
 %09public 
+final 
 void ver
@@ -781,16 +781,83 @@
 tion %7B%0A%0A
+%09%09// Get all class from the test class to GenericMockitoTest class%0A
 %09%09Class%3C
@@ -952,16 +952,59 @@
 ass) %7B%0A%0A
+%09%09%09// Scan all fields of the current class%0A
 %09%09%09for (
@@ -1056,24 +1056,83 @@
 ields()) %7B%0A%0A
+%09%09%09%09// Check if the current field has the @Mock annotation%0A
 %09%09%09%09final An
@@ -1186,17 +1186,16 @@
 class);%0A
-%0A
 %09%09%09%09if (
@@ -1213,24 +1213,118 @@
 otation) %7B%0A%0A
+%09%09%09%09%09// Get the mock object to check no more or no interactions%0A%09%09%09%09%09final Object mockObject;%0A
 %09%09%09%09%09if (fie
@@ -1354,66 +1354,155 @@
 %09%09%09%09
-Mockito.verifyNoMoreInteractions(field.get(this));
+mockObject = field.get(this);%0A%09%09%09%09%09%7D%0A%09%09%09%09%09// In the case of a unaccessible field (private, protected or package), set it accessible temporary
 %0A%09%09%09%09%09
-%7D 
 else
@@ -1547,41 +1547,21 @@
 %09%09%09%09
-Mockito.verifyNoMoreInteractions(
+mockObject = 
 fiel
@@ -1571,17 +1571,16 @@
 et(this)
-)
 ;%0A%09%09%09%09%09%09
@@ -1618,19 +1618,211 @@
 %09%09%7D%0A
-%09%09%09%09%7D%0A%09%09%09%7D%0A
+%0A%09%09%09%09%09Mockito.verifyNoMoreInteractions(mockObject);%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%0A%09%09%09// Get the super class of the current class%0A%09%09%09// An unchecked cast is needed because it's not possible to check generic type at runtime
 %0A%09%09%09

Add comments and minor changes