Issue with @Spy and OSX

1,020 views
Skip to first unread message

Sebastian Konkol

unread,
Dec 13, 2013, 2:45:05 AM12/13/13
to moc...@googlegroups.com
Hi,
some time ago we found interesting issue while using Mockito (great library BTW). The code presented below works great under Windows, but presents error under OSX. 

class MockitoTest.java:

import static org.junit.Assert.assertNotNull;

import org.junit.Before;
import org.junit.Test;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;

public class MockitoTest {
@Spy
private Bartender bartender;

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

@Test
public void shouldInstantiateSpy() throws Exception {
// given

// when
// then
assertNotNull(bartender);
}
}


class Bartender.java:

public class Bartender {

 public Bartender() {
// TODO Auto-generated constructor stub
}
}


Can anybody help me to understand - what am I doing incorrectly? Mockito version is 1.9.5 (downloaded from Maven) and Java version used by me is 1.6.45 (but error also occurred while using 1.7). 
Stack I'm receiving goes like this:

org.mockito.exceptions.base.MockitoException: 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();
at MockitoTest.setup(MockitoTest.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)


Best regards
Sebastian Konkol

KARR, DAVID

unread,
Dec 13, 2013, 5:47:34 PM12/13/13
to moc...@googlegroups.com

I suggest you add “@RunWith(MockitoJUnitRunner.class)” as a class annotation, and then remove the entire “setup()” method (unless it’s doing something else you’re not describing here).

 

 

--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at http://groups.google.com/group/mockito.
For more options, visit https://groups.google.com/groups/opt_out.

Brice Dutheil

unread,
Dec 18, 2013, 4:57:02 AM12/18/13
to moc...@googlegroups.com
There's nothing wrong with tis code, it should work. It certainly isn't an issue with the OS but rather an issue with the effective classpath on the dev environment on OSX. Before Mockito 1.9.5 spies were not automatically instanciated, that resulted in this exception.

Cheers

-- Brice
Reply all
Reply to author
Forward
0 new messages