org.mockito.exceptions.base.MockitoException

Unable to initialize @Spy annotated field 'a'. null


Solutions on the web83

Solution icon of github
Unable to initialize @Spy annotated field 'a'. null

Solution icon of github
via GitHub by tonilopezmr
, 2 months ago
Unable to initialize @Spy annotated field 'getSuperHeroes'. Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference

Solution icon of github
Unable to initialize @Spy annotated field 'monitorTaskScheduler'. Unable to create mock instance of type 'MonitorTaskScheduler'

Solution icon of stackoverflow
Unable to initialize @Spy annotated field 'allRequestParams'. Type 'Map' is an interface and it cannot be spied on.

Solution icon of googlegroups
via Google Groups by Sebastian Konkol, 2 months ago
Cannot create a @Spy for 'bartender' field because the *instance* is missing The instance must be created *before* initMocks(); Example of correct usage of @Spy: @Spy List mock = new LinkedList(); //also, don't forget about MockitoAnnotations.initMocks();

Solution icon of stackoverflow
Cannot mock/spy class A Mockito cannot mock/spy following: - final classes - anonymous classes - primitive types

Solution icon of googlegroups
does not verify/stub final methods. 3. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies - - with doReturn|Throw() family of methods. More in javadocs for Mockito. spy() method.

Solution icon of stackoverflow
via Stack Overflow by danny.lesnik
, 11 months ago
Field 'testedPlugin' annotated with @InjectMocks is null. Please make sure the instance is created *before* MockitoAnnotations.initMocks(); Example of correct usage: class SomeTest { @InjectMocks private Foo foo = new Foo(); @Before public void setUp() { MockitoAnnotations.initMock(this);

Solution icon of stackoverflow
Field 'resource' annotated with @InjectMocks is null. Please make sure the instance is created *before* MockitoAnnotations.initMocks(); Example of correct usage: class SomeTest { @InjectMocks private Foo foo = new Foo(); @Before public void setUp() { MockitoAnnotations.initMock(this);

Solution icon of github
Cannot mock/spy class com.example.Foo Mockito cannot mock/spy because : - final class

Stack trace

  • org.mockito.exceptions.base.MockitoException: Unable to initialize @Spy annotated field 'a'. null at org.mockito.internal.configuration.SpyAnnotationEngine.process(SpyAnnotationEngine.java:70) at org.mockito.internal.configuration.InjectingAnnotationEngine.processIndependentAnnotations(InjectingAnnotationEngine.java:59) at org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.java:41)

Write tip

You have a different solution? A short tip here would help you and many other users who saw this issue last week.

Users with the same issue

You are the first who have seen this exception. Write a tip to help other users and build your expert profile.