gpt4 book ai didi

java - JUnit-Mockito 测试因 InvalidUseOfMatchersException 而失败

转载 作者:行者123 更新时间:2023-11-29 08:21:49 32 4
gpt4 key购买 nike

我在构建测试的 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));
}

}

最佳答案

只需提供准确的ArgumentMatchers

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

关于java - JUnit-Mockito 测试因 InvalidUseOfMatchersException 而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57097608/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com