-1

I use maven and junit 4.5, mockito 1.7

If right click in the code in TestCaseA.java, select "Run as" -"Junit test" it is okay.
But if I right click parent package of TestCaseA.java, select "Run as" -"Junit test", it will fail:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Misplaced argument matcher detected! Somewhere before this line you probably misused Mockito argument matchers.
For example you might have used anyObject() argument matcher outside of verification or stubbing. Here are examples of correct usage of argument matchers: when(mock.get(anyInt())).thenReturn(null); doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject()); verify(mock).someMethod(contains("foo")); at testCaseA.setUP(testCaseA.java:33)

public TestCaseA{
SomeService service;
@Mock
private CommonService commonService;

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);***//it said this is error***
}

@Test
public void testvalidate() {
    //fail even here is empty
}
Jack
  • 49
  • 1
  • 9
  • How many other tests are in the package? Are you using any special runners (Mockito, Spring, etc)? – John B Jun 10 '14 at 17:01

1 Answers1

1

This usually happens because you've left a matcher on the matcher stack in some other test case. Mockito matchers actually work via side effects, such that a different test case can pollute your test environment.

Preface your other test classes with @RunWith(MockitoJUnitTestRunner.class) or add a method to each of them that looks like this:

@After
public void checkMockito() {
  Mockito.validateMockitoUsage();
}

...which is what MockitoJUnitTestRunner does, anyway, as well as calling initMocks(this) for you. At that point, one of your other test cases will fail, and you'll have a lot more luck debugging it in isolation. There's a very good chance you're stubbing or verifying a final method in that other test case. For more context, or to see some more debugging tips, see "How do Mockito matchers work?" also linked above.

Community
  • 1
  • 1
Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251