JUnit-Mockito测试失败,并显示InvalidUseOfMatchersException

时间:2019-07-18 15:06:32

标签: java spring unit-testing junit mockito

我在构建测试的when-then部分时遇到了麻烦。运行测试时出现以下错误。

我已经研究过此SO链接(Mockito: InvalidUseOfMatchersException),并将代码重构为使用eq(),但仍然存在相同的错误。

错误:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
3 matchers expected, 2 recorded:
-> at com.myco.dao.ProductDaoTest.getOriginiatorUsingMsgId(ProductDaoTest.java:24)
-> at com.myco.dao.ProductDaoTest.getOriginiatorUsingMsgId(ProductDaoTest.java:24)

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.


    at com.myco.dao.ProductDaoTest.getOriginiatorUsingMsgId(ProductDaoTest.java:24)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)


Process finished with exit code -1

ProductDao.java

public class ProductDao extends DAOManager {

    private static final Logger logger = Logger.getLogger(ProductDao.class);
    private String orgnOfMsgQuery;

    public String getOrgnOfMsgQuery() {
        return orgnOfMsgQuery;
    }

    public void setOrgnOfMsgQuery(String orgnOfMsgQuery) {
        this.orgnOfMsgQuery = orgnOfMsgQuery;
    }

    @Override
    public String getOriginiatorUsingMsgId(String msgVerUniqueRefNum) {
        try {
            String originator = getJdbcTemplate().queryForObject(getOrgnOfMsgQuery(),
                    new Object[]{msgVerUniqueRefNum}, String.class);
            logger.info("Originator=" + originator + " for msg_ver_uuid_id=" + msgVerUniqueRefNum);
            return originator;
        } catch (Exception e) {
            logger.error("DB Error occurred while loading the originator for the msg : " + CommandControlServiceUtil.getCustomStackTrace(e));
            return null;
        }
    }

}

ProductDaoTest.java

@RunWith(MockitoJUnitRunner.class)
public class ProductDaoTest {

    private ProductDao instance = new ProductDao();
    private static final String SOME_STR_VAL = "some str value";
    private JdbcTemplate jdbcTemplate = Mockito.mock(JdbcTemplate.class);

    @Test
    public void getOriginiatorUsingMsgId() {
        instance.setJdbcTemplate(jdbcTemplate);
        //when-then
        when(instance.getJdbcTemplate().queryForObject(anyString(), any(), eq(String.class))).thenReturn(eq(SOME_STR_VAL));
        assertNotNull(instance.getOriginiatorUsingMsgId(SOME_STR_VAL));
    }

}

1 个答案:

答案 0 :(得分:4)

只需提供准确的ArgumentMatchers

when(instance.getJdbcTemplate().queryForObject(anyString(), any(Object[].class), eq(String.class))).thenReturn(SOME_STR_VAL);