我想在Finatra HttpClient中編寫以下函數的測試。嘲笑使用Mockito和斯卡拉的通用方法
def executeJson[T: Manifest](
request: Request,
expectedStatus: Status = Status.Ok): Future[T] = {...}
根據另一個問題在StackOverflow上回答。 mocking generic scala method in mockito。這是一個速記:
def executeJson[T](
request: Request,
expectedStatus: Status = Status.Ok)
(implicit T: Manifest[T]): Futuren[T] = {...}
所以,我想,
verify(httpClientMock, times(1)).executeJson[JiraProject]
(argThat(new RequestMatcher(req)))(Matchers.any())
不幸的是,它並沒有解決我的問題。我仍然有以下錯誤。
Invalid use of argument matchers!
0 matchers expected, 1 recorded:
-> at org.specs.mock.MockitoStubs$class.argThat(Mockito.scala:331)
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"));
我也試過Matchers.eq(Manifest[JiraProject])
,它抱怨說value Manifest of type scala.reflect.ManifestFactory.type does not take type parameters
。
我是新來的斯卡拉和Mockito,有什麼我做錯了或我誤解?
在此先感謝您的幫助!
發現問題了!所以executeJson實際上需要3個參數 - request,expectedStatus和一個Manifest。但是,因爲expectedStatus是可選的,所以我沒有明確地通過它,這就是爲什麼它抱怨。所以,最終的代碼應該是verify(httpClientMock, times(1)).executeJson[JiraProject](argThat(new RequestMatcher(req)), Matchers.any[Status])(Matchers.any())
是的,我認爲你是對的,我應該避免嘲笑第三方庫。 –