Call to stub(...).toReturn(...) throws InvalidUseOfMatchersException during full build

852 views
Skip to first unread message

bringmebeer

unread,
Nov 24, 2008, 8:42:14 AM11/24/08
to mockito
Hi,

When I run my full test suite I get an InvalidUseOfMatchersException
thrown. This only happens when I run the full suite, both locally and
during my continuous integration builds.

* It's always a call to stub that throws the exception
* The test that fails due to this exception will pass if I run it
independently
* My CI server will report a different test as failing than that which
fails locally, but both tests pass when I run them independently
* This happens on all Mockito versions from 1.2 through 1.5

Does anyone have any idea what could be causing this?

Thanks for your help.

Jon

bringmebeer

unread,
Nov 24, 2008, 8:46:13 AM11/24/08
to mockito
btw - this is the full error...

org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
1 matchers expected, 2 recorded.
This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
//correct:
someMethod(anyObject(), eq("String by matcher"));

For more info see javadoc for Matchers class.

szczepiq

unread,
Nov 24, 2008, 8:50:06 AM11/24/08
to moc...@googlegroups.com
Hi,

Please show us the test case where it fails.

Cheers,
Szczepan Faber

bringmebeer

unread,
Nov 24, 2008, 10:39:17 AM11/24/08
to mockito
Hi this is the full test class code...

package com.informa.wcis.reports.web.components;

import static com.informa.java.util.CollectionUtils.asList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.stub;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import com.informa.utils.TapestryTestUtils;
import com.informa.wcis.reports.dom.specification.Criterion;
import com.informa.wcis.reports.dom.specification.Level;
import com.informa.wcis.reports.service.ReportService;

@RunWith(JUnit4.class)
public class CriteriaPaletteComponentSpec {

private static final Set<Criterion> CRITERIA = new HashSet<Criterion>
();

private CriteriaPaletteComponent palette;

private ReportService reportService;

@SuppressWarnings("serial")
@Before
public void context() {

reportService = mock(ReportService.class);
Map<String, Object> dependencies = new HashMap<String, Object>(){{put
("reportService", reportService);}};

palette = TapestryTestUtils.createTestableVersionOf
(CriteriaPaletteComponent.class, dependencies);
}

@Test
public void canGetCriterionSelectionModel() {

CriterionSingleSelectionModel model = new
CriterionSingleSelectionModel(asList(CRITERIA));
stub(reportService.getCriteriaFor(Level.NETWORK)).toReturn
(CRITERIA);

palette.setLevels("network");

assertThat(palette.getModel(), equalTo(model));
}

}

On Nov 24, 1:50 pm, szczepiq <szcze...@gmail.com> wrote:
> Hi,
>
> Please show us the test case where it fails.
>
> Cheers,
> Szczepan Faber
>

bringmebeer

unread,
Nov 24, 2008, 10:43:18 AM11/24/08
to mockito
the line that fails is

stub(reportService.getCriteriaFor(Level.NETWORK)).toReturn(CRITERIA);

Dan North

unread,
Nov 24, 2008, 11:23:44 AM11/24/08
to moc...@googlegroups.com
Is it me or is there a subliminal Mandelbrot beetle emerging from your imports?


2008/11/24 bringmebeer <bring...@gmail.com>

szczepiq

unread,
Nov 24, 2008, 12:18:16 PM11/24/08
to moc...@googlegroups.com
Yeah, that's the one: http://www.minworks.co.uk/mandelbrot/ :)

Coming back to your problem. I think somewhere in your test there is a
vagabond matcher used. Look for things like that:

....
anyObject();
//or:
someRealObject.someRealMethod(anyObject());
....
//then when you do this:
stub(mock.getStuff()).toReturn("stuff");
//mockito will cause an error because getStuff() gets no arguments but
there is a matcher recorded.

Unfortunately, Mockito can detect wrong usage only the next time when
you stub/verify. Look for invalid usage in a previous test.

Let me know how are you doing with the hunt. I'll think about making
exceptions better.

Cheers,
Szczepan Faber
Reply all
Reply to author
Forward
0 new messages