question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

2.0.8-beta -> 2.0.9-beta 'Unable to initialize @Spy annotated field

See original GitHub issue

No test failures when using 2.0.8-beta, but when incrementing to 2.0.9-beta, the following error occurs:

org.mockito.exceptions.base.MockitoException: Unable to initialize @Spy annotated field 'monitorTaskScheduler'.
Unable to create mock instance of type 'MonitorTaskScheduler'
    at net.project.dash.monitor.MonitorTaskScheduler$MockitoMock$1184625981.getExecutorService(Unknown Source)
    at net.project.dash.monitor.MonitorTaskScheduler.<init>(MonitorTaskScheduler.java:39)
    at net.project.dash.monitor.MonitorTaskScheduler$MockitoMock$1184625981.<init>(Unknown Source)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at org.mockito.internal.creation.instance.ConstructorInstantiator.invokeConstructor(ConstructorInstantiator.java:42)
    at org.mockito.internal.creation.instance.ConstructorInstantiator.noArgConstructor(ConstructorInstantiator.java:66)
    at org.mockito.internal.creation.instance.ConstructorInstantiator.newInstance(ConstructorInstantiator.java:17)
    at org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker.createMock(ByteBuddyMockMaker.java:27)
    at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:33)
    at org.mockito.internal.MockitoCore.mock(MockitoCore.java:59)
    at org.mockito.Mockito.mock(Mockito.java:1284)
    at org.mockito.internal.configuration.SpyAnnotationEngine.newSpyInstance(SpyAnnotationEngine.java:117)
    at org.mockito.internal.configuration.SpyAnnotationEngine.process(SpyAnnotationEngine.java:67)
    at org.mockito.internal.configuration.InjectingAnnotationEngine.processIndependentAnnotations(InjectingAnnotationEngine.java:73)
    at org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.java:55)
    at org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:108)
    at net.project.dash.monitor.MonitorTaskSchedulerTest.setUp(MonitorTaskSchedulerTest.java:38)

Relevant code (MonitorTaskSchedulerTest.java)

@Spy
MonitorTaskScheduler monitorTaskScheduler;

@Before
public void setUp()
{
    MockitoAnnotations.initMocks(this);
}

Relevant code (MonitorTaskScheduler.java)

public class MonitorTaskScheduler
{
    public MonitorTaskScheduler()
    {
        this.futureToTaskMap = new ConcurrentHashMap<>();
    }
}

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
bric3commented, Jun 6, 2015

Actually there maybe a better workaround, using the Mockito.spy() factory method.

class MonitorTaskSchedulerTest {

// @Spy
MonitorTaskScheduler monitorTaskScheduler = spy(new MinitorTaskScheduler());

This even works with @InjectMocks

0reactions
brcolowcommented, Jun 26, 2015

2d036ec fixed my original issue indeed! Thanks so much, great work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mockito - Unable to initialize Spy on HttpEntity - Stack Overflow
HttpEntity is an interface, it cannot be spied, only mocked. So just change the annotation with @Mock , or another option is to...
Read more >
InjectMocks (Mockito 2.0.8-beta API) - javadoc.io
Minimizes repetitive mock and spy injection. Mockito will try to inject mocks only either by constructor injection, setter injection, or property injection in ......
Read more >
Assessment of the Microbial Spoilage and Quality of ... - MDPI
Abstract: Fourier-transform infrared spectroscopy (FT-IR), multispectral imaging (MSI), and an elec- tronic nose (E-nose) were implemented ...
Read more >
iOS version history - Wikipedia
iOS is a mobile operating system developed by Apple Inc. It was first released as iPhone OS in June 2007. iPhone OS was...
Read more >
availables.xml - MIT
2.0 Bart Cremers A small calculator integrated in IntelliJ. ... Inspection that reports fields annotated with @PrivateWrite that are assigned a value, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found