JAXBElement<SearchResponse> response = null;
JAXBElement<SearchRequest> req = soapClient.genConn(searchReq);
try {
response = (JAXBElement<SearchResponse>) getWebServiceTemplate().marshalSendAndReceive(req, new SoapActionCallback("search"));
} catch (RuntimeException e) {
throw new Exception(e);
}
public class SearchInvokerTest extends PackageTest{
@InjectMocks private SearchInvoker invoker;
@Mock private SearchSoapClient soapClient;
@Mock private WebServiceOperations template;
@Test
public void searchInvokerTest() throws Exception {
ObjectFactory factory = new ObjectFactory();
doReturn(factory.createSearchResponse(generateAwsSearchRsp())).when(template.marshalSendAndReceive(any(JAXBElement.class), any(WebServiceMessageCallback.class)));
SearchResponse rsp = invoker.doSearch(new SearchRequestDVO());
assertNotNull(rsp);
assertEquals("123", rsp.getTraceID());
}
}
[ERROR] Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 0.018 s <<< FAILURE! - in SearchInvokerTest
[ERROR] searchInvokerTest(SearchInvokerTest) Time elapsed: 0.002 s <<< ERROR!
java.lang.NullPointerException
at SearchInvokerTest.searchInvokerTest(SearchInvokerTest.java:33)
[ERROR] searchInvokerTest(SearchInvokerTest) Time elapsed: 0.017 s <<< ERROR!
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Misplaced or misused argument matcher detected here:
-> at SearchInvokerTest.searchInvokerTest(SymcorSearchInvokerTest.java:33)
-> at SearchInvokerTest.searchInvokerTest(SymcorSearchInvokerTest.java:33)
You cannot use argument matchers outside of verification or stubbing.
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"))
This message may appear after an NullPointerException if the last matcher is returning an object
like any() but the stubbed method signature expect a primitive argument, in this case,
use primitive alternatives.
when(mock.get(any())); // bad use, will raise NPE
when(mock.get(anyInt())); // correct usage use
Also, this error might show up because you use argument matchers with methods that cannot be mocked.
Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode().
Mocking methods declared on non-public parent classes is not supported.
错误消息表明模拟没有初始化。
您必须告诉JUnit使用Mockito运行器运行:
[...]
@RunWith(MockitoJRunner.class)
public class SearchInvokerTest extends PackageTest {
[...]
}
除其他外,这将初始化模拟。
异常堆栈跟踪
问题内容: 我有四个让我们说A,B,C,D的类,每个类都从另一个调用方法。 现在我已经模拟了类A,并且想模拟使用嘲笑的方法 并希望在递归方法调用上获取“ foo” 应该回来 我试过了 when(a.getB()。getC()。getD())。thenReturn(“ foo”); 但是得到了nullPointerException 然后我尝试 doReturn(“ foo”)。when(a.get
我有4个类让说A,B,C,D,每一个调用的方法从另一个。 现在我已经模拟了类A,并想使用mockito模拟一个方法 但得到nullPointerException 然后我试着 doReturn(“foo”).When(A.getb().getc().getd()); 但我不能一次就做到吗?如有任何帮助,我们将不胜感激。
使用mockito模拟一个方法会确保永远不会调用被模拟的方法吗?我有一个主类,它包含一些我想为其编写单元测试的代码,还有一个单元测试类MainTest,它包含主类的单元测试。 eg: 源类: JUnit测试(使用mockito) 这项测试失败了。为什么?
让我向您展示getCurrentWeatherWithForecastUsecase类actgually是什么样子: } //这很容易,它只需要一个天气存储库,并要求它获取结果。我把它发送回调用者,调用者将显示它。 更新: 以下是故障的整个堆栈跟踪:
我试图测试调用接口方法时是否传递了正确的值。我得到以下错误: org.mockito.exceptions.misusing.未完成验证异常:缺少的方法调用验证(mock)在这里:- 正确验证示例:验证(模拟)。doSomething() 此验证行上正在抛出错误: